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> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 40 | #include "ext4_jbd2.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 41 | #include "xattr.h" |
| 42 | #include "acl.h" |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 43 | #include "ext4_extents.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 44 | |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 45 | #define MPAGE_DA_EXTENT_TAIL 0x01 |
| 46 | |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 47 | static inline int ext4_begin_ordered_truncate(struct inode *inode, |
| 48 | loff_t new_size) |
| 49 | { |
| 50 | return jbd2_journal_begin_ordered_truncate(&EXT4_I(inode)->jinode, |
| 51 | new_size); |
| 52 | } |
| 53 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 54 | static void ext4_invalidatepage(struct page *page, unsigned long offset); |
| 55 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 56 | /* |
| 57 | * Test whether an inode is a fast symlink. |
| 58 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 59 | static int ext4_inode_is_fast_symlink(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 60 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 61 | int ea_blocks = EXT4_I(inode)->i_file_acl ? |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 62 | (inode->i_sb->s_blocksize >> 9) : 0; |
| 63 | |
| 64 | return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); |
| 65 | } |
| 66 | |
| 67 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 68 | * 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] | 69 | * which has been journaled. Metadata (eg. indirect blocks) must be |
| 70 | * revoked in all cases. |
| 71 | * |
| 72 | * "bh" may be NULL: a metadata block may have been freed from memory |
| 73 | * but there may still be a record of it in the journal, and that record |
| 74 | * still needs to be revoked. |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 75 | * |
| 76 | * If the handle isn't valid we're not journaling so there's nothing to do. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 77 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 78 | int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode, |
| 79 | struct buffer_head *bh, ext4_fsblk_t blocknr) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 80 | { |
| 81 | int err; |
| 82 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 83 | if (!ext4_handle_valid(handle)) |
| 84 | return 0; |
| 85 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 86 | might_sleep(); |
| 87 | |
| 88 | BUFFER_TRACE(bh, "enter"); |
| 89 | |
| 90 | jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, " |
| 91 | "data mode %lx\n", |
| 92 | bh, is_metadata, inode->i_mode, |
| 93 | test_opt(inode->i_sb, DATA_FLAGS)); |
| 94 | |
| 95 | /* Never use the revoke function if we are doing full data |
| 96 | * journaling: there is no need to, and a V1 superblock won't |
| 97 | * support it. Otherwise, only skip the revoke on un-journaled |
| 98 | * data blocks. */ |
| 99 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 100 | if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || |
| 101 | (!is_metadata && !ext4_should_journal_data(inode))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 102 | if (bh) { |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 103 | BUFFER_TRACE(bh, "call jbd2_journal_forget"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 104 | return ext4_journal_forget(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * data!=journal && (is_metadata || should_journal_data(inode)) |
| 111 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 112 | BUFFER_TRACE(bh, "call ext4_journal_revoke"); |
| 113 | err = ext4_journal_revoke(handle, blocknr, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 114 | if (err) |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 115 | ext4_abort(inode->i_sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 116 | "error %d when attempting revoke", err); |
| 117 | BUFFER_TRACE(bh, "exit"); |
| 118 | return err; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Work out how many blocks we need to proceed with the next chunk of a |
| 123 | * truncate transaction. |
| 124 | */ |
| 125 | static unsigned long blocks_for_truncate(struct inode *inode) |
| 126 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 127 | ext4_lblk_t needed; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 128 | |
| 129 | needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); |
| 130 | |
| 131 | /* Give ourselves just enough room to cope with inodes in which |
| 132 | * i_blocks is corrupt: we've seen disk corruptions in the past |
| 133 | * which resulted in random data in an inode which looked enough |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 134 | * like a regular file for ext4 to try to delete it. Things |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 135 | * will go a bit crazy if that happens, but at least we should |
| 136 | * try not to panic the whole kernel. */ |
| 137 | if (needed < 2) |
| 138 | needed = 2; |
| 139 | |
| 140 | /* But we need to bound the transaction so we don't overflow the |
| 141 | * journal. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 142 | if (needed > EXT4_MAX_TRANS_DATA) |
| 143 | needed = EXT4_MAX_TRANS_DATA; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 144 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 145 | return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Truncate transactions can be complex and absolutely huge. So we need to |
| 150 | * be able to restart the transaction at a conventient checkpoint to make |
| 151 | * sure we don't overflow the journal. |
| 152 | * |
| 153 | * start_transaction gets us a new handle for a truncate transaction, |
| 154 | * and extend_transaction tries to extend the existing one a bit. If |
| 155 | * extend fails, we need to propagate the failure up and restart the |
| 156 | * transaction in the top-level truncate loop. --sct |
| 157 | */ |
| 158 | static handle_t *start_transaction(struct inode *inode) |
| 159 | { |
| 160 | handle_t *result; |
| 161 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 162 | result = ext4_journal_start(inode, blocks_for_truncate(inode)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 163 | if (!IS_ERR(result)) |
| 164 | return result; |
| 165 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 166 | ext4_std_error(inode->i_sb, PTR_ERR(result)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 167 | return result; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Try to extend this transaction for the purposes of truncation. |
| 172 | * |
| 173 | * Returns 0 if we managed to create more room. If we can't create more |
| 174 | * room, and the transaction must be restarted we return 1. |
| 175 | */ |
| 176 | static int try_to_extend_transaction(handle_t *handle, struct inode *inode) |
| 177 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 178 | if (!ext4_handle_valid(handle)) |
| 179 | return 0; |
| 180 | if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 181 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 182 | if (!ext4_journal_extend(handle, blocks_for_truncate(inode))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 183 | return 0; |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Restart the transaction associated with *handle. This does a commit, |
| 189 | * so before we call here everything must be consistently dirtied against |
| 190 | * this transaction. |
| 191 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 192 | static int ext4_journal_test_restart(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 193 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 194 | BUG_ON(EXT4_JOURNAL(inode) == NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 195 | jbd_debug(2, "restarting handle %p\n", handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 196 | return ext4_journal_restart(handle, blocks_for_truncate(inode)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Called at the last iput() if i_nlink is zero. |
| 201 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 202 | void ext4_delete_inode(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 203 | { |
| 204 | handle_t *handle; |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 205 | int err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 206 | |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 207 | if (ext4_should_order_data(inode)) |
| 208 | ext4_begin_ordered_truncate(inode, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 209 | truncate_inode_pages(&inode->i_data, 0); |
| 210 | |
| 211 | if (is_bad_inode(inode)) |
| 212 | goto no_delete; |
| 213 | |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 214 | handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 215 | if (IS_ERR(handle)) { |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 216 | ext4_std_error(inode->i_sb, PTR_ERR(handle)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 217 | /* |
| 218 | * If we're going to skip the normal cleanup, we still need to |
| 219 | * make sure that the in-core orphan linked list is properly |
| 220 | * cleaned up. |
| 221 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 222 | ext4_orphan_del(NULL, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 223 | goto no_delete; |
| 224 | } |
| 225 | |
| 226 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 227 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 228 | inode->i_size = 0; |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 229 | err = ext4_mark_inode_dirty(handle, inode); |
| 230 | if (err) { |
| 231 | ext4_warning(inode->i_sb, __func__, |
| 232 | "couldn't mark inode dirty (err %d)", err); |
| 233 | goto stop_handle; |
| 234 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 235 | if (inode->i_blocks) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 236 | ext4_truncate(inode); |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * ext4_ext_truncate() doesn't reserve any slop when it |
| 240 | * restarts journal transactions; therefore there may not be |
| 241 | * enough credits left in the handle to remove the inode from |
| 242 | * the orphan list and set the dtime field. |
| 243 | */ |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 244 | if (!ext4_handle_has_enough_credits(handle, 3)) { |
Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 245 | err = ext4_journal_extend(handle, 3); |
| 246 | if (err > 0) |
| 247 | err = ext4_journal_restart(handle, 3); |
| 248 | if (err != 0) { |
| 249 | ext4_warning(inode->i_sb, __func__, |
| 250 | "couldn't extend journal (err %d)", err); |
| 251 | stop_handle: |
| 252 | ext4_journal_stop(handle); |
| 253 | goto no_delete; |
| 254 | } |
| 255 | } |
| 256 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 257 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 258 | * Kill off the orphan record which ext4_truncate created. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 259 | * AKPM: I think this can be inside the above `if'. |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 260 | * 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] | 261 | * deletion of a non-existent orphan - this is because we don't |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 262 | * know if ext4_truncate() actually created an orphan record. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 263 | * (Well, we could do this if we need to, but heck - it works) |
| 264 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 265 | ext4_orphan_del(handle, inode); |
| 266 | EXT4_I(inode)->i_dtime = get_seconds(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * One subtle ordering requirement: if anything has gone wrong |
| 270 | * (transaction abort, IO errors, whatever), then we can still |
| 271 | * do these next steps (the fs will already have been marked as |
| 272 | * having errors), but we can't free the inode if the mark_dirty |
| 273 | * fails. |
| 274 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 275 | if (ext4_mark_inode_dirty(handle, inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 276 | /* If that failed, just do the required in-core inode clear. */ |
| 277 | clear_inode(inode); |
| 278 | else |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 279 | ext4_free_inode(handle, inode); |
| 280 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 281 | return; |
| 282 | no_delete: |
| 283 | clear_inode(inode); /* We must guarantee clearing of inode... */ |
| 284 | } |
| 285 | |
| 286 | typedef struct { |
| 287 | __le32 *p; |
| 288 | __le32 key; |
| 289 | struct buffer_head *bh; |
| 290 | } Indirect; |
| 291 | |
| 292 | static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) |
| 293 | { |
| 294 | p->key = *(p->p = v); |
| 295 | p->bh = bh; |
| 296 | } |
| 297 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 298 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 299 | * ext4_block_to_path - parse the block number into array of offsets |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 300 | * @inode: inode in question (we are only interested in its superblock) |
| 301 | * @i_block: block number to be parsed |
| 302 | * @offsets: array to store the offsets in |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 303 | * @boundary: set this non-zero if the referred-to block is likely to be |
| 304 | * followed (on disk) by an indirect block. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 305 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 306 | * 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] | 307 | * for UNIX filesystems - tree of pointers anchored in the inode, with |
| 308 | * data blocks at leaves and indirect blocks in intermediate nodes. |
| 309 | * This function translates the block number into path in that tree - |
| 310 | * return value is the path length and @offsets[n] is the offset of |
| 311 | * pointer to (n+1)th node in the nth one. If @block is out of range |
| 312 | * (negative or too large) warning is printed and zero returned. |
| 313 | * |
| 314 | * Note: function doesn't find node addresses, so no IO is needed. All |
| 315 | * we need to know is the capacity of indirect blocks (taken from the |
| 316 | * inode->i_sb). |
| 317 | */ |
| 318 | |
| 319 | /* |
| 320 | * Portability note: the last comparison (check that we fit into triple |
| 321 | * indirect block) is spelled differently, because otherwise on an |
| 322 | * architecture with 32-bit longs and 8Kb pages we might get into trouble |
| 323 | * if our filesystem had 8Kb blocks. We might use long long, but that would |
| 324 | * kill us on x86. Oh, well, at least the sign propagation does not matter - |
| 325 | * i_block would have to be negative in the very beginning, so we would not |
| 326 | * get there at all. |
| 327 | */ |
| 328 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 329 | static int ext4_block_to_path(struct inode *inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 330 | ext4_lblk_t i_block, |
| 331 | ext4_lblk_t offsets[4], int *boundary) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 332 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 333 | int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
| 334 | int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); |
| 335 | const long direct_blocks = EXT4_NDIR_BLOCKS, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 336 | indirect_blocks = ptrs, |
| 337 | double_blocks = (1 << (ptrs_bits * 2)); |
| 338 | int n = 0; |
| 339 | int final = 0; |
| 340 | |
| 341 | if (i_block < 0) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 342 | ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 343 | } else if (i_block < direct_blocks) { |
| 344 | offsets[n++] = i_block; |
| 345 | final = direct_blocks; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 346 | } else if ((i_block -= direct_blocks) < indirect_blocks) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 347 | offsets[n++] = EXT4_IND_BLOCK; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 348 | offsets[n++] = i_block; |
| 349 | final = ptrs; |
| 350 | } else if ((i_block -= indirect_blocks) < double_blocks) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 351 | offsets[n++] = EXT4_DIND_BLOCK; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 352 | offsets[n++] = i_block >> ptrs_bits; |
| 353 | offsets[n++] = i_block & (ptrs - 1); |
| 354 | final = ptrs; |
| 355 | } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 356 | offsets[n++] = EXT4_TIND_BLOCK; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 357 | offsets[n++] = i_block >> (ptrs_bits * 2); |
| 358 | offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); |
| 359 | offsets[n++] = i_block & (ptrs - 1); |
| 360 | final = ptrs; |
| 361 | } else { |
Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 362 | ext4_warning(inode->i_sb, "ext4_block_to_path", |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 363 | "block %lu > max", |
Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 364 | i_block + direct_blocks + |
| 365 | indirect_blocks + double_blocks); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 366 | } |
| 367 | if (boundary) |
| 368 | *boundary = final - 1 - (i_block & (ptrs - 1)); |
| 369 | return n; |
| 370 | } |
| 371 | |
| 372 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 373 | * ext4_get_branch - read the chain of indirect blocks leading to data |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 374 | * @inode: inode in question |
| 375 | * @depth: depth of the chain (1 - direct pointer, etc.) |
| 376 | * @offsets: offsets of pointers in inode/indirect blocks |
| 377 | * @chain: place to store the result |
| 378 | * @err: here we store the error value |
| 379 | * |
| 380 | * Function fills the array of triples <key, p, bh> and returns %NULL |
| 381 | * if everything went OK or the pointer to the last filled triple |
| 382 | * (incomplete one) otherwise. Upon the return chain[i].key contains |
| 383 | * the number of (i+1)-th block in the chain (as it is stored in memory, |
| 384 | * i.e. little-endian 32-bit), chain[i].p contains the address of that |
| 385 | * number (it points into struct inode for i==0 and into the bh->b_data |
| 386 | * for i>0) and chain[i].bh points to the buffer_head of i-th indirect |
| 387 | * block for i>0 and NULL for i==0. In other words, it holds the block |
| 388 | * numbers of the chain, addresses they were taken from (and where we can |
| 389 | * verify that chain did not change) and buffer_heads hosting these |
| 390 | * numbers. |
| 391 | * |
| 392 | * Function stops when it stumbles upon zero pointer (absent block) |
| 393 | * (pointer to last triple returned, *@err == 0) |
| 394 | * or when it gets an IO error reading an indirect block |
| 395 | * (ditto, *@err == -EIO) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 396 | * or when it reads all @depth-1 indirect blocks successfully and finds |
| 397 | * 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] | 398 | * |
| 399 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 400 | * down_read(&EXT4_I(inode)->i_data_sem) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 401 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 402 | static Indirect *ext4_get_branch(struct inode *inode, int depth, |
| 403 | ext4_lblk_t *offsets, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 404 | Indirect chain[4], int *err) |
| 405 | { |
| 406 | struct super_block *sb = inode->i_sb; |
| 407 | Indirect *p = chain; |
| 408 | struct buffer_head *bh; |
| 409 | |
| 410 | *err = 0; |
| 411 | /* i_data is not going away, no lock needed */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 412 | add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 413 | if (!p->key) |
| 414 | goto no_block; |
| 415 | while (--depth) { |
| 416 | bh = sb_bread(sb, le32_to_cpu(p->key)); |
| 417 | if (!bh) |
| 418 | goto failure; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 419 | add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 420 | /* Reader: end */ |
| 421 | if (!p->key) |
| 422 | goto no_block; |
| 423 | } |
| 424 | return NULL; |
| 425 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 426 | failure: |
| 427 | *err = -EIO; |
| 428 | no_block: |
| 429 | return p; |
| 430 | } |
| 431 | |
| 432 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 433 | * ext4_find_near - find a place for allocation with sufficient locality |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 434 | * @inode: owner |
| 435 | * @ind: descriptor of indirect block. |
| 436 | * |
Benoit Boissinot | 1cc8dcf | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 437 | * This function returns the preferred place for block allocation. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 438 | * It is used when heuristic for sequential allocation fails. |
| 439 | * Rules are: |
| 440 | * + if there is a block to the left of our position - allocate near it. |
| 441 | * + if pointer will live in indirect block - allocate near that block. |
| 442 | * + if pointer will live in inode - allocate in the same |
| 443 | * cylinder group. |
| 444 | * |
| 445 | * In the latter case we colour the starting block by the callers PID to |
| 446 | * prevent it from clashing with concurrent allocations for a different inode |
| 447 | * in the same block group. The PID is used here so that functionally related |
| 448 | * files will be close-by on-disk. |
| 449 | * |
| 450 | * Caller must make sure that @ind is valid and will stay that way. |
| 451 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 452 | static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 453 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 454 | struct ext4_inode_info *ei = EXT4_I(inode); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 455 | __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 456 | __le32 *p; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 457 | ext4_fsblk_t bg_start; |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 458 | ext4_fsblk_t last_block; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 459 | ext4_grpblk_t colour; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 460 | |
| 461 | /* Try to find previous block */ |
| 462 | for (p = ind->p - 1; p >= start; p--) { |
| 463 | if (*p) |
| 464 | return le32_to_cpu(*p); |
| 465 | } |
| 466 | |
| 467 | /* No such thing, so let's try location of indirect block */ |
| 468 | if (ind->bh) |
| 469 | return ind->bh->b_blocknr; |
| 470 | |
| 471 | /* |
| 472 | * It is going to be referred to from the inode itself? OK, just put it |
| 473 | * into the same cylinder group then. |
| 474 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 475 | bg_start = ext4_group_first_block_no(inode->i_sb, ei->i_block_group); |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 476 | last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; |
| 477 | |
| 478 | if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) |
| 479 | colour = (current->pid % 16) * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 480 | (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 481 | else |
| 482 | colour = (current->pid % 16) * ((last_block - bg_start) / 16); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 483 | return bg_start + colour; |
| 484 | } |
| 485 | |
| 486 | /** |
Benoit Boissinot | 1cc8dcf | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 487 | * ext4_find_goal - find a preferred place for allocation. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 488 | * @inode: owner |
| 489 | * @block: block we want |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 490 | * @partial: pointer to the last triple within a chain |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 491 | * |
Benoit Boissinot | 1cc8dcf | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 492 | * Normally this function find the preferred place for block allocation, |
Akinobu Mita | fb01bfd | 2008-02-06 01:40:16 -0800 | [diff] [blame] | 493 | * returns it. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 494 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 495 | static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, |
Akinobu Mita | fb01bfd | 2008-02-06 01:40:16 -0800 | [diff] [blame] | 496 | Indirect *partial) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 497 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 498 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 499 | * XXX need to get goal block from mballoc's data structures |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 500 | */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 501 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 502 | return ext4_find_near(inode, partial); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 506 | * 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] | 507 | * of direct blocks need to be allocated for the given branch. |
| 508 | * |
| 509 | * @branch: chain of indirect blocks |
| 510 | * @k: number of blocks need for indirect blocks |
| 511 | * @blks: number of data blocks to be mapped. |
| 512 | * @blocks_to_boundary: the offset in the indirect block |
| 513 | * |
| 514 | * return the total number of blocks to be allocate, including the |
| 515 | * direct and indirect blocks. |
| 516 | */ |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 517 | static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 518 | int blocks_to_boundary) |
| 519 | { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 520 | unsigned int count = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 521 | |
| 522 | /* |
| 523 | * Simple case, [t,d]Indirect block(s) has not allocated yet |
| 524 | * then it's clear blocks on that path have not allocated |
| 525 | */ |
| 526 | if (k > 0) { |
| 527 | /* right now we don't handle cross boundary allocation */ |
| 528 | if (blks < blocks_to_boundary + 1) |
| 529 | count += blks; |
| 530 | else |
| 531 | count += blocks_to_boundary + 1; |
| 532 | return count; |
| 533 | } |
| 534 | |
| 535 | count++; |
| 536 | while (count < blks && count <= blocks_to_boundary && |
| 537 | le32_to_cpu(*(branch[0].p + count)) == 0) { |
| 538 | count++; |
| 539 | } |
| 540 | return count; |
| 541 | } |
| 542 | |
| 543 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 544 | * ext4_alloc_blocks: multiple allocate blocks needed for a branch |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 545 | * @indirect_blks: the number of blocks need to allocate for indirect |
| 546 | * blocks |
| 547 | * |
| 548 | * @new_blocks: on return it will store the new block numbers for |
| 549 | * the indirect blocks(if needed) and the first direct block, |
| 550 | * @blks: on return it will store the total number of allocated |
| 551 | * direct blocks |
| 552 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 553 | static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 554 | ext4_lblk_t iblock, ext4_fsblk_t goal, |
| 555 | int indirect_blks, int blks, |
| 556 | ext4_fsblk_t new_blocks[4], int *err) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 557 | { |
Theodore Ts'o | 815a113 | 2009-01-01 23:59:43 -0500 | [diff] [blame] | 558 | struct ext4_allocation_request ar; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 559 | int target, i; |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 560 | unsigned long count = 0, blk_allocated = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 561 | int index = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 562 | ext4_fsblk_t current_block = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 563 | int ret = 0; |
| 564 | |
| 565 | /* |
| 566 | * Here we try to allocate the requested multiple blocks at once, |
| 567 | * on a best-effort basis. |
| 568 | * To build a branch, we should allocate blocks for |
| 569 | * the indirect blocks(if not allocated yet), and at least |
| 570 | * the first direct block of this branch. That's the |
| 571 | * minimum number of blocks need to allocate(required) |
| 572 | */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 573 | /* first we try to allocate the indirect blocks */ |
| 574 | target = indirect_blks; |
| 575 | while (target > 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 576 | count = target; |
| 577 | /* allocating blocks for indirect blocks and direct blocks */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 578 | current_block = ext4_new_meta_blocks(handle, inode, |
| 579 | goal, &count, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 580 | if (*err) |
| 581 | goto failed_out; |
| 582 | |
| 583 | target -= count; |
| 584 | /* allocate blocks for indirect blocks */ |
| 585 | while (index < indirect_blks && count) { |
| 586 | new_blocks[index++] = current_block++; |
| 587 | count--; |
| 588 | } |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 589 | if (count > 0) { |
| 590 | /* |
| 591 | * save the new block number |
| 592 | * for the first direct block |
| 593 | */ |
| 594 | new_blocks[index] = current_block; |
| 595 | printk(KERN_INFO "%s returned more blocks than " |
| 596 | "requested\n", __func__); |
| 597 | WARN_ON(1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 598 | break; |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 599 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 602 | target = blks - count ; |
| 603 | blk_allocated = count; |
| 604 | if (!target) |
| 605 | goto allocated; |
| 606 | /* Now allocate data blocks */ |
Theodore Ts'o | 815a113 | 2009-01-01 23:59:43 -0500 | [diff] [blame] | 607 | memset(&ar, 0, sizeof(ar)); |
| 608 | ar.inode = inode; |
| 609 | ar.goal = goal; |
| 610 | ar.len = target; |
| 611 | ar.logical = iblock; |
| 612 | if (S_ISREG(inode->i_mode)) |
| 613 | /* enable in-core preallocation only for regular files */ |
| 614 | ar.flags = EXT4_MB_HINT_DATA; |
| 615 | |
| 616 | current_block = ext4_mb_new_blocks(handle, &ar, err); |
| 617 | |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 618 | if (*err && (target == blks)) { |
| 619 | /* |
| 620 | * if the allocation failed and we didn't allocate |
| 621 | * any blocks before |
| 622 | */ |
| 623 | goto failed_out; |
| 624 | } |
| 625 | if (!*err) { |
| 626 | if (target == blks) { |
| 627 | /* |
| 628 | * save the new block number |
| 629 | * for the first direct block |
| 630 | */ |
| 631 | new_blocks[index] = current_block; |
| 632 | } |
Theodore Ts'o | 815a113 | 2009-01-01 23:59:43 -0500 | [diff] [blame] | 633 | blk_allocated += ar.len; |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 634 | } |
| 635 | allocated: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 636 | /* total number of blocks allocated for direct blocks */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 637 | ret = blk_allocated; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 638 | *err = 0; |
| 639 | return ret; |
| 640 | failed_out: |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 641 | for (i = 0; i < index; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 642 | ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 647 | * ext4_alloc_branch - allocate and set up a chain of blocks. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 648 | * @inode: owner |
| 649 | * @indirect_blks: number of allocated indirect blocks |
| 650 | * @blks: number of allocated direct blocks |
| 651 | * @offsets: offsets (in the blocks) to store the pointers to next. |
| 652 | * @branch: place to store the chain in. |
| 653 | * |
| 654 | * This function allocates blocks, zeroes out all but the last one, |
| 655 | * links them into chain and (if we are synchronous) writes them to disk. |
| 656 | * In other words, it prepares a branch that can be spliced onto the |
| 657 | * inode. It stores the information about that chain in the branch[], in |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 658 | * 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] | 659 | * we had read the existing part of chain and partial points to the last |
| 660 | * 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] | 661 | * picture as after the successful ext4_get_block(), except that in one |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 662 | * place chain is disconnected - *branch->p is still zero (we did not |
| 663 | * set the last link), but branch->key contains the number that should |
| 664 | * be placed into *branch->p to fill that gap. |
| 665 | * |
| 666 | * If allocation fails we free all blocks we've allocated (and forget |
| 667 | * their buffer_heads) and return the error value the from failed |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 668 | * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 669 | * as described above and return 0. |
| 670 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 671 | static int ext4_alloc_branch(handle_t *handle, struct inode *inode, |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 672 | ext4_lblk_t iblock, int indirect_blks, |
| 673 | int *blks, ext4_fsblk_t goal, |
| 674 | ext4_lblk_t *offsets, Indirect *branch) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 675 | { |
| 676 | int blocksize = inode->i_sb->s_blocksize; |
| 677 | int i, n = 0; |
| 678 | int err = 0; |
| 679 | struct buffer_head *bh; |
| 680 | int num; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 681 | ext4_fsblk_t new_blocks[4]; |
| 682 | ext4_fsblk_t current_block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 683 | |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 684 | num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 685 | *blks, new_blocks, &err); |
| 686 | if (err) |
| 687 | return err; |
| 688 | |
| 689 | branch[0].key = cpu_to_le32(new_blocks[0]); |
| 690 | /* |
| 691 | * metadata blocks and data blocks are allocated. |
| 692 | */ |
| 693 | for (n = 1; n <= indirect_blks; n++) { |
| 694 | /* |
| 695 | * Get buffer_head for parent block, zero it out |
| 696 | * and set the pointer to new one, then send |
| 697 | * parent to disk. |
| 698 | */ |
| 699 | bh = sb_getblk(inode->i_sb, new_blocks[n-1]); |
| 700 | branch[n].bh = bh; |
| 701 | lock_buffer(bh); |
| 702 | BUFFER_TRACE(bh, "call get_create_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 703 | err = ext4_journal_get_create_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 704 | if (err) { |
| 705 | unlock_buffer(bh); |
| 706 | brelse(bh); |
| 707 | goto failed; |
| 708 | } |
| 709 | |
| 710 | memset(bh->b_data, 0, blocksize); |
| 711 | branch[n].p = (__le32 *) bh->b_data + offsets[n]; |
| 712 | branch[n].key = cpu_to_le32(new_blocks[n]); |
| 713 | *branch[n].p = branch[n].key; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 714 | if (n == indirect_blks) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 715 | current_block = new_blocks[n]; |
| 716 | /* |
| 717 | * End of chain, update the last new metablock of |
| 718 | * the chain to point to the new allocated |
| 719 | * data blocks numbers |
| 720 | */ |
| 721 | for (i=1; i < num; i++) |
| 722 | *(branch[n].p + i) = cpu_to_le32(++current_block); |
| 723 | } |
| 724 | BUFFER_TRACE(bh, "marking uptodate"); |
| 725 | set_buffer_uptodate(bh); |
| 726 | unlock_buffer(bh); |
| 727 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 728 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
| 729 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 730 | if (err) |
| 731 | goto failed; |
| 732 | } |
| 733 | *blks = num; |
| 734 | return err; |
| 735 | failed: |
| 736 | /* Allocation failed, free what we already allocated */ |
| 737 | for (i = 1; i <= n ; i++) { |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 738 | BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 739 | ext4_journal_forget(handle, branch[i].bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 740 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 741 | for (i = 0; i < indirect_blks; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 742 | ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 743 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 744 | ext4_free_blocks(handle, inode, new_blocks[i], num, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 745 | |
| 746 | return err; |
| 747 | } |
| 748 | |
| 749 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 750 | * ext4_splice_branch - splice the allocated branch onto inode. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 751 | * @inode: owner |
| 752 | * @block: (logical) number of block we are adding |
| 753 | * @chain: chain of indirect blocks (with a missing link - see |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 754 | * ext4_alloc_branch) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 755 | * @where: location of missing link |
| 756 | * @num: number of indirect blocks we are adding |
| 757 | * @blks: number of direct blocks we are adding |
| 758 | * |
| 759 | * This function fills the missing link and does all housekeeping needed in |
| 760 | * inode (->i_blocks, etc.). In case of success we end up with the full |
| 761 | * chain to new block and return 0. |
| 762 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 763 | static int ext4_splice_branch(handle_t *handle, struct inode *inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 764 | ext4_lblk_t block, Indirect *where, int num, int blks) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 765 | { |
| 766 | int i; |
| 767 | int err = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 768 | ext4_fsblk_t current_block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 769 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 770 | /* |
| 771 | * If we're splicing into a [td]indirect block (as opposed to the |
| 772 | * inode) then we need to get write access to the [td]indirect block |
| 773 | * before the splice. |
| 774 | */ |
| 775 | if (where->bh) { |
| 776 | BUFFER_TRACE(where->bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 777 | err = ext4_journal_get_write_access(handle, where->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 778 | if (err) |
| 779 | goto err_out; |
| 780 | } |
| 781 | /* That's it */ |
| 782 | |
| 783 | *where->p = where->key; |
| 784 | |
| 785 | /* |
| 786 | * Update the host buffer_head or inode to point to more just allocated |
| 787 | * direct blocks blocks |
| 788 | */ |
| 789 | if (num == 0 && blks > 1) { |
| 790 | current_block = le32_to_cpu(where->key) + 1; |
| 791 | for (i = 1; i < blks; i++) |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 792 | *(where->p + i) = cpu_to_le32(current_block++); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 795 | /* We are done with atomic stuff, now do the rest of housekeeping */ |
| 796 | |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 797 | inode->i_ctime = ext4_current_time(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 798 | ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 799 | |
| 800 | /* had we spliced it onto indirect block? */ |
| 801 | if (where->bh) { |
| 802 | /* |
| 803 | * If we spliced it onto an indirect block, we haven't |
| 804 | * altered the inode. Note however that if it is being spliced |
| 805 | * onto an indirect block at the very end of the file (the |
| 806 | * file is growing) then we *will* alter the inode to reflect |
| 807 | * 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] | 808 | * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 809 | */ |
| 810 | jbd_debug(5, "splicing indirect only\n"); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 811 | BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata"); |
| 812 | err = ext4_handle_dirty_metadata(handle, inode, where->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 813 | if (err) |
| 814 | goto err_out; |
| 815 | } else { |
| 816 | /* |
| 817 | * OK, we spliced it into the inode itself on a direct block. |
| 818 | * Inode was dirtied above. |
| 819 | */ |
| 820 | jbd_debug(5, "splicing direct\n"); |
| 821 | } |
| 822 | return err; |
| 823 | |
| 824 | err_out: |
| 825 | for (i = 1; i <= num; i++) { |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 826 | BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 827 | ext4_journal_forget(handle, where[i].bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 828 | ext4_free_blocks(handle, inode, |
| 829 | le32_to_cpu(where[i-1].key), 1, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 830 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 831 | 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] | 832 | |
| 833 | return err; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Allocation strategy is simple: if we have to allocate something, we will |
| 838 | * have to go the whole way to leaf. So let's do it before attaching anything |
| 839 | * to tree, set linkage between the newborn blocks, write them if sync is |
| 840 | * required, recheck the path, free and repeat if check fails, otherwise |
| 841 | * set the last missing link (that will protect us from any truncate-generated |
| 842 | * removals - all blocks on the path are immune now) and possibly force the |
| 843 | * write on the parent block. |
| 844 | * That has a nice additional property: no special recovery from the failed |
| 845 | * allocations is needed - we simply release blocks and do not touch anything |
| 846 | * reachable from inode. |
| 847 | * |
| 848 | * `handle' can be NULL if create == 0. |
| 849 | * |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 850 | * return > 0, # of blocks mapped or allocated. |
| 851 | * return = 0, if plain lookup failed. |
| 852 | * return < 0, error case. |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 853 | * |
| 854 | * |
| 855 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 856 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
| 857 | * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 858 | */ |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 859 | static int ext4_get_blocks_handle(handle_t *handle, struct inode *inode, |
| 860 | ext4_lblk_t iblock, unsigned int maxblocks, |
| 861 | struct buffer_head *bh_result, |
| 862 | int create, int extend_disksize) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 863 | { |
| 864 | int err = -EIO; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 865 | ext4_lblk_t offsets[4]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 866 | Indirect chain[4]; |
| 867 | Indirect *partial; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 868 | ext4_fsblk_t goal; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 869 | int indirect_blks; |
| 870 | int blocks_to_boundary = 0; |
| 871 | int depth; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 872 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 873 | int count = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 874 | ext4_fsblk_t first_block = 0; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 875 | loff_t disksize; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 876 | |
| 877 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 878 | J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 879 | J_ASSERT(handle != NULL || create == 0); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 880 | depth = ext4_block_to_path(inode, iblock, offsets, |
| 881 | &blocks_to_boundary); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 882 | |
| 883 | if (depth == 0) |
| 884 | goto out; |
| 885 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 886 | partial = ext4_get_branch(inode, depth, offsets, chain, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 887 | |
| 888 | /* Simplest case - block found, no allocation needed */ |
| 889 | if (!partial) { |
| 890 | first_block = le32_to_cpu(chain[depth - 1].key); |
| 891 | clear_buffer_new(bh_result); |
| 892 | count++; |
| 893 | /*map more blocks*/ |
| 894 | while (count < maxblocks && count <= blocks_to_boundary) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 895 | ext4_fsblk_t blk; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 896 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 897 | blk = le32_to_cpu(*(chain[depth-1].p + count)); |
| 898 | |
| 899 | if (blk == first_block + count) |
| 900 | count++; |
| 901 | else |
| 902 | break; |
| 903 | } |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 904 | goto got_it; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /* Next simple case - plain lookup or failed read of indirect block */ |
| 908 | if (!create || err == -EIO) |
| 909 | goto cleanup; |
| 910 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 911 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 912 | * Okay, we need to do block allocation. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 913 | */ |
Akinobu Mita | fb01bfd | 2008-02-06 01:40:16 -0800 | [diff] [blame] | 914 | goal = ext4_find_goal(inode, iblock, partial); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 915 | |
| 916 | /* the number of blocks need to allocate for [d,t]indirect blocks */ |
| 917 | indirect_blks = (chain + depth) - partial - 1; |
| 918 | |
| 919 | /* |
| 920 | * Next look up the indirect map to count the totoal number of |
| 921 | * direct blocks to allocate for this branch. |
| 922 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 923 | count = ext4_blks_to_allocate(partial, indirect_blks, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 924 | maxblocks, blocks_to_boundary); |
| 925 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 926 | * Block out ext4_truncate while we alter the tree |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 927 | */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 928 | err = ext4_alloc_branch(handle, inode, iblock, indirect_blks, |
| 929 | &count, goal, |
| 930 | offsets + (partial - chain), partial); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 931 | |
| 932 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 933 | * The ext4_splice_branch call will free and forget any buffers |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 934 | * on the new chain if there is a failure, but that risks using |
| 935 | * up transaction credits, especially for bitmaps where the |
| 936 | * credits cannot be returned. Can we handle this somehow? We |
| 937 | * may need to return -EAGAIN upwards in the worst case. --sct |
| 938 | */ |
| 939 | if (!err) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 940 | err = ext4_splice_branch(handle, inode, iblock, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 941 | partial, indirect_blks, count); |
| 942 | /* |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 943 | * i_disksize growing is protected by i_data_sem. Don't forget to |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 944 | * protect it if you're about to implement concurrent |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 945 | * ext4_get_block() -bzzz |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 946 | */ |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 947 | if (!err && extend_disksize) { |
| 948 | disksize = ((loff_t) iblock + count) << inode->i_blkbits; |
| 949 | if (disksize > i_size_read(inode)) |
| 950 | disksize = i_size_read(inode); |
| 951 | if (disksize > ei->i_disksize) |
| 952 | ei->i_disksize = disksize; |
| 953 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 954 | if (err) |
| 955 | goto cleanup; |
| 956 | |
| 957 | set_buffer_new(bh_result); |
| 958 | got_it: |
| 959 | map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); |
| 960 | if (count > blocks_to_boundary) |
| 961 | set_buffer_boundary(bh_result); |
| 962 | err = count; |
| 963 | /* Clean up and exit */ |
| 964 | partial = chain + depth - 1; /* the whole chain */ |
| 965 | cleanup: |
| 966 | while (partial > chain) { |
| 967 | BUFFER_TRACE(partial->bh, "call brelse"); |
| 968 | brelse(partial->bh); |
| 969 | partial--; |
| 970 | } |
| 971 | BUFFER_TRACE(bh_result, "returned"); |
| 972 | out: |
| 973 | return err; |
| 974 | } |
| 975 | |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 976 | /* |
| 977 | * Calculate the number of metadata blocks need to reserve |
| 978 | * to allocate @blocks for non extent file based file |
| 979 | */ |
| 980 | static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks) |
| 981 | { |
| 982 | int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
| 983 | int ind_blks, dind_blks, tind_blks; |
| 984 | |
| 985 | /* number of new indirect blocks needed */ |
| 986 | ind_blks = (blocks + icap - 1) / icap; |
| 987 | |
| 988 | dind_blks = (ind_blks + icap - 1) / icap; |
| 989 | |
| 990 | tind_blks = 1; |
| 991 | |
| 992 | return ind_blks + dind_blks + tind_blks; |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | * Calculate the number of metadata blocks need to reserve |
| 997 | * to allocate given number of blocks |
| 998 | */ |
| 999 | static int ext4_calc_metadata_amount(struct inode *inode, int blocks) |
| 1000 | { |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1001 | if (!blocks) |
| 1002 | return 0; |
| 1003 | |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1004 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) |
| 1005 | return ext4_ext_calc_metadata_amount(inode, blocks); |
| 1006 | |
| 1007 | return ext4_indirect_calc_metadata_amount(inode, blocks); |
| 1008 | } |
| 1009 | |
| 1010 | static void ext4_da_update_reserve_space(struct inode *inode, int used) |
| 1011 | { |
| 1012 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1013 | int total, mdb, mdb_free; |
| 1014 | |
| 1015 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1016 | /* recalculate the number of metablocks still need to be reserved */ |
| 1017 | total = EXT4_I(inode)->i_reserved_data_blocks - used; |
| 1018 | mdb = ext4_calc_metadata_amount(inode, total); |
| 1019 | |
| 1020 | /* figure out how many metablocks to release */ |
| 1021 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); |
| 1022 | mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; |
| 1023 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 1024 | if (mdb_free) { |
| 1025 | /* Account for allocated meta_blocks */ |
| 1026 | mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks; |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1027 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 1028 | /* update fs dirty blocks counter */ |
| 1029 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free); |
| 1030 | EXT4_I(inode)->i_allocated_meta_blocks = 0; |
| 1031 | EXT4_I(inode)->i_reserved_meta_blocks = mdb; |
| 1032 | } |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1033 | |
| 1034 | /* update per-inode reservations */ |
| 1035 | BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks); |
| 1036 | EXT4_I(inode)->i_reserved_data_blocks -= used; |
| 1037 | |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1038 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1039 | } |
| 1040 | |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1041 | /* |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1042 | * The ext4_get_blocks_wrap() function try to look up the requested blocks, |
| 1043 | * and returns if the blocks are already mapped. |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1044 | * |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1045 | * Otherwise it takes the write lock of the i_data_sem and allocate blocks |
| 1046 | * and store the allocated blocks in the result buffer head and mark it |
| 1047 | * mapped. |
| 1048 | * |
| 1049 | * If file type is extents based, it will call ext4_ext_get_blocks(), |
| 1050 | * Otherwise, call with ext4_get_blocks_handle() to handle indirect mapping |
| 1051 | * based files |
| 1052 | * |
| 1053 | * On success, it returns the number of blocks being mapped or allocate. |
| 1054 | * if create==0 and the blocks are pre-allocated and uninitialized block, |
| 1055 | * the result buffer head is unmapped. If the create ==1, it will make sure |
| 1056 | * the buffer head is mapped. |
| 1057 | * |
| 1058 | * It returns 0 if plain look up failed (blocks have not been allocated), in |
| 1059 | * that casem, buffer head is unmapped |
| 1060 | * |
| 1061 | * It returns the error in case of allocation failure. |
| 1062 | */ |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1063 | int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block, |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1064 | unsigned int max_blocks, struct buffer_head *bh, |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1065 | int create, int extend_disksize, int flag) |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1066 | { |
| 1067 | int retval; |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1068 | |
| 1069 | clear_buffer_mapped(bh); |
| 1070 | |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1071 | /* |
| 1072 | * Try to see if we can get the block without requesting |
| 1073 | * for new file system block. |
| 1074 | */ |
| 1075 | down_read((&EXT4_I(inode)->i_data_sem)); |
| 1076 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { |
| 1077 | retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, |
| 1078 | bh, 0, 0); |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1079 | } else { |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1080 | retval = ext4_get_blocks_handle(handle, |
| 1081 | inode, block, max_blocks, bh, 0, 0); |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1082 | } |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1083 | up_read((&EXT4_I(inode)->i_data_sem)); |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1084 | |
| 1085 | /* If it is only a block(s) look up */ |
| 1086 | if (!create) |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1087 | return retval; |
| 1088 | |
| 1089 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1090 | * Returns if the blocks have already allocated |
| 1091 | * |
| 1092 | * Note that if blocks have been preallocated |
| 1093 | * ext4_ext_get_block() returns th create = 0 |
| 1094 | * with buffer head unmapped. |
| 1095 | */ |
| 1096 | if (retval > 0 && buffer_mapped(bh)) |
| 1097 | return retval; |
| 1098 | |
| 1099 | /* |
| 1100 | * New blocks allocate and/or writing to uninitialized extent |
| 1101 | * will possibly result in updating i_data, so we take |
| 1102 | * the write lock of i_data_sem, and call get_blocks() |
| 1103 | * with create == 1 flag. |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1104 | */ |
| 1105 | down_write((&EXT4_I(inode)->i_data_sem)); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1106 | |
| 1107 | /* |
| 1108 | * if the caller is from delayed allocation writeout path |
| 1109 | * we have already reserved fs blocks for allocation |
| 1110 | * let the underlying get_block() function know to |
| 1111 | * avoid double accounting |
| 1112 | */ |
| 1113 | if (flag) |
| 1114 | EXT4_I(inode)->i_delalloc_reserved_flag = 1; |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1115 | /* |
| 1116 | * We need to check for EXT4 here because migrate |
| 1117 | * could have changed the inode type in between |
| 1118 | */ |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1119 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { |
| 1120 | retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, |
| 1121 | bh, create, extend_disksize); |
| 1122 | } else { |
| 1123 | retval = ext4_get_blocks_handle(handle, inode, block, |
| 1124 | max_blocks, bh, create, extend_disksize); |
Aneesh Kumar K.V | 267e4db | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 1125 | |
| 1126 | if (retval > 0 && buffer_new(bh)) { |
| 1127 | /* |
| 1128 | * We allocated new blocks which will result in |
| 1129 | * i_data's format changing. Force the migrate |
| 1130 | * to fail by clearing migrate flags |
| 1131 | */ |
| 1132 | EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags & |
| 1133 | ~EXT4_EXT_MIGRATE; |
| 1134 | } |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1135 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1136 | |
| 1137 | if (flag) { |
| 1138 | EXT4_I(inode)->i_delalloc_reserved_flag = 0; |
| 1139 | /* |
| 1140 | * Update reserved blocks/metadata blocks |
| 1141 | * after successful block allocation |
| 1142 | * which were deferred till now |
| 1143 | */ |
| 1144 | if ((retval > 0) && buffer_delay(bh)) |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1145 | ext4_da_update_reserve_space(inode, retval); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1146 | } |
| 1147 | |
Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1148 | up_write((&EXT4_I(inode)->i_data_sem)); |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1149 | return retval; |
| 1150 | } |
| 1151 | |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 1152 | /* Maximum number of blocks we map for direct IO at once. */ |
| 1153 | #define DIO_MAX_BLOCKS 4096 |
| 1154 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1155 | int ext4_get_block(struct inode *inode, sector_t iblock, |
| 1156 | struct buffer_head *bh_result, int create) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1157 | { |
Dmitriy Monakhov | 3e4fdaf | 2007-02-10 01:46:35 -0800 | [diff] [blame] | 1158 | handle_t *handle = ext4_journal_current_handle(); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1159 | int ret = 0, started = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1160 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 1161 | int dio_credits; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1162 | |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1163 | if (create && !handle) { |
| 1164 | /* Direct IO write... */ |
| 1165 | if (max_blocks > DIO_MAX_BLOCKS) |
| 1166 | max_blocks = DIO_MAX_BLOCKS; |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 1167 | dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 1168 | handle = ext4_journal_start(inode, dio_credits); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1169 | if (IS_ERR(handle)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1170 | ret = PTR_ERR(handle); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1171 | goto out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1172 | } |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1173 | started = 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1176 | ret = ext4_get_blocks_wrap(handle, inode, iblock, |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1177 | max_blocks, bh_result, create, 0, 0); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1178 | if (ret > 0) { |
| 1179 | bh_result->b_size = (ret << inode->i_blkbits); |
| 1180 | ret = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1181 | } |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1182 | if (started) |
| 1183 | ext4_journal_stop(handle); |
| 1184 | out: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1185 | return ret; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * `handle' can be NULL if create is zero |
| 1190 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1191 | 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] | 1192 | ext4_lblk_t block, int create, int *errp) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1193 | { |
| 1194 | struct buffer_head dummy; |
| 1195 | int fatal = 0, err; |
| 1196 | |
| 1197 | J_ASSERT(handle != NULL || create == 0); |
| 1198 | |
| 1199 | dummy.b_state = 0; |
| 1200 | dummy.b_blocknr = -1000; |
| 1201 | buffer_trace_init(&dummy.b_history); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1202 | err = ext4_get_blocks_wrap(handle, inode, block, 1, |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1203 | &dummy, create, 1, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1204 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1205 | * ext4_get_blocks_handle() returns number of blocks |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1206 | * mapped. 0 in case of a HOLE. |
| 1207 | */ |
| 1208 | if (err > 0) { |
| 1209 | if (err > 1) |
| 1210 | WARN_ON(1); |
| 1211 | err = 0; |
| 1212 | } |
| 1213 | *errp = err; |
| 1214 | if (!err && buffer_mapped(&dummy)) { |
| 1215 | struct buffer_head *bh; |
| 1216 | bh = sb_getblk(inode->i_sb, dummy.b_blocknr); |
| 1217 | if (!bh) { |
| 1218 | *errp = -EIO; |
| 1219 | goto err; |
| 1220 | } |
| 1221 | if (buffer_new(&dummy)) { |
| 1222 | J_ASSERT(create != 0); |
Aneesh Kumar K.V | ac39849 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1223 | J_ASSERT(handle != NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1224 | |
| 1225 | /* |
| 1226 | * Now that we do not always journal data, we should |
| 1227 | * keep in mind whether this should always journal the |
| 1228 | * new buffer as metadata. For now, regular file |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1229 | * writes use ext4_get_block instead, so it's not a |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1230 | * problem. |
| 1231 | */ |
| 1232 | lock_buffer(bh); |
| 1233 | BUFFER_TRACE(bh, "call get_create_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1234 | fatal = ext4_journal_get_create_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1235 | if (!fatal && !buffer_uptodate(bh)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1236 | memset(bh->b_data, 0, inode->i_sb->s_blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1237 | set_buffer_uptodate(bh); |
| 1238 | } |
| 1239 | unlock_buffer(bh); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1240 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
| 1241 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1242 | if (!fatal) |
| 1243 | fatal = err; |
| 1244 | } else { |
| 1245 | BUFFER_TRACE(bh, "not a new buffer"); |
| 1246 | } |
| 1247 | if (fatal) { |
| 1248 | *errp = fatal; |
| 1249 | brelse(bh); |
| 1250 | bh = NULL; |
| 1251 | } |
| 1252 | return bh; |
| 1253 | } |
| 1254 | err: |
| 1255 | return NULL; |
| 1256 | } |
| 1257 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1258 | 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] | 1259 | ext4_lblk_t block, int create, int *err) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1260 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1261 | struct buffer_head *bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1262 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1263 | bh = ext4_getblk(handle, inode, block, create, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1264 | if (!bh) |
| 1265 | return bh; |
| 1266 | if (buffer_uptodate(bh)) |
| 1267 | return bh; |
| 1268 | ll_rw_block(READ_META, 1, &bh); |
| 1269 | wait_on_buffer(bh); |
| 1270 | if (buffer_uptodate(bh)) |
| 1271 | return bh; |
| 1272 | put_bh(bh); |
| 1273 | *err = -EIO; |
| 1274 | return NULL; |
| 1275 | } |
| 1276 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1277 | static int walk_page_buffers(handle_t *handle, |
| 1278 | struct buffer_head *head, |
| 1279 | unsigned from, |
| 1280 | unsigned to, |
| 1281 | int *partial, |
| 1282 | int (*fn)(handle_t *handle, |
| 1283 | struct buffer_head *bh)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1284 | { |
| 1285 | struct buffer_head *bh; |
| 1286 | unsigned block_start, block_end; |
| 1287 | unsigned blocksize = head->b_size; |
| 1288 | int err, ret = 0; |
| 1289 | struct buffer_head *next; |
| 1290 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1291 | for (bh = head, block_start = 0; |
| 1292 | ret == 0 && (bh != head || !block_start); |
| 1293 | block_start = block_end, bh = next) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1294 | { |
| 1295 | next = bh->b_this_page; |
| 1296 | block_end = block_start + blocksize; |
| 1297 | if (block_end <= from || block_start >= to) { |
| 1298 | if (partial && !buffer_uptodate(bh)) |
| 1299 | *partial = 1; |
| 1300 | continue; |
| 1301 | } |
| 1302 | err = (*fn)(handle, bh); |
| 1303 | if (!ret) |
| 1304 | ret = err; |
| 1305 | } |
| 1306 | return ret; |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * To preserve ordering, it is essential that the hole instantiation and |
| 1311 | * the data write be encapsulated in a single transaction. We cannot |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1312 | * 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] | 1313 | * 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] | 1314 | * prepare_write() is the right place. |
| 1315 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1316 | * Also, this function can nest inside ext4_writepage() -> |
| 1317 | * block_write_full_page(). In that case, we *know* that ext4_writepage() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1318 | * has generated enough buffer credits to do the whole page. So we won't |
| 1319 | * block on the journal in that case, which is good, because the caller may |
| 1320 | * be PF_MEMALLOC. |
| 1321 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1322 | * By accident, ext4 can be reentered when a transaction is open via |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1323 | * quota file writes. If we were to commit the transaction while thus |
| 1324 | * reentered, there can be a deadlock - we would be holding a quota |
| 1325 | * lock, and the commit would never complete if another thread had a |
| 1326 | * transaction open and was blocking on the quota lock - a ranking |
| 1327 | * violation. |
| 1328 | * |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 1329 | * 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] | 1330 | * will _not_ run commit under these circumstances because handle->h_ref |
| 1331 | * is elevated. We'll still have enough credits for the tiny quotafile |
| 1332 | * write. |
| 1333 | */ |
| 1334 | static int do_journal_get_write_access(handle_t *handle, |
| 1335 | struct buffer_head *bh) |
| 1336 | { |
| 1337 | if (!buffer_mapped(bh) || buffer_freed(bh)) |
| 1338 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1339 | return ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1342 | static int ext4_write_begin(struct file *file, struct address_space *mapping, |
| 1343 | loff_t pos, unsigned len, unsigned flags, |
| 1344 | struct page **pagep, void **fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1345 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1346 | struct inode *inode = mapping->host; |
Andrew Morton | 7479d2b | 2007-04-01 23:49:44 -0700 | [diff] [blame] | 1347 | int ret, needed_blocks = ext4_writepage_trans_blocks(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1348 | handle_t *handle; |
| 1349 | int retries = 0; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1350 | struct page *page; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1351 | pgoff_t index; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1352 | unsigned from, to; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1353 | |
| 1354 | index = pos >> PAGE_CACHE_SHIFT; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1355 | from = pos & (PAGE_CACHE_SIZE - 1); |
| 1356 | to = from + len; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1357 | |
| 1358 | retry: |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1359 | handle = ext4_journal_start(inode, needed_blocks); |
| 1360 | if (IS_ERR(handle)) { |
| 1361 | ret = PTR_ERR(handle); |
| 1362 | goto out; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 1365 | page = grab_cache_page_write_begin(mapping, index, flags); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1366 | if (!page) { |
| 1367 | ext4_journal_stop(handle); |
| 1368 | ret = -ENOMEM; |
| 1369 | goto out; |
| 1370 | } |
| 1371 | *pagep = page; |
| 1372 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1373 | ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
| 1374 | ext4_get_block); |
| 1375 | |
| 1376 | if (!ret && ext4_should_journal_data(inode)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1377 | ret = walk_page_buffers(handle, page_buffers(page), |
| 1378 | from, to, NULL, do_journal_get_write_access); |
| 1379 | } |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1380 | |
| 1381 | if (ret) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1382 | unlock_page(page); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1383 | ext4_journal_stop(handle); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1384 | page_cache_release(page); |
Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 1385 | /* |
| 1386 | * block_write_begin may have instantiated a few blocks |
| 1387 | * outside i_size. Trim these off again. Don't need |
| 1388 | * i_size_read because we hold i_mutex. |
| 1389 | */ |
| 1390 | if (pos + len > inode->i_size) |
| 1391 | vmtruncate(inode, inode->i_size); |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1394 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1395 | goto retry; |
Andrew Morton | 7479d2b | 2007-04-01 23:49:44 -0700 | [diff] [blame] | 1396 | out: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1397 | return ret; |
| 1398 | } |
| 1399 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1400 | /* For write_end() in data=journal mode */ |
| 1401 | static int write_end_fn(handle_t *handle, struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1402 | { |
| 1403 | if (!buffer_mapped(bh) || buffer_freed(bh)) |
| 1404 | return 0; |
| 1405 | set_buffer_uptodate(bh); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1406 | return ext4_handle_dirty_metadata(handle, NULL, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * We need to pick up the new inode size which generic_commit_write gave us |
| 1411 | * `file' can be NULL - eg, when called from page_symlink(). |
| 1412 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1413 | * ext4 never places buffers on inode->i_mapping->private_list. metadata |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1414 | * buffers are managed internally. |
| 1415 | */ |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1416 | static int ext4_ordered_write_end(struct file *file, |
| 1417 | struct address_space *mapping, |
| 1418 | loff_t pos, unsigned len, unsigned copied, |
| 1419 | struct page *page, void *fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1420 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1421 | handle_t *handle = ext4_journal_current_handle(); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1422 | struct inode *inode = mapping->host; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1423 | int ret = 0, ret2; |
| 1424 | |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1425 | ret = ext4_jbd2_file_inode(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1426 | |
| 1427 | if (ret == 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1428 | loff_t new_i_size; |
| 1429 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1430 | new_i_size = pos + copied; |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1431 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 1432 | ext4_update_i_disksize(inode, new_i_size); |
| 1433 | /* We need to mark inode dirty even if |
| 1434 | * new_i_size is less that inode->i_size |
| 1435 | * bu greater than i_disksize.(hint delalloc) |
| 1436 | */ |
| 1437 | ext4_mark_inode_dirty(handle, inode); |
| 1438 | } |
| 1439 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1440 | ret2 = generic_write_end(file, mapping, pos, len, copied, |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1441 | page, fsdata); |
Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 1442 | copied = ret2; |
| 1443 | if (ret2 < 0) |
| 1444 | ret = ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1445 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1446 | ret2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1447 | if (!ret) |
| 1448 | ret = ret2; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1449 | |
| 1450 | return ret ? ret : copied; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1453 | static int ext4_writeback_write_end(struct file *file, |
| 1454 | struct address_space *mapping, |
| 1455 | loff_t pos, unsigned len, unsigned copied, |
| 1456 | struct page *page, void *fsdata) |
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 | handle_t *handle = ext4_journal_current_handle(); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1459 | struct inode *inode = mapping->host; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1460 | int ret = 0, ret2; |
| 1461 | loff_t new_i_size; |
| 1462 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1463 | new_i_size = pos + copied; |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1464 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 1465 | ext4_update_i_disksize(inode, new_i_size); |
| 1466 | /* We need to mark inode dirty even if |
| 1467 | * new_i_size is less that inode->i_size |
| 1468 | * bu greater than i_disksize.(hint delalloc) |
| 1469 | */ |
| 1470 | ext4_mark_inode_dirty(handle, inode); |
| 1471 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1472 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1473 | ret2 = generic_write_end(file, mapping, pos, len, copied, |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1474 | page, fsdata); |
Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 1475 | copied = ret2; |
| 1476 | if (ret2 < 0) |
| 1477 | ret = ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1478 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1479 | ret2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1480 | if (!ret) |
| 1481 | ret = ret2; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1482 | |
| 1483 | return ret ? ret : copied; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1486 | static int ext4_journalled_write_end(struct file *file, |
| 1487 | struct address_space *mapping, |
| 1488 | loff_t pos, unsigned len, unsigned copied, |
| 1489 | struct page *page, void *fsdata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1490 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1491 | handle_t *handle = ext4_journal_current_handle(); |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1492 | struct inode *inode = mapping->host; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1493 | int ret = 0, ret2; |
| 1494 | int partial = 0; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1495 | unsigned from, to; |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1496 | loff_t new_i_size; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1497 | |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1498 | from = pos & (PAGE_CACHE_SIZE - 1); |
| 1499 | to = from + len; |
| 1500 | |
| 1501 | if (copied < len) { |
| 1502 | if (!PageUptodate(page)) |
| 1503 | copied = 0; |
| 1504 | page_zero_new_buffers(page, from+copied, to); |
| 1505 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1506 | |
| 1507 | ret = walk_page_buffers(handle, page_buffers(page), from, |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1508 | to, &partial, write_end_fn); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1509 | if (!partial) |
| 1510 | SetPageUptodate(page); |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1511 | new_i_size = pos + copied; |
| 1512 | if (new_i_size > inode->i_size) |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1513 | i_size_write(inode, pos+copied); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1514 | EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1515 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 1516 | ext4_update_i_disksize(inode, new_i_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1517 | ret2 = ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1518 | if (!ret) |
| 1519 | ret = ret2; |
| 1520 | } |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1521 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1522 | unlock_page(page); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1523 | ret2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1524 | if (!ret) |
| 1525 | ret = ret2; |
Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1526 | page_cache_release(page); |
| 1527 | |
| 1528 | return ret ? ret : copied; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1529 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1530 | |
| 1531 | static int ext4_da_reserve_space(struct inode *inode, int nrblocks) |
| 1532 | { |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1533 | int retries = 0; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1534 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1535 | unsigned long md_needed, mdblocks, total = 0; |
| 1536 | |
| 1537 | /* |
| 1538 | * recalculate the amount of metadata blocks to reserve |
| 1539 | * in order to allocate nrblocks |
| 1540 | * worse case is one extent per block |
| 1541 | */ |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1542 | repeat: |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1543 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1544 | total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks; |
| 1545 | mdblocks = ext4_calc_metadata_amount(inode, total); |
| 1546 | BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks); |
| 1547 | |
| 1548 | md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks; |
| 1549 | total = md_needed + nrblocks; |
| 1550 | |
Aneesh Kumar K.V | a30d542a | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 1551 | if (ext4_claim_free_blocks(sbi, total)) { |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1552 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1553 | if (ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 1554 | yield(); |
| 1555 | goto repeat; |
| 1556 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1557 | return -ENOSPC; |
| 1558 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1559 | EXT4_I(inode)->i_reserved_data_blocks += nrblocks; |
| 1560 | EXT4_I(inode)->i_reserved_meta_blocks = mdblocks; |
| 1561 | |
| 1562 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1563 | return 0; /* success */ |
| 1564 | } |
| 1565 | |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1566 | static void ext4_da_release_space(struct inode *inode, int to_free) |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1567 | { |
| 1568 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1569 | int total, mdb, mdb_free, release; |
| 1570 | |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1571 | if (!to_free) |
| 1572 | return; /* Nothing to release, exit */ |
| 1573 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1574 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1575 | |
| 1576 | if (!EXT4_I(inode)->i_reserved_data_blocks) { |
| 1577 | /* |
| 1578 | * if there is no reserved blocks, but we try to free some |
| 1579 | * then the counter is messed up somewhere. |
| 1580 | * but since this function is called from invalidate |
| 1581 | * page, it's harmless to return without any action |
| 1582 | */ |
| 1583 | printk(KERN_INFO "ext4 delalloc try to release %d reserved " |
| 1584 | "blocks for inode %lu, but there is no reserved " |
| 1585 | "data blocks\n", to_free, inode->i_ino); |
| 1586 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1587 | return; |
| 1588 | } |
| 1589 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1590 | /* recalculate the number of metablocks still need to be reserved */ |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1591 | total = EXT4_I(inode)->i_reserved_data_blocks - to_free; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1592 | mdb = ext4_calc_metadata_amount(inode, total); |
| 1593 | |
| 1594 | /* figure out how many metablocks to release */ |
| 1595 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); |
| 1596 | mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; |
| 1597 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1598 | release = to_free + mdb_free; |
| 1599 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 1600 | /* update fs dirty blocks counter for truncate case */ |
| 1601 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, release); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1602 | |
| 1603 | /* update per-inode reservations */ |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1604 | BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks); |
| 1605 | EXT4_I(inode)->i_reserved_data_blocks -= to_free; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1606 | |
| 1607 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); |
| 1608 | EXT4_I(inode)->i_reserved_meta_blocks = mdb; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1609 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1610 | } |
| 1611 | |
| 1612 | static void ext4_da_page_release_reservation(struct page *page, |
| 1613 | unsigned long offset) |
| 1614 | { |
| 1615 | int to_release = 0; |
| 1616 | struct buffer_head *head, *bh; |
| 1617 | unsigned int curr_off = 0; |
| 1618 | |
| 1619 | head = page_buffers(page); |
| 1620 | bh = head; |
| 1621 | do { |
| 1622 | unsigned int next_off = curr_off + bh->b_size; |
| 1623 | |
| 1624 | if ((offset <= curr_off) && (buffer_delay(bh))) { |
| 1625 | to_release++; |
| 1626 | clear_buffer_delay(bh); |
| 1627 | } |
| 1628 | curr_off = next_off; |
| 1629 | } while ((bh = bh->b_this_page) != head); |
Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1630 | ext4_da_release_space(page->mapping->host, to_release); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1631 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1632 | |
| 1633 | /* |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1634 | * Delayed allocation stuff |
| 1635 | */ |
| 1636 | |
| 1637 | struct mpage_da_data { |
| 1638 | struct inode *inode; |
| 1639 | struct buffer_head lbh; /* extent of blocks */ |
| 1640 | unsigned long first_page, next_page; /* extent of pages */ |
| 1641 | get_block_t *get_block; |
| 1642 | struct writeback_control *wbc; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1643 | int io_done; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1644 | int pages_written; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1645 | int retval; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1646 | }; |
| 1647 | |
| 1648 | /* |
| 1649 | * 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] | 1650 | * them with writepage() call back |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1651 | * |
| 1652 | * @mpd->inode: inode |
| 1653 | * @mpd->first_page: first page of the extent |
| 1654 | * @mpd->next_page: page after the last page of the extent |
| 1655 | * @mpd->get_block: the filesystem's block mapper function |
| 1656 | * |
| 1657 | * By the time mpage_da_submit_io() is called we expect all blocks |
| 1658 | * to be allocated. this may be wrong if allocation failed. |
| 1659 | * |
| 1660 | * As pages are already locked by write_cache_pages(), we can't use it |
| 1661 | */ |
| 1662 | static int mpage_da_submit_io(struct mpage_da_data *mpd) |
| 1663 | { |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 1664 | long pages_skipped; |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1665 | struct pagevec pvec; |
| 1666 | unsigned long index, end; |
| 1667 | int ret = 0, err, nr_pages, i; |
| 1668 | struct inode *inode = mpd->inode; |
| 1669 | struct address_space *mapping = inode->i_mapping; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1670 | |
| 1671 | BUG_ON(mpd->next_page <= mpd->first_page); |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1672 | /* |
| 1673 | * We need to start from the first_page to the next_page - 1 |
| 1674 | * to make sure we also write the mapped dirty buffer_heads. |
| 1675 | * If we look at mpd->lbh.b_blocknr we would only be looking |
| 1676 | * at the currently mapped buffer_heads. |
| 1677 | */ |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1678 | index = mpd->first_page; |
| 1679 | end = mpd->next_page - 1; |
| 1680 | |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1681 | pagevec_init(&pvec, 0); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1682 | while (index <= end) { |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1683 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1684 | if (nr_pages == 0) |
| 1685 | break; |
| 1686 | for (i = 0; i < nr_pages; i++) { |
| 1687 | struct page *page = pvec.pages[i]; |
| 1688 | |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1689 | index = page->index; |
| 1690 | if (index > end) |
| 1691 | break; |
| 1692 | index++; |
| 1693 | |
| 1694 | BUG_ON(!PageLocked(page)); |
| 1695 | BUG_ON(PageWriteback(page)); |
| 1696 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 1697 | pages_skipped = mpd->wbc->pages_skipped; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1698 | err = mapping->a_ops->writepage(page, mpd->wbc); |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 1699 | if (!err && (pages_skipped == mpd->wbc->pages_skipped)) |
| 1700 | /* |
| 1701 | * have successfully written the page |
| 1702 | * without skipping the same |
| 1703 | */ |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1704 | mpd->pages_written++; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1705 | /* |
| 1706 | * In error case, we have to continue because |
| 1707 | * remaining pages are still locked |
| 1708 | * XXX: unlock and re-dirty them? |
| 1709 | */ |
| 1710 | if (ret == 0) |
| 1711 | ret = err; |
| 1712 | } |
| 1713 | pagevec_release(&pvec); |
| 1714 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1715 | return ret; |
| 1716 | } |
| 1717 | |
| 1718 | /* |
| 1719 | * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers |
| 1720 | * |
| 1721 | * @mpd->inode - inode to walk through |
| 1722 | * @exbh->b_blocknr - first block on a disk |
| 1723 | * @exbh->b_size - amount of space in bytes |
| 1724 | * @logical - first logical block to start assignment with |
| 1725 | * |
| 1726 | * the function goes through all passed space and put actual disk |
| 1727 | * block numbers into buffer heads, dropping BH_Delay |
| 1728 | */ |
| 1729 | static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical, |
| 1730 | struct buffer_head *exbh) |
| 1731 | { |
| 1732 | struct inode *inode = mpd->inode; |
| 1733 | struct address_space *mapping = inode->i_mapping; |
| 1734 | int blocks = exbh->b_size >> inode->i_blkbits; |
| 1735 | sector_t pblock = exbh->b_blocknr, cur_logical; |
| 1736 | struct buffer_head *head, *bh; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1737 | pgoff_t index, end; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1738 | struct pagevec pvec; |
| 1739 | int nr_pages, i; |
| 1740 | |
| 1741 | index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1742 | end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1743 | cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1744 | |
| 1745 | pagevec_init(&pvec, 0); |
| 1746 | |
| 1747 | while (index <= end) { |
| 1748 | /* XXX: optimize tail */ |
| 1749 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); |
| 1750 | if (nr_pages == 0) |
| 1751 | break; |
| 1752 | for (i = 0; i < nr_pages; i++) { |
| 1753 | struct page *page = pvec.pages[i]; |
| 1754 | |
| 1755 | index = page->index; |
| 1756 | if (index > end) |
| 1757 | break; |
| 1758 | index++; |
| 1759 | |
| 1760 | BUG_ON(!PageLocked(page)); |
| 1761 | BUG_ON(PageWriteback(page)); |
| 1762 | BUG_ON(!page_has_buffers(page)); |
| 1763 | |
| 1764 | bh = page_buffers(page); |
| 1765 | head = bh; |
| 1766 | |
| 1767 | /* skip blocks out of the range */ |
| 1768 | do { |
| 1769 | if (cur_logical >= logical) |
| 1770 | break; |
| 1771 | cur_logical++; |
| 1772 | } while ((bh = bh->b_this_page) != head); |
| 1773 | |
| 1774 | do { |
| 1775 | if (cur_logical >= logical + blocks) |
| 1776 | break; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1777 | if (buffer_delay(bh)) { |
| 1778 | bh->b_blocknr = pblock; |
| 1779 | clear_buffer_delay(bh); |
Aneesh Kumar K.V | bf068ee | 2008-08-19 22:16:43 -0400 | [diff] [blame] | 1780 | bh->b_bdev = inode->i_sb->s_bdev; |
| 1781 | } else if (buffer_unwritten(bh)) { |
| 1782 | bh->b_blocknr = pblock; |
| 1783 | clear_buffer_unwritten(bh); |
| 1784 | set_buffer_mapped(bh); |
| 1785 | set_buffer_new(bh); |
| 1786 | bh->b_bdev = inode->i_sb->s_bdev; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1787 | } else if (buffer_mapped(bh)) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1788 | BUG_ON(bh->b_blocknr != pblock); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1789 | |
| 1790 | cur_logical++; |
| 1791 | pblock++; |
| 1792 | } while ((bh = bh->b_this_page) != head); |
| 1793 | } |
| 1794 | pagevec_release(&pvec); |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | |
| 1799 | /* |
| 1800 | * __unmap_underlying_blocks - just a helper function to unmap |
| 1801 | * set of blocks described by @bh |
| 1802 | */ |
| 1803 | static inline void __unmap_underlying_blocks(struct inode *inode, |
| 1804 | struct buffer_head *bh) |
| 1805 | { |
| 1806 | struct block_device *bdev = inode->i_sb->s_bdev; |
| 1807 | int blocks, i; |
| 1808 | |
| 1809 | blocks = bh->b_size >> inode->i_blkbits; |
| 1810 | for (i = 0; i < blocks; i++) |
| 1811 | unmap_underlying_metadata(bdev, bh->b_blocknr + i); |
| 1812 | } |
| 1813 | |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1814 | static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd, |
| 1815 | sector_t logical, long blk_cnt) |
| 1816 | { |
| 1817 | int nr_pages, i; |
| 1818 | pgoff_t index, end; |
| 1819 | struct pagevec pvec; |
| 1820 | struct inode *inode = mpd->inode; |
| 1821 | struct address_space *mapping = inode->i_mapping; |
| 1822 | |
| 1823 | index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1824 | end = (logical + blk_cnt - 1) >> |
| 1825 | (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1826 | while (index <= end) { |
| 1827 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); |
| 1828 | if (nr_pages == 0) |
| 1829 | break; |
| 1830 | for (i = 0; i < nr_pages; i++) { |
| 1831 | struct page *page = pvec.pages[i]; |
| 1832 | index = page->index; |
| 1833 | if (index > end) |
| 1834 | break; |
| 1835 | index++; |
| 1836 | |
| 1837 | BUG_ON(!PageLocked(page)); |
| 1838 | BUG_ON(PageWriteback(page)); |
| 1839 | block_invalidatepage(page, 0); |
| 1840 | ClearPageUptodate(page); |
| 1841 | unlock_page(page); |
| 1842 | } |
| 1843 | } |
| 1844 | return; |
| 1845 | } |
| 1846 | |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1847 | static void ext4_print_free_blocks(struct inode *inode) |
| 1848 | { |
| 1849 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1850 | printk(KERN_EMERG "Total free blocks count %lld\n", |
| 1851 | ext4_count_free_blocks(inode->i_sb)); |
| 1852 | printk(KERN_EMERG "Free/Dirty block details\n"); |
| 1853 | printk(KERN_EMERG "free_blocks=%lld\n", |
Alexander Beregalov | 8f72fbd | 2008-10-29 17:13:08 -0400 | [diff] [blame] | 1854 | (long long)percpu_counter_sum(&sbi->s_freeblocks_counter)); |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1855 | printk(KERN_EMERG "dirty_blocks=%lld\n", |
Alexander Beregalov | 8f72fbd | 2008-10-29 17:13:08 -0400 | [diff] [blame] | 1856 | (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter)); |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1857 | printk(KERN_EMERG "Block reservation details\n"); |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1858 | printk(KERN_EMERG "i_reserved_data_blocks=%u\n", |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1859 | EXT4_I(inode)->i_reserved_data_blocks); |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1860 | printk(KERN_EMERG "i_reserved_meta_blocks=%u\n", |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1861 | EXT4_I(inode)->i_reserved_meta_blocks); |
| 1862 | return; |
| 1863 | } |
| 1864 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1865 | /* |
| 1866 | * mpage_da_map_blocks - go through given space |
| 1867 | * |
| 1868 | * @mpd->lbh - bh describing space |
| 1869 | * @mpd->get_block - the filesystem's block mapper function |
| 1870 | * |
| 1871 | * The function skips space we know is already mapped to disk blocks. |
| 1872 | * |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1873 | */ |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1874 | static int mpage_da_map_blocks(struct mpage_da_data *mpd) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1875 | { |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1876 | int err = 0; |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1877 | struct buffer_head new; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1878 | struct buffer_head *lbh = &mpd->lbh; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1879 | sector_t next; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1880 | |
| 1881 | /* |
| 1882 | * We consider only non-mapped and non-allocated blocks |
| 1883 | */ |
| 1884 | if (buffer_mapped(lbh) && !buffer_delay(lbh)) |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1885 | return 0; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1886 | new.b_state = lbh->b_state; |
| 1887 | new.b_blocknr = 0; |
| 1888 | new.b_size = lbh->b_size; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1889 | next = lbh->b_blocknr; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1890 | /* |
| 1891 | * If we didn't accumulate anything |
| 1892 | * to write simply return |
| 1893 | */ |
| 1894 | if (!new.b_size) |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1895 | return 0; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1896 | err = mpd->get_block(mpd->inode, next, &new, 1); |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1897 | if (err) { |
| 1898 | |
| 1899 | /* If get block returns with error |
| 1900 | * we simply return. Later writepage |
| 1901 | * will redirty the page and writepages |
| 1902 | * will find the dirty page again |
| 1903 | */ |
| 1904 | if (err == -EAGAIN) |
| 1905 | return 0; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1906 | |
| 1907 | if (err == -ENOSPC && |
| 1908 | ext4_count_free_blocks(mpd->inode->i_sb)) { |
| 1909 | mpd->retval = err; |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1913 | /* |
| 1914 | * get block failure will cause us |
| 1915 | * to loop in writepages. Because |
| 1916 | * a_ops->writepage won't be able to |
| 1917 | * make progress. The page will be redirtied |
| 1918 | * by writepage and writepages will again |
| 1919 | * try to write the same. |
| 1920 | */ |
| 1921 | printk(KERN_EMERG "%s block allocation failed for inode %lu " |
| 1922 | "at logical offset %llu with max blocks " |
| 1923 | "%zd with error %d\n", |
| 1924 | __func__, mpd->inode->i_ino, |
| 1925 | (unsigned long long)next, |
| 1926 | lbh->b_size >> mpd->inode->i_blkbits, err); |
| 1927 | printk(KERN_EMERG "This should not happen.!! " |
| 1928 | "Data will be lost\n"); |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1929 | if (err == -ENOSPC) { |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1930 | ext4_print_free_blocks(mpd->inode); |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1931 | } |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1932 | /* invlaidate all the pages */ |
| 1933 | ext4_da_block_invalidatepages(mpd, next, |
| 1934 | lbh->b_size >> mpd->inode->i_blkbits); |
| 1935 | return err; |
| 1936 | } |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1937 | BUG_ON(new.b_size == 0); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1938 | |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1939 | if (buffer_new(&new)) |
| 1940 | __unmap_underlying_blocks(mpd->inode, &new); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1941 | |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1942 | /* |
| 1943 | * If blocks are delayed marked, we need to |
| 1944 | * put actual blocknr and drop delayed bit |
| 1945 | */ |
| 1946 | if (buffer_delay(lbh) || buffer_unwritten(lbh)) |
| 1947 | mpage_put_bnr_to_bhs(mpd, next, &new); |
| 1948 | |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 1949 | return 0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1950 | } |
| 1951 | |
Aneesh Kumar K.V | bf068ee | 2008-08-19 22:16:43 -0400 | [diff] [blame] | 1952 | #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ |
| 1953 | (1 << BH_Delay) | (1 << BH_Unwritten)) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1954 | |
| 1955 | /* |
| 1956 | * mpage_add_bh_to_extent - try to add one more block to extent of blocks |
| 1957 | * |
| 1958 | * @mpd->lbh - extent of blocks |
| 1959 | * @logical - logical number of the block in the file |
| 1960 | * @bh - bh of the block (used to access block's state) |
| 1961 | * |
| 1962 | * the function is used to collect contig. blocks in same state |
| 1963 | */ |
| 1964 | static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, |
| 1965 | sector_t logical, struct buffer_head *bh) |
| 1966 | { |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1967 | sector_t next; |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1968 | size_t b_size = bh->b_size; |
| 1969 | struct buffer_head *lbh = &mpd->lbh; |
| 1970 | int nrblocks = lbh->b_size >> mpd->inode->i_blkbits; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1971 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1972 | /* check if thereserved journal credits might overflow */ |
| 1973 | if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) { |
| 1974 | if (nrblocks >= EXT4_MAX_TRANS_DATA) { |
| 1975 | /* |
| 1976 | * With non-extent format we are limited by the journal |
| 1977 | * credit available. Total credit needed to insert |
| 1978 | * nrblocks contiguous blocks is dependent on the |
| 1979 | * nrblocks. So limit nrblocks. |
| 1980 | */ |
| 1981 | goto flush_it; |
| 1982 | } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > |
| 1983 | EXT4_MAX_TRANS_DATA) { |
| 1984 | /* |
| 1985 | * Adding the new buffer_head would make it cross the |
| 1986 | * allowed limit for which we have journal credit |
| 1987 | * reserved. So limit the new bh->b_size |
| 1988 | */ |
| 1989 | b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << |
| 1990 | mpd->inode->i_blkbits; |
| 1991 | /* we will do mpage_da_submit_io in the next loop */ |
| 1992 | } |
| 1993 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1994 | /* |
| 1995 | * First block in the extent |
| 1996 | */ |
| 1997 | if (lbh->b_size == 0) { |
| 1998 | lbh->b_blocknr = logical; |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1999 | lbh->b_size = b_size; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2000 | lbh->b_state = bh->b_state & BH_FLAGS; |
| 2001 | return; |
| 2002 | } |
| 2003 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2004 | next = lbh->b_blocknr + nrblocks; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2005 | /* |
| 2006 | * Can we merge the block to our big extent? |
| 2007 | */ |
| 2008 | if (logical == next && (bh->b_state & BH_FLAGS) == lbh->b_state) { |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2009 | lbh->b_size += b_size; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2010 | return; |
| 2011 | } |
| 2012 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2013 | flush_it: |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2014 | /* |
| 2015 | * We couldn't merge the block to our extent, so we |
| 2016 | * need to flush current extent and start new one |
| 2017 | */ |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2018 | if (mpage_da_map_blocks(mpd) == 0) |
| 2019 | mpage_da_submit_io(mpd); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2020 | mpd->io_done = 1; |
| 2021 | return; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /* |
| 2025 | * __mpage_da_writepage - finds extent of pages and blocks |
| 2026 | * |
| 2027 | * @page: page to consider |
| 2028 | * @wbc: not used, we just follow rules |
| 2029 | * @data: context |
| 2030 | * |
| 2031 | * The function finds extents of pages and scan them for all blocks. |
| 2032 | */ |
| 2033 | static int __mpage_da_writepage(struct page *page, |
| 2034 | struct writeback_control *wbc, void *data) |
| 2035 | { |
| 2036 | struct mpage_da_data *mpd = data; |
| 2037 | struct inode *inode = mpd->inode; |
| 2038 | struct buffer_head *bh, *head, fake; |
| 2039 | sector_t logical; |
| 2040 | |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2041 | if (mpd->io_done) { |
| 2042 | /* |
| 2043 | * Rest of the page in the page_vec |
| 2044 | * redirty then and skip then. We will |
| 2045 | * try to to write them again after |
| 2046 | * starting a new transaction |
| 2047 | */ |
| 2048 | redirty_page_for_writepage(wbc, page); |
| 2049 | unlock_page(page); |
| 2050 | return MPAGE_DA_EXTENT_TAIL; |
| 2051 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2052 | /* |
| 2053 | * Can we merge this page to current extent? |
| 2054 | */ |
| 2055 | if (mpd->next_page != page->index) { |
| 2056 | /* |
| 2057 | * Nope, we can't. So, we map non-allocated blocks |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2058 | * and start IO on them using writepage() |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2059 | */ |
| 2060 | if (mpd->next_page != mpd->first_page) { |
Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2061 | if (mpage_da_map_blocks(mpd) == 0) |
| 2062 | mpage_da_submit_io(mpd); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2063 | /* |
| 2064 | * skip rest of the page in the page_vec |
| 2065 | */ |
| 2066 | mpd->io_done = 1; |
| 2067 | redirty_page_for_writepage(wbc, page); |
| 2068 | unlock_page(page); |
| 2069 | return MPAGE_DA_EXTENT_TAIL; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | /* |
| 2073 | * Start next extent of pages ... |
| 2074 | */ |
| 2075 | mpd->first_page = page->index; |
| 2076 | |
| 2077 | /* |
| 2078 | * ... and blocks |
| 2079 | */ |
| 2080 | mpd->lbh.b_size = 0; |
| 2081 | mpd->lbh.b_state = 0; |
| 2082 | mpd->lbh.b_blocknr = 0; |
| 2083 | } |
| 2084 | |
| 2085 | mpd->next_page = page->index + 1; |
| 2086 | logical = (sector_t) page->index << |
| 2087 | (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 2088 | |
| 2089 | if (!page_has_buffers(page)) { |
| 2090 | /* |
| 2091 | * There is no attached buffer heads yet (mmap?) |
| 2092 | * we treat the page asfull of dirty blocks |
| 2093 | */ |
| 2094 | bh = &fake; |
| 2095 | bh->b_size = PAGE_CACHE_SIZE; |
| 2096 | bh->b_state = 0; |
| 2097 | set_buffer_dirty(bh); |
| 2098 | set_buffer_uptodate(bh); |
| 2099 | mpage_add_bh_to_extent(mpd, logical, bh); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2100 | if (mpd->io_done) |
| 2101 | return MPAGE_DA_EXTENT_TAIL; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2102 | } else { |
| 2103 | /* |
| 2104 | * Page with regular buffer heads, just add all dirty ones |
| 2105 | */ |
| 2106 | head = page_buffers(page); |
| 2107 | bh = head; |
| 2108 | do { |
| 2109 | BUG_ON(buffer_locked(bh)); |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 2110 | /* |
| 2111 | * We need to try to allocate |
| 2112 | * unmapped blocks in the same page. |
| 2113 | * Otherwise we won't make progress |
| 2114 | * with the page in ext4_da_writepage |
| 2115 | */ |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2116 | if (buffer_dirty(bh) && |
| 2117 | (!buffer_mapped(bh) || buffer_delay(bh))) { |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2118 | mpage_add_bh_to_extent(mpd, logical, bh); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2119 | if (mpd->io_done) |
| 2120 | return MPAGE_DA_EXTENT_TAIL; |
Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 2121 | } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { |
| 2122 | /* |
| 2123 | * mapped dirty buffer. We need to update |
| 2124 | * the b_state because we look at |
| 2125 | * b_state in mpage_da_map_blocks. We don't |
| 2126 | * update b_size because if we find an |
| 2127 | * unmapped buffer_head later we need to |
| 2128 | * use the b_state flag of that buffer_head. |
| 2129 | */ |
| 2130 | if (mpd->lbh.b_size == 0) |
| 2131 | mpd->lbh.b_state = |
| 2132 | bh->b_state & BH_FLAGS; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2133 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2134 | logical++; |
| 2135 | } while ((bh = bh->b_this_page) != head); |
| 2136 | } |
| 2137 | |
| 2138 | return 0; |
| 2139 | } |
| 2140 | |
| 2141 | /* |
| 2142 | * mpage_da_writepages - walk the list of dirty pages of the given |
| 2143 | * address space, allocates non-allocated blocks, maps newly-allocated |
| 2144 | * blocks to existing bhs and issue IO them |
| 2145 | * |
| 2146 | * @mapping: address space structure to write |
| 2147 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 2148 | * @get_block: the filesystem's block mapper function. |
| 2149 | * |
| 2150 | * This is a library function, which implements the writepages() |
| 2151 | * address_space_operation. |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2152 | */ |
| 2153 | static int mpage_da_writepages(struct address_space *mapping, |
| 2154 | struct writeback_control *wbc, |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2155 | struct mpage_da_data *mpd) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2156 | { |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2157 | int ret; |
| 2158 | |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2159 | if (!mpd->get_block) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2160 | return generic_writepages(mapping, wbc); |
| 2161 | |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2162 | mpd->lbh.b_size = 0; |
| 2163 | mpd->lbh.b_state = 0; |
| 2164 | mpd->lbh.b_blocknr = 0; |
| 2165 | mpd->first_page = 0; |
| 2166 | mpd->next_page = 0; |
| 2167 | mpd->io_done = 0; |
| 2168 | mpd->pages_written = 0; |
| 2169 | mpd->retval = 0; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2170 | |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2171 | ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, mpd); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2172 | /* |
| 2173 | * Handle last extent of pages |
| 2174 | */ |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2175 | if (!mpd->io_done && mpd->next_page != mpd->first_page) { |
| 2176 | if (mpage_da_map_blocks(mpd) == 0) |
| 2177 | mpage_da_submit_io(mpd); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2178 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2179 | mpd->io_done = 1; |
| 2180 | ret = MPAGE_DA_EXTENT_TAIL; |
| 2181 | } |
| 2182 | wbc->nr_to_write -= mpd->pages_written; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2183 | return ret; |
| 2184 | } |
| 2185 | |
| 2186 | /* |
| 2187 | * this is a special callback for ->write_begin() only |
| 2188 | * it's intention is to return mapped block or reserve space |
| 2189 | */ |
| 2190 | static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, |
| 2191 | struct buffer_head *bh_result, int create) |
| 2192 | { |
| 2193 | int ret = 0; |
| 2194 | |
| 2195 | BUG_ON(create == 0); |
| 2196 | BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); |
| 2197 | |
| 2198 | /* |
| 2199 | * first, we need to know whether the block is allocated already |
| 2200 | * preallocated blocks are unmapped but should treated |
| 2201 | * the same as allocated blocks. |
| 2202 | */ |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2203 | ret = ext4_get_blocks_wrap(NULL, inode, iblock, 1, bh_result, 0, 0, 0); |
| 2204 | if ((ret == 0) && !buffer_delay(bh_result)) { |
| 2205 | /* the block isn't (pre)allocated yet, let's reserve space */ |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2206 | /* |
| 2207 | * XXX: __block_prepare_write() unmaps passed block, |
| 2208 | * is it OK? |
| 2209 | */ |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2210 | ret = ext4_da_reserve_space(inode, 1); |
| 2211 | if (ret) |
| 2212 | /* not enough space to reserve */ |
| 2213 | return ret; |
| 2214 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2215 | map_bh(bh_result, inode->i_sb, 0); |
| 2216 | set_buffer_new(bh_result); |
| 2217 | set_buffer_delay(bh_result); |
| 2218 | } else if (ret > 0) { |
| 2219 | bh_result->b_size = (ret << inode->i_blkbits); |
| 2220 | ret = 0; |
| 2221 | } |
| 2222 | |
| 2223 | return ret; |
| 2224 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2225 | #define EXT4_DELALLOC_RSVED 1 |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2226 | static int ext4_da_get_block_write(struct inode *inode, sector_t iblock, |
| 2227 | struct buffer_head *bh_result, int create) |
| 2228 | { |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2229 | int ret; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2230 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; |
| 2231 | loff_t disksize = EXT4_I(inode)->i_disksize; |
| 2232 | handle_t *handle = NULL; |
| 2233 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2234 | handle = ext4_journal_current_handle(); |
Aneesh Kumar K.V | 166348d | 2008-09-08 23:08:40 -0400 | [diff] [blame] | 2235 | BUG_ON(!handle); |
| 2236 | ret = ext4_get_blocks_wrap(handle, inode, iblock, max_blocks, |
| 2237 | bh_result, create, 0, EXT4_DELALLOC_RSVED); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2238 | if (ret > 0) { |
Aneesh Kumar K.V | 166348d | 2008-09-08 23:08:40 -0400 | [diff] [blame] | 2239 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2240 | bh_result->b_size = (ret << inode->i_blkbits); |
| 2241 | |
Aneesh Kumar K.V | 166348d | 2008-09-08 23:08:40 -0400 | [diff] [blame] | 2242 | if (ext4_should_order_data(inode)) { |
| 2243 | int retval; |
| 2244 | retval = ext4_jbd2_file_inode(handle, inode); |
| 2245 | if (retval) |
| 2246 | /* |
| 2247 | * Failed to add inode for ordered |
| 2248 | * mode. Don't update file size |
| 2249 | */ |
| 2250 | return retval; |
| 2251 | } |
| 2252 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2253 | /* |
| 2254 | * Update on-disk size along with block allocation |
| 2255 | * we don't use 'extend_disksize' as size may change |
| 2256 | * within already allocated block -bzzz |
| 2257 | */ |
| 2258 | disksize = ((loff_t) iblock + ret) << inode->i_blkbits; |
| 2259 | if (disksize > i_size_read(inode)) |
| 2260 | disksize = i_size_read(inode); |
| 2261 | if (disksize > EXT4_I(inode)->i_disksize) { |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 2262 | ext4_update_i_disksize(inode, disksize); |
| 2263 | ret = ext4_mark_inode_dirty(handle, inode); |
| 2264 | return ret; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2265 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2266 | ret = 0; |
| 2267 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2268 | return ret; |
| 2269 | } |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2270 | |
| 2271 | static int ext4_bh_unmapped_or_delay(handle_t *handle, struct buffer_head *bh) |
| 2272 | { |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2273 | /* |
| 2274 | * unmapped buffer is possible for holes. |
| 2275 | * delay buffer is possible with delayed allocation |
| 2276 | */ |
| 2277 | return ((!buffer_mapped(bh) || buffer_delay(bh)) && buffer_dirty(bh)); |
| 2278 | } |
| 2279 | |
| 2280 | static int ext4_normal_get_block_write(struct inode *inode, sector_t iblock, |
| 2281 | struct buffer_head *bh_result, int create) |
| 2282 | { |
| 2283 | int ret = 0; |
| 2284 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; |
| 2285 | |
| 2286 | /* |
| 2287 | * we don't want to do block allocation in writepage |
| 2288 | * so call get_block_wrap with create = 0 |
| 2289 | */ |
| 2290 | ret = ext4_get_blocks_wrap(NULL, inode, iblock, max_blocks, |
| 2291 | bh_result, 0, 0, 0); |
| 2292 | if (ret > 0) { |
| 2293 | bh_result->b_size = (ret << inode->i_blkbits); |
| 2294 | ret = 0; |
| 2295 | } |
| 2296 | return ret; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2297 | } |
| 2298 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2299 | /* |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2300 | * get called vi ext4_da_writepages after taking page lock (have journal handle) |
| 2301 | * get called via journal_submit_inode_data_buffers (no journal handle) |
| 2302 | * get called via shrink_page_list via pdflush (no journal handle) |
| 2303 | * or grab_page_cache when doing write_begin (have journal handle) |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2304 | */ |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2305 | static int ext4_da_writepage(struct page *page, |
| 2306 | struct writeback_control *wbc) |
| 2307 | { |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2308 | int ret = 0; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2309 | loff_t size; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2310 | unsigned int len; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2311 | struct buffer_head *page_bufs; |
| 2312 | struct inode *inode = page->mapping->host; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2313 | |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2314 | size = i_size_read(inode); |
| 2315 | if (page->index == size >> PAGE_CACHE_SHIFT) |
| 2316 | len = size & ~PAGE_CACHE_MASK; |
| 2317 | else |
| 2318 | len = PAGE_CACHE_SIZE; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2319 | |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2320 | if (page_has_buffers(page)) { |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2321 | page_bufs = page_buffers(page); |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2322 | if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, |
| 2323 | ext4_bh_unmapped_or_delay)) { |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2324 | /* |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2325 | * We don't want to do block allocation |
| 2326 | * So redirty the page and return |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2327 | * We may reach here when we do a journal commit |
| 2328 | * via journal_submit_inode_data_buffers. |
| 2329 | * If we don't have mapping block we just ignore |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2330 | * them. We can also reach here via shrink_page_list |
| 2331 | */ |
| 2332 | redirty_page_for_writepage(wbc, page); |
| 2333 | unlock_page(page); |
| 2334 | return 0; |
| 2335 | } |
| 2336 | } else { |
| 2337 | /* |
| 2338 | * The test for page_has_buffers() is subtle: |
| 2339 | * We know the page is dirty but it lost buffers. That means |
| 2340 | * that at some moment in time after write_begin()/write_end() |
| 2341 | * has been called all buffers have been clean and thus they |
| 2342 | * must have been written at least once. So they are all |
| 2343 | * mapped and we can happily proceed with mapping them |
| 2344 | * and writing the page. |
| 2345 | * |
| 2346 | * Try to initialize the buffer_heads and check whether |
| 2347 | * all are mapped and non delay. We don't want to |
| 2348 | * do block allocation here. |
| 2349 | */ |
| 2350 | ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE, |
| 2351 | ext4_normal_get_block_write); |
| 2352 | if (!ret) { |
| 2353 | page_bufs = page_buffers(page); |
| 2354 | /* check whether all are mapped and non delay */ |
| 2355 | if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, |
| 2356 | ext4_bh_unmapped_or_delay)) { |
| 2357 | redirty_page_for_writepage(wbc, page); |
| 2358 | unlock_page(page); |
| 2359 | return 0; |
| 2360 | } |
| 2361 | } else { |
| 2362 | /* |
| 2363 | * We can't do block allocation here |
| 2364 | * so just redity the page and unlock |
| 2365 | * and return |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2366 | */ |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2367 | redirty_page_for_writepage(wbc, page); |
| 2368 | unlock_page(page); |
| 2369 | return 0; |
| 2370 | } |
Aneesh Kumar K.V | ed9b3e3 | 2008-11-07 09:06:45 -0500 | [diff] [blame] | 2371 | /* now mark the buffer_heads as dirty and uptodate */ |
| 2372 | block_commit_write(page, 0, PAGE_CACHE_SIZE); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode)) |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2376 | ret = nobh_writepage(page, ext4_normal_get_block_write, wbc); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2377 | else |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2378 | ret = block_write_full_page(page, |
| 2379 | ext4_normal_get_block_write, |
| 2380 | wbc); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2381 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2382 | return ret; |
| 2383 | } |
| 2384 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2385 | /* |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2386 | * This is called via ext4_da_writepages() to |
| 2387 | * calulate the total number of credits to reserve to fit |
| 2388 | * a single extent allocation into a single transaction, |
| 2389 | * ext4_da_writpeages() will loop calling this before |
| 2390 | * the block allocation. |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2391 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2392 | |
| 2393 | static int ext4_da_writepages_trans_blocks(struct inode *inode) |
| 2394 | { |
| 2395 | int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; |
| 2396 | |
| 2397 | /* |
| 2398 | * With non-extent format the journal credit needed to |
| 2399 | * insert nrblocks contiguous block is dependent on |
| 2400 | * number of contiguous block. So we will limit |
| 2401 | * number of contiguous block to a sane value |
| 2402 | */ |
| 2403 | if (!(inode->i_flags & EXT4_EXTENTS_FL) && |
| 2404 | (max_blocks > EXT4_MAX_TRANS_DATA)) |
| 2405 | max_blocks = EXT4_MAX_TRANS_DATA; |
| 2406 | |
| 2407 | return ext4_chunk_trans_blocks(inode, max_blocks); |
| 2408 | } |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2409 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2410 | static int ext4_da_writepages(struct address_space *mapping, |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2411 | struct writeback_control *wbc) |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2412 | { |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2413 | pgoff_t index; |
| 2414 | int range_whole = 0; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2415 | handle_t *handle = NULL; |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2416 | struct mpage_da_data mpd; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2417 | struct inode *inode = mapping->host; |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2418 | int no_nrwrite_index_update; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2419 | int pages_written = 0; |
| 2420 | long pages_skipped; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2421 | int needed_blocks, ret = 0, nr_to_writebump = 0; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2422 | struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2423 | |
| 2424 | /* |
| 2425 | * No pages to write? This is mainly a kludge to avoid starting |
| 2426 | * a transaction for special inodes like journal inode on last iput() |
| 2427 | * because that could violate lock ordering on umount |
| 2428 | */ |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2429 | if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2430 | return 0; |
Theodore Ts'o | 2a21e37 | 2008-11-05 09:22:24 -0500 | [diff] [blame] | 2431 | |
| 2432 | /* |
| 2433 | * If the filesystem has aborted, it is read-only, so return |
| 2434 | * right away instead of dumping stack traces later on that |
| 2435 | * will obscure the real source of the problem. We test |
| 2436 | * EXT4_MOUNT_ABORT instead of sb->s_flag's MS_RDONLY because |
| 2437 | * the latter could be true if the filesystem is mounted |
| 2438 | * read-only, and in that case, ext4_da_writepages should |
| 2439 | * *never* be called, so if that ever happens, we would want |
| 2440 | * the stack trace. |
| 2441 | */ |
| 2442 | if (unlikely(sbi->s_mount_opt & EXT4_MOUNT_ABORT)) |
| 2443 | return -EROFS; |
| 2444 | |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2445 | /* |
| 2446 | * Make sure nr_to_write is >= sbi->s_mb_stream_request |
| 2447 | * This make sure small files blocks are allocated in |
| 2448 | * single attempt. This ensure that small files |
| 2449 | * get less fragmented. |
| 2450 | */ |
| 2451 | if (wbc->nr_to_write < sbi->s_mb_stream_request) { |
| 2452 | nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write; |
| 2453 | wbc->nr_to_write = sbi->s_mb_stream_request; |
| 2454 | } |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2455 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 2456 | range_whole = 1; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2457 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2458 | if (wbc->range_cyclic) |
| 2459 | index = mapping->writeback_index; |
| 2460 | else |
| 2461 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2462 | |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2463 | mpd.wbc = wbc; |
| 2464 | mpd.inode = mapping->host; |
| 2465 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2466 | /* |
| 2467 | * we don't want write_cache_pages to update |
| 2468 | * nr_to_write and writeback_index |
| 2469 | */ |
| 2470 | no_nrwrite_index_update = wbc->no_nrwrite_index_update; |
| 2471 | wbc->no_nrwrite_index_update = 1; |
| 2472 | pages_skipped = wbc->pages_skipped; |
| 2473 | |
| 2474 | while (!ret && wbc->nr_to_write > 0) { |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2475 | |
| 2476 | /* |
| 2477 | * we insert one extent at a time. So we need |
| 2478 | * credit needed for single extent allocation. |
| 2479 | * journalled mode is currently not supported |
| 2480 | * by delalloc |
| 2481 | */ |
| 2482 | BUG_ON(ext4_should_journal_data(inode)); |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2483 | needed_blocks = ext4_da_writepages_trans_blocks(inode); |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2484 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2485 | /* start a new transaction*/ |
| 2486 | handle = ext4_journal_start(inode, needed_blocks); |
| 2487 | if (IS_ERR(handle)) { |
| 2488 | ret = PTR_ERR(handle); |
Theodore Ts'o | 2a21e37 | 2008-11-05 09:22:24 -0500 | [diff] [blame] | 2489 | printk(KERN_CRIT "%s: jbd2_start: " |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2490 | "%ld pages, ino %lu; err %d\n", __func__, |
| 2491 | wbc->nr_to_write, inode->i_ino, ret); |
| 2492 | dump_stack(); |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2493 | goto out_writepages; |
| 2494 | } |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2495 | mpd.get_block = ext4_da_get_block_write; |
| 2496 | ret = mpage_da_writepages(mapping, wbc, &mpd); |
| 2497 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2498 | ext4_journal_stop(handle); |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2499 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2500 | if (mpd.retval == -ENOSPC) { |
| 2501 | /* commit the transaction which would |
| 2502 | * free blocks released in the transaction |
| 2503 | * and try again |
| 2504 | */ |
Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2505 | jbd2_journal_force_commit_nested(sbi->s_journal); |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2506 | wbc->pages_skipped = pages_skipped; |
| 2507 | ret = 0; |
| 2508 | } else if (ret == MPAGE_DA_EXTENT_TAIL) { |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2509 | /* |
| 2510 | * got one extent now try with |
| 2511 | * rest of the pages |
| 2512 | */ |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2513 | pages_written += mpd.pages_written; |
| 2514 | wbc->pages_skipped = pages_skipped; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2515 | ret = 0; |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2516 | } else if (wbc->nr_to_write) |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2517 | /* |
| 2518 | * There is no more writeout needed |
| 2519 | * or we requested for a noblocking writeout |
| 2520 | * and we found the device congested |
| 2521 | */ |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2522 | break; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2523 | } |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2524 | if (pages_skipped != wbc->pages_skipped) |
| 2525 | printk(KERN_EMERG "This should not happen leaving %s " |
| 2526 | "with nr_to_write = %ld ret = %d\n", |
| 2527 | __func__, wbc->nr_to_write, ret); |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2528 | |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2529 | /* Update index */ |
| 2530 | index += pages_written; |
| 2531 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 2532 | /* |
| 2533 | * set the writeback_index so that range_cyclic |
| 2534 | * mode will write it back later |
| 2535 | */ |
| 2536 | mapping->writeback_index = index; |
Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2537 | |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2538 | out_writepages: |
Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2539 | if (!no_nrwrite_index_update) |
| 2540 | wbc->no_nrwrite_index_update = 0; |
| 2541 | wbc->nr_to_write -= nr_to_writebump; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2542 | return ret; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2543 | } |
| 2544 | |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2545 | #define FALL_BACK_TO_NONDELALLOC 1 |
| 2546 | static int ext4_nonda_switch(struct super_block *sb) |
| 2547 | { |
| 2548 | s64 free_blocks, dirty_blocks; |
| 2549 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2550 | |
| 2551 | /* |
| 2552 | * switch to non delalloc mode if we are running low |
| 2553 | * on free block. The free block accounting via percpu |
| 2554 | * counters can get slightly wrong with FBC_BATCH getting |
| 2555 | * accumulated on each CPU without updating global counters |
| 2556 | * Delalloc need an accurate free block accounting. So switch |
| 2557 | * to non delalloc when we are near to error range. |
| 2558 | */ |
| 2559 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
| 2560 | dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); |
| 2561 | if (2 * free_blocks < 3 * dirty_blocks || |
| 2562 | free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { |
| 2563 | /* |
| 2564 | * free block count is less that 150% of dirty blocks |
| 2565 | * or free blocks is less that watermark |
| 2566 | */ |
| 2567 | return 1; |
| 2568 | } |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2572 | static int ext4_da_write_begin(struct file *file, struct address_space *mapping, |
| 2573 | loff_t pos, unsigned len, unsigned flags, |
| 2574 | struct page **pagep, void **fsdata) |
| 2575 | { |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2576 | int ret, retries = 0; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2577 | struct page *page; |
| 2578 | pgoff_t index; |
| 2579 | unsigned from, to; |
| 2580 | struct inode *inode = mapping->host; |
| 2581 | handle_t *handle; |
| 2582 | |
| 2583 | index = pos >> PAGE_CACHE_SHIFT; |
| 2584 | from = pos & (PAGE_CACHE_SIZE - 1); |
| 2585 | to = from + len; |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2586 | |
| 2587 | if (ext4_nonda_switch(inode->i_sb)) { |
| 2588 | *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; |
| 2589 | return ext4_write_begin(file, mapping, pos, |
| 2590 | len, flags, pagep, fsdata); |
| 2591 | } |
| 2592 | *fsdata = (void *)0; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2593 | retry: |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2594 | /* |
| 2595 | * With delayed allocation, we don't log the i_disksize update |
| 2596 | * if there is delayed block allocation. But we still need |
| 2597 | * to journalling the i_disksize update if writes to the end |
| 2598 | * of file which has an already mapped buffer. |
| 2599 | */ |
| 2600 | handle = ext4_journal_start(inode, 1); |
| 2601 | if (IS_ERR(handle)) { |
| 2602 | ret = PTR_ERR(handle); |
| 2603 | goto out; |
| 2604 | } |
| 2605 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 2606 | page = grab_cache_page_write_begin(mapping, index, flags); |
Eric Sandeen | d5a0d4f | 2008-08-02 18:51:06 -0400 | [diff] [blame] | 2607 | if (!page) { |
| 2608 | ext4_journal_stop(handle); |
| 2609 | ret = -ENOMEM; |
| 2610 | goto out; |
| 2611 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2612 | *pagep = page; |
| 2613 | |
| 2614 | ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
| 2615 | ext4_da_get_block_prep); |
| 2616 | if (ret < 0) { |
| 2617 | unlock_page(page); |
| 2618 | ext4_journal_stop(handle); |
| 2619 | page_cache_release(page); |
Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 2620 | /* |
| 2621 | * block_write_begin may have instantiated a few blocks |
| 2622 | * outside i_size. Trim these off again. Don't need |
| 2623 | * i_size_read because we hold i_mutex. |
| 2624 | */ |
| 2625 | if (pos + len > inode->i_size) |
| 2626 | vmtruncate(inode, inode->i_size); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2627 | } |
| 2628 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2629 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
| 2630 | goto retry; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2631 | out: |
| 2632 | return ret; |
| 2633 | } |
| 2634 | |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2635 | /* |
| 2636 | * Check if we should update i_disksize |
| 2637 | * when write to the end of file but not require block allocation |
| 2638 | */ |
| 2639 | static int ext4_da_should_update_i_disksize(struct page *page, |
| 2640 | unsigned long offset) |
| 2641 | { |
| 2642 | struct buffer_head *bh; |
| 2643 | struct inode *inode = page->mapping->host; |
| 2644 | unsigned int idx; |
| 2645 | int i; |
| 2646 | |
| 2647 | bh = page_buffers(page); |
| 2648 | idx = offset >> inode->i_blkbits; |
| 2649 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2650 | for (i = 0; i < idx; i++) |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2651 | bh = bh->b_this_page; |
| 2652 | |
| 2653 | if (!buffer_mapped(bh) || (buffer_delay(bh))) |
| 2654 | return 0; |
| 2655 | return 1; |
| 2656 | } |
| 2657 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2658 | static int ext4_da_write_end(struct file *file, |
| 2659 | struct address_space *mapping, |
| 2660 | loff_t pos, unsigned len, unsigned copied, |
| 2661 | struct page *page, void *fsdata) |
| 2662 | { |
| 2663 | struct inode *inode = mapping->host; |
| 2664 | int ret = 0, ret2; |
| 2665 | handle_t *handle = ext4_journal_current_handle(); |
| 2666 | loff_t new_i_size; |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2667 | unsigned long start, end; |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2668 | int write_mode = (int)(unsigned long)fsdata; |
| 2669 | |
| 2670 | if (write_mode == FALL_BACK_TO_NONDELALLOC) { |
| 2671 | if (ext4_should_order_data(inode)) { |
| 2672 | return ext4_ordered_write_end(file, mapping, pos, |
| 2673 | len, copied, page, fsdata); |
| 2674 | } else if (ext4_should_writeback_data(inode)) { |
| 2675 | return ext4_writeback_write_end(file, mapping, pos, |
| 2676 | len, copied, page, fsdata); |
| 2677 | } else { |
| 2678 | BUG(); |
| 2679 | } |
| 2680 | } |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2681 | |
| 2682 | start = pos & (PAGE_CACHE_SIZE - 1); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2683 | end = start + copied - 1; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2684 | |
| 2685 | /* |
| 2686 | * generic_write_end() will run mark_inode_dirty() if i_size |
| 2687 | * changes. So let's piggyback the i_disksize mark_inode_dirty |
| 2688 | * into that. |
| 2689 | */ |
| 2690 | |
| 2691 | new_i_size = pos + copied; |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2692 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 2693 | if (ext4_da_should_update_i_disksize(page, end)) { |
| 2694 | down_write(&EXT4_I(inode)->i_data_sem); |
| 2695 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
| 2696 | /* |
| 2697 | * Updating i_disksize when extending file |
| 2698 | * without needing block allocation |
| 2699 | */ |
| 2700 | if (ext4_should_order_data(inode)) |
| 2701 | ret = ext4_jbd2_file_inode(handle, |
| 2702 | inode); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2703 | |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2704 | EXT4_I(inode)->i_disksize = new_i_size; |
| 2705 | } |
| 2706 | up_write(&EXT4_I(inode)->i_data_sem); |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 2707 | /* We need to mark inode dirty even if |
| 2708 | * new_i_size is less that inode->i_size |
| 2709 | * bu greater than i_disksize.(hint delalloc) |
| 2710 | */ |
| 2711 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2712 | } |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2713 | } |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2714 | ret2 = generic_write_end(file, mapping, pos, len, copied, |
| 2715 | page, fsdata); |
| 2716 | copied = ret2; |
| 2717 | if (ret2 < 0) |
| 2718 | ret = ret2; |
| 2719 | ret2 = ext4_journal_stop(handle); |
| 2720 | if (!ret) |
| 2721 | ret = ret2; |
| 2722 | |
| 2723 | return ret ? ret : copied; |
| 2724 | } |
| 2725 | |
| 2726 | static void ext4_da_invalidatepage(struct page *page, unsigned long offset) |
| 2727 | { |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2728 | /* |
| 2729 | * Drop reserved blocks |
| 2730 | */ |
| 2731 | BUG_ON(!PageLocked(page)); |
| 2732 | if (!page_has_buffers(page)) |
| 2733 | goto out; |
| 2734 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2735 | ext4_da_page_release_reservation(page, offset); |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2736 | |
| 2737 | out: |
| 2738 | ext4_invalidatepage(page, offset); |
| 2739 | |
| 2740 | return; |
| 2741 | } |
| 2742 | |
| 2743 | |
| 2744 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2745 | * bmap() is special. It gets used by applications such as lilo and by |
| 2746 | * the swapper to find the on-disk block of a specific piece of data. |
| 2747 | * |
| 2748 | * Naturally, this is dangerous if the block concerned is still in the |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2749 | * journal. If somebody makes a swapfile on an ext4 data-journaling |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2750 | * filesystem and enables swap, then they may get a nasty shock when the |
| 2751 | * data getting swapped to that swapfile suddenly gets overwritten by |
| 2752 | * the original zero's written out previously to the journal and |
| 2753 | * awaiting writeback in the kernel's buffer cache. |
| 2754 | * |
| 2755 | * So, if we see any bmap calls here on a modified, data-journaled file, |
| 2756 | * take extra steps to flush any blocks which might be in the cache. |
| 2757 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2758 | static sector_t ext4_bmap(struct address_space *mapping, sector_t block) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2759 | { |
| 2760 | struct inode *inode = mapping->host; |
| 2761 | journal_t *journal; |
| 2762 | int err; |
| 2763 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2764 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && |
| 2765 | test_opt(inode->i_sb, DELALLOC)) { |
| 2766 | /* |
| 2767 | * With delalloc we want to sync the file |
| 2768 | * so that we can make sure we allocate |
| 2769 | * blocks for file |
| 2770 | */ |
| 2771 | filemap_write_and_wait(mapping); |
| 2772 | } |
| 2773 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2774 | BUG_ON(!EXT4_JOURNAL(inode) && |
| 2775 | EXT4_I(inode)->i_state & EXT4_STATE_JDATA); |
| 2776 | |
| 2777 | if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2778 | /* |
| 2779 | * This is a REALLY heavyweight approach, but the use of |
| 2780 | * bmap on dirty files is expected to be extremely rare: |
| 2781 | * only if we run lilo or swapon on a freshly made file |
| 2782 | * do we expect this to happen. |
| 2783 | * |
| 2784 | * (bmap requires CAP_SYS_RAWIO so this does not |
| 2785 | * represent an unprivileged user DOS attack --- we'd be |
| 2786 | * in trouble if mortal users could trigger this path at |
| 2787 | * will.) |
| 2788 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2789 | * NB. EXT4_STATE_JDATA is not set on files other than |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2790 | * regular files. If somebody wants to bmap a directory |
| 2791 | * or symlink and gets confused because the buffer |
| 2792 | * hasn't yet been flushed to disk, they deserve |
| 2793 | * everything they get. |
| 2794 | */ |
| 2795 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2796 | EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA; |
| 2797 | journal = EXT4_JOURNAL(inode); |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 2798 | jbd2_journal_lock_updates(journal); |
| 2799 | err = jbd2_journal_flush(journal); |
| 2800 | jbd2_journal_unlock_updates(journal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2801 | |
| 2802 | if (err) |
| 2803 | return 0; |
| 2804 | } |
| 2805 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2806 | return generic_block_bmap(mapping, block, ext4_get_block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | static int bget_one(handle_t *handle, struct buffer_head *bh) |
| 2810 | { |
| 2811 | get_bh(bh); |
| 2812 | return 0; |
| 2813 | } |
| 2814 | |
| 2815 | static int bput_one(handle_t *handle, struct buffer_head *bh) |
| 2816 | { |
| 2817 | put_bh(bh); |
| 2818 | return 0; |
| 2819 | } |
| 2820 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2821 | /* |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2822 | * Note that we don't need to start a transaction unless we're journaling data |
| 2823 | * because we should have holes filled from ext4_page_mkwrite(). We even don't |
| 2824 | * need to file the inode to the transaction's list in ordered mode because if |
| 2825 | * we are writing back data added by write(), the inode is already there and if |
| 2826 | * we are writing back data modified via mmap(), noone guarantees in which |
| 2827 | * transaction the data will hit the disk. In case we are journaling data, we |
| 2828 | * cannot start transaction directly because transaction start ranks above page |
| 2829 | * lock so we have to do some magic. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2830 | * |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2831 | * In all journaling modes block_write_full_page() will start the I/O. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2832 | * |
| 2833 | * Problem: |
| 2834 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2835 | * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> |
| 2836 | * ext4_writepage() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2837 | * |
| 2838 | * Similar for: |
| 2839 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2840 | * ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ... |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2841 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2842 | * Same applies to ext4_get_block(). We will deadlock on various things like |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 2843 | * lock_journal and i_data_sem |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2844 | * |
| 2845 | * Setting PF_MEMALLOC here doesn't work - too many internal memory |
| 2846 | * allocations fail. |
| 2847 | * |
| 2848 | * 16May01: If we're reentered then journal_current_handle() will be |
| 2849 | * non-zero. We simply *return*. |
| 2850 | * |
| 2851 | * 1 July 2001: @@@ FIXME: |
| 2852 | * In journalled data mode, a data buffer may be metadata against the |
| 2853 | * current transaction. But the same file is part of a shared mapping |
| 2854 | * and someone does a writepage() on it. |
| 2855 | * |
| 2856 | * We will move the buffer onto the async_data list, but *after* it has |
| 2857 | * been dirtied. So there's a small window where we have dirty data on |
| 2858 | * BJ_Metadata. |
| 2859 | * |
| 2860 | * Note that this only applies to the last partial page in the file. The |
| 2861 | * bit which block_write_full_page() uses prepare/commit for. (That's |
| 2862 | * broken code anyway: it's wrong for msync()). |
| 2863 | * |
| 2864 | * It's a rare case: affects the final partial page, for journalled data |
| 2865 | * where the file is subject to bith write() and writepage() in the same |
| 2866 | * transction. To fix it we'll need a custom block_write_full_page(). |
| 2867 | * We'll probably need that anyway for journalling writepage() output. |
| 2868 | * |
| 2869 | * We don't honour synchronous mounts for writepage(). That would be |
| 2870 | * disastrous. Any write() or metadata operation will sync the fs for |
| 2871 | * us. |
| 2872 | * |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2873 | */ |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2874 | static int __ext4_normal_writepage(struct page *page, |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2875 | struct writeback_control *wbc) |
| 2876 | { |
| 2877 | struct inode *inode = page->mapping->host; |
| 2878 | |
| 2879 | if (test_opt(inode->i_sb, NOBH)) |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2880 | return nobh_writepage(page, |
| 2881 | ext4_normal_get_block_write, wbc); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2882 | else |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2883 | return block_write_full_page(page, |
| 2884 | ext4_normal_get_block_write, |
| 2885 | wbc); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2886 | } |
| 2887 | |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2888 | static int ext4_normal_writepage(struct page *page, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2889 | struct writeback_control *wbc) |
| 2890 | { |
| 2891 | struct inode *inode = page->mapping->host; |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2892 | loff_t size = i_size_read(inode); |
| 2893 | loff_t len; |
| 2894 | |
| 2895 | J_ASSERT(PageLocked(page)); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2896 | if (page->index == size >> PAGE_CACHE_SHIFT) |
| 2897 | len = size & ~PAGE_CACHE_MASK; |
| 2898 | else |
| 2899 | len = PAGE_CACHE_SIZE; |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2900 | |
| 2901 | if (page_has_buffers(page)) { |
| 2902 | /* if page has buffers it should all be mapped |
| 2903 | * and allocated. If there are not buffers attached |
| 2904 | * to the page we know the page is dirty but it lost |
| 2905 | * buffers. That means that at some moment in time |
| 2906 | * after write_begin() / write_end() has been called |
| 2907 | * all buffers have been clean and thus they must have been |
| 2908 | * written at least once. So they are all mapped and we can |
| 2909 | * happily proceed with mapping them and writing the page. |
| 2910 | */ |
| 2911 | BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, |
| 2912 | ext4_bh_unmapped_or_delay)); |
| 2913 | } |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2914 | |
| 2915 | if (!ext4_journal_current_handle()) |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2916 | return __ext4_normal_writepage(page, wbc); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2917 | |
| 2918 | redirty_page_for_writepage(wbc, page); |
| 2919 | unlock_page(page); |
| 2920 | return 0; |
| 2921 | } |
| 2922 | |
| 2923 | static int __ext4_journalled_writepage(struct page *page, |
| 2924 | struct writeback_control *wbc) |
| 2925 | { |
| 2926 | struct address_space *mapping = page->mapping; |
| 2927 | struct inode *inode = mapping->host; |
| 2928 | struct buffer_head *page_bufs; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2929 | handle_t *handle = NULL; |
| 2930 | int ret = 0; |
| 2931 | int err; |
| 2932 | |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2933 | ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE, |
| 2934 | ext4_normal_get_block_write); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2935 | if (ret != 0) |
| 2936 | goto out_unlock; |
| 2937 | |
| 2938 | page_bufs = page_buffers(page); |
| 2939 | walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE, NULL, |
| 2940 | bget_one); |
| 2941 | /* As soon as we unlock the page, it can go away, but we have |
| 2942 | * references to buffers so we are safe */ |
| 2943 | unlock_page(page); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2944 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2945 | handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2946 | if (IS_ERR(handle)) { |
| 2947 | ret = PTR_ERR(handle); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2948 | goto out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2951 | ret = walk_page_buffers(handle, page_bufs, 0, |
| 2952 | PAGE_CACHE_SIZE, NULL, do_journal_get_write_access); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2953 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2954 | err = walk_page_buffers(handle, page_bufs, 0, |
| 2955 | PAGE_CACHE_SIZE, NULL, write_end_fn); |
| 2956 | if (ret == 0) |
| 2957 | ret = err; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2958 | err = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2959 | if (!ret) |
| 2960 | ret = err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2961 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2962 | walk_page_buffers(handle, page_bufs, 0, |
| 2963 | PAGE_CACHE_SIZE, NULL, bput_one); |
| 2964 | EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; |
| 2965 | goto out; |
| 2966 | |
| 2967 | out_unlock: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2968 | unlock_page(page); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2969 | out: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2970 | return ret; |
| 2971 | } |
| 2972 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2973 | static int ext4_journalled_writepage(struct page *page, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2974 | struct writeback_control *wbc) |
| 2975 | { |
| 2976 | struct inode *inode = page->mapping->host; |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2977 | loff_t size = i_size_read(inode); |
| 2978 | loff_t len; |
| 2979 | |
| 2980 | J_ASSERT(PageLocked(page)); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2981 | if (page->index == size >> PAGE_CACHE_SHIFT) |
| 2982 | len = size & ~PAGE_CACHE_MASK; |
| 2983 | else |
| 2984 | len = PAGE_CACHE_SIZE; |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2985 | |
| 2986 | if (page_has_buffers(page)) { |
| 2987 | /* if page has buffers it should all be mapped |
| 2988 | * and allocated. If there are not buffers attached |
| 2989 | * to the page we know the page is dirty but it lost |
| 2990 | * buffers. That means that at some moment in time |
| 2991 | * after write_begin() / write_end() has been called |
| 2992 | * all buffers have been clean and thus they must have been |
| 2993 | * written at least once. So they are all mapped and we can |
| 2994 | * happily proceed with mapping them and writing the page. |
| 2995 | */ |
| 2996 | BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, |
| 2997 | ext4_bh_unmapped_or_delay)); |
| 2998 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2999 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3000 | if (ext4_journal_current_handle()) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3001 | goto no_write; |
| 3002 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3003 | if (PageChecked(page)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3004 | /* |
| 3005 | * It's mmapped pagecache. Add buffers and journal it. There |
| 3006 | * doesn't seem much point in redirtying the page here. |
| 3007 | */ |
| 3008 | ClearPageChecked(page); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3009 | return __ext4_journalled_writepage(page, wbc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3010 | } else { |
| 3011 | /* |
| 3012 | * It may be a page full of checkpoint-mode buffers. We don't |
| 3013 | * really know unless we go poke around in the buffer_heads. |
| 3014 | * But block_write_full_page will do the right thing. |
| 3015 | */ |
Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3016 | return block_write_full_page(page, |
| 3017 | ext4_normal_get_block_write, |
| 3018 | wbc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3019 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3020 | no_write: |
| 3021 | redirty_page_for_writepage(wbc, page); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3022 | unlock_page(page); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3023 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3026 | static int ext4_readpage(struct file *file, struct page *page) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3027 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3028 | return mpage_readpage(page, ext4_get_block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3032 | ext4_readpages(struct file *file, struct address_space *mapping, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3033 | struct list_head *pages, unsigned nr_pages) |
| 3034 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3035 | return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3038 | static void ext4_invalidatepage(struct page *page, unsigned long offset) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3039 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3040 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3041 | |
| 3042 | /* |
| 3043 | * If it's a full truncate we just forget about the pending dirtying |
| 3044 | */ |
| 3045 | if (offset == 0) |
| 3046 | ClearPageChecked(page); |
| 3047 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3048 | if (journal) |
| 3049 | jbd2_journal_invalidatepage(journal, page, offset); |
| 3050 | else |
| 3051 | block_invalidatepage(page, offset); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3052 | } |
| 3053 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3054 | static int ext4_releasepage(struct page *page, gfp_t wait) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3055 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3056 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3057 | |
| 3058 | WARN_ON(PageChecked(page)); |
| 3059 | if (!page_has_buffers(page)) |
| 3060 | return 0; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3061 | if (journal) |
| 3062 | return jbd2_journal_try_to_free_buffers(journal, page, wait); |
| 3063 | else |
| 3064 | return try_to_free_buffers(page); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | /* |
| 3068 | * If the O_DIRECT write will extend the file then add this inode to the |
| 3069 | * orphan list. So recovery will truncate it back to the original size |
| 3070 | * if the machine crashes during the write. |
| 3071 | * |
| 3072 | * 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] | 3073 | * crashes then stale disk data _may_ be exposed inside the file. But current |
| 3074 | * 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] | 3075 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3076 | static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3077 | const struct iovec *iov, loff_t offset, |
| 3078 | unsigned long nr_segs) |
| 3079 | { |
| 3080 | struct file *file = iocb->ki_filp; |
| 3081 | struct inode *inode = file->f_mapping->host; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3082 | struct ext4_inode_info *ei = EXT4_I(inode); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3083 | handle_t *handle; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3084 | ssize_t ret; |
| 3085 | int orphan = 0; |
| 3086 | size_t count = iov_length(iov, nr_segs); |
| 3087 | |
| 3088 | if (rw == WRITE) { |
| 3089 | loff_t final_size = offset + count; |
| 3090 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3091 | if (final_size > inode->i_size) { |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3092 | /* Credits for sb + inode write */ |
| 3093 | handle = ext4_journal_start(inode, 2); |
| 3094 | if (IS_ERR(handle)) { |
| 3095 | ret = PTR_ERR(handle); |
| 3096 | goto out; |
| 3097 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3098 | ret = ext4_orphan_add(handle, inode); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3099 | if (ret) { |
| 3100 | ext4_journal_stop(handle); |
| 3101 | goto out; |
| 3102 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3103 | orphan = 1; |
| 3104 | ei->i_disksize = inode->i_size; |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3105 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, |
| 3110 | offset, nr_segs, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3111 | ext4_get_block, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3112 | |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3113 | if (orphan) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3114 | int err; |
| 3115 | |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3116 | /* Credits for sb + inode write */ |
| 3117 | handle = ext4_journal_start(inode, 2); |
| 3118 | if (IS_ERR(handle)) { |
| 3119 | /* This is really bad luck. We've written the data |
| 3120 | * but cannot extend i_size. Bail out and pretend |
| 3121 | * the write failed... */ |
| 3122 | ret = PTR_ERR(handle); |
| 3123 | goto out; |
| 3124 | } |
| 3125 | if (inode->i_nlink) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3126 | ext4_orphan_del(handle, inode); |
Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3127 | if (ret > 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3128 | loff_t end = offset + ret; |
| 3129 | if (end > inode->i_size) { |
| 3130 | ei->i_disksize = end; |
| 3131 | i_size_write(inode, end); |
| 3132 | /* |
| 3133 | * We're going to return a positive `ret' |
| 3134 | * here due to non-zero-length I/O, so there's |
| 3135 | * no way of reporting error returns from |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3136 | * ext4_mark_inode_dirty() to userspace. So |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3137 | * ignore it. |
| 3138 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3139 | ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3140 | } |
| 3141 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3142 | err = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3143 | if (ret == 0) |
| 3144 | ret = err; |
| 3145 | } |
| 3146 | out: |
| 3147 | return ret; |
| 3148 | } |
| 3149 | |
| 3150 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3151 | * Pages can be marked dirty completely asynchronously from ext4's journalling |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3152 | * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do |
| 3153 | * much here because ->set_page_dirty is called under VFS locks. The page is |
| 3154 | * not necessarily locked. |
| 3155 | * |
| 3156 | * We cannot just dirty the page and leave attached buffers clean, because the |
| 3157 | * buffers' dirty state is "definitive". We cannot just set the buffers dirty |
| 3158 | * or jbddirty because all the journalling code will explode. |
| 3159 | * |
| 3160 | * So what we do is to mark the page "pending dirty" and next time writepage |
| 3161 | * is called, propagate that into the buffers appropriately. |
| 3162 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3163 | static int ext4_journalled_set_page_dirty(struct page *page) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3164 | { |
| 3165 | SetPageChecked(page); |
| 3166 | return __set_page_dirty_nobuffers(page); |
| 3167 | } |
| 3168 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3169 | static const struct address_space_operations ext4_ordered_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3170 | .readpage = ext4_readpage, |
| 3171 | .readpages = ext4_readpages, |
| 3172 | .writepage = ext4_normal_writepage, |
| 3173 | .sync_page = block_sync_page, |
| 3174 | .write_begin = ext4_write_begin, |
| 3175 | .write_end = ext4_ordered_write_end, |
| 3176 | .bmap = ext4_bmap, |
| 3177 | .invalidatepage = ext4_invalidatepage, |
| 3178 | .releasepage = ext4_releasepage, |
| 3179 | .direct_IO = ext4_direct_IO, |
| 3180 | .migratepage = buffer_migrate_page, |
| 3181 | .is_partially_uptodate = block_is_partially_uptodate, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3182 | }; |
| 3183 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3184 | static const struct address_space_operations ext4_writeback_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3185 | .readpage = ext4_readpage, |
| 3186 | .readpages = ext4_readpages, |
| 3187 | .writepage = ext4_normal_writepage, |
| 3188 | .sync_page = block_sync_page, |
| 3189 | .write_begin = ext4_write_begin, |
| 3190 | .write_end = ext4_writeback_write_end, |
| 3191 | .bmap = ext4_bmap, |
| 3192 | .invalidatepage = ext4_invalidatepage, |
| 3193 | .releasepage = ext4_releasepage, |
| 3194 | .direct_IO = ext4_direct_IO, |
| 3195 | .migratepage = buffer_migrate_page, |
| 3196 | .is_partially_uptodate = block_is_partially_uptodate, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3197 | }; |
| 3198 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3199 | static const struct address_space_operations ext4_journalled_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3200 | .readpage = ext4_readpage, |
| 3201 | .readpages = ext4_readpages, |
| 3202 | .writepage = ext4_journalled_writepage, |
| 3203 | .sync_page = block_sync_page, |
| 3204 | .write_begin = ext4_write_begin, |
| 3205 | .write_end = ext4_journalled_write_end, |
| 3206 | .set_page_dirty = ext4_journalled_set_page_dirty, |
| 3207 | .bmap = ext4_bmap, |
| 3208 | .invalidatepage = ext4_invalidatepage, |
| 3209 | .releasepage = ext4_releasepage, |
| 3210 | .is_partially_uptodate = block_is_partially_uptodate, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3211 | }; |
| 3212 | |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3213 | static const struct address_space_operations ext4_da_aops = { |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3214 | .readpage = ext4_readpage, |
| 3215 | .readpages = ext4_readpages, |
| 3216 | .writepage = ext4_da_writepage, |
| 3217 | .writepages = ext4_da_writepages, |
| 3218 | .sync_page = block_sync_page, |
| 3219 | .write_begin = ext4_da_write_begin, |
| 3220 | .write_end = ext4_da_write_end, |
| 3221 | .bmap = ext4_bmap, |
| 3222 | .invalidatepage = ext4_da_invalidatepage, |
| 3223 | .releasepage = ext4_releasepage, |
| 3224 | .direct_IO = ext4_direct_IO, |
| 3225 | .migratepage = buffer_migrate_page, |
| 3226 | .is_partially_uptodate = block_is_partially_uptodate, |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3227 | }; |
| 3228 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3229 | void ext4_set_aops(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3230 | { |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3231 | if (ext4_should_order_data(inode) && |
| 3232 | test_opt(inode->i_sb, DELALLOC)) |
| 3233 | inode->i_mapping->a_ops = &ext4_da_aops; |
| 3234 | else if (ext4_should_order_data(inode)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3235 | inode->i_mapping->a_ops = &ext4_ordered_aops; |
Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3236 | else if (ext4_should_writeback_data(inode) && |
| 3237 | test_opt(inode->i_sb, DELALLOC)) |
| 3238 | inode->i_mapping->a_ops = &ext4_da_aops; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3239 | else if (ext4_should_writeback_data(inode)) |
| 3240 | inode->i_mapping->a_ops = &ext4_writeback_aops; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3241 | else |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3242 | inode->i_mapping->a_ops = &ext4_journalled_aops; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3246 | * ext4_block_truncate_page() zeroes out a mapping from file offset `from' |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3247 | * up to the end of the block which corresponds to `from'. |
| 3248 | * This required during truncate. We need to physically zero the tail end |
| 3249 | * of that block so it doesn't yield old data if the file is later grown. |
| 3250 | */ |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3251 | int ext4_block_truncate_page(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3252 | struct address_space *mapping, loff_t from) |
| 3253 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3254 | ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3255 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3256 | unsigned blocksize, length, pos; |
| 3257 | ext4_lblk_t iblock; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3258 | struct inode *inode = mapping->host; |
| 3259 | struct buffer_head *bh; |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3260 | struct page *page; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3261 | int err = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3262 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3263 | page = grab_cache_page(mapping, from >> PAGE_CACHE_SHIFT); |
| 3264 | if (!page) |
| 3265 | return -EINVAL; |
| 3266 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3267 | blocksize = inode->i_sb->s_blocksize; |
| 3268 | length = blocksize - (offset & (blocksize - 1)); |
| 3269 | iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); |
| 3270 | |
| 3271 | /* |
| 3272 | * For "nobh" option, we can only work if we don't need to |
| 3273 | * read-in the page - otherwise we create buffers to do the IO. |
| 3274 | */ |
| 3275 | if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) && |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3276 | ext4_should_writeback_data(inode) && PageUptodate(page)) { |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 3277 | zero_user(page, offset, length); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3278 | set_page_dirty(page); |
| 3279 | goto unlock; |
| 3280 | } |
| 3281 | |
| 3282 | if (!page_has_buffers(page)) |
| 3283 | create_empty_buffers(page, blocksize, 0); |
| 3284 | |
| 3285 | /* Find the buffer that contains "offset" */ |
| 3286 | bh = page_buffers(page); |
| 3287 | pos = blocksize; |
| 3288 | while (offset >= pos) { |
| 3289 | bh = bh->b_this_page; |
| 3290 | iblock++; |
| 3291 | pos += blocksize; |
| 3292 | } |
| 3293 | |
| 3294 | err = 0; |
| 3295 | if (buffer_freed(bh)) { |
| 3296 | BUFFER_TRACE(bh, "freed: skip"); |
| 3297 | goto unlock; |
| 3298 | } |
| 3299 | |
| 3300 | if (!buffer_mapped(bh)) { |
| 3301 | BUFFER_TRACE(bh, "unmapped"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3302 | ext4_get_block(inode, iblock, bh, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3303 | /* unmapped? It's a hole - nothing to do */ |
| 3304 | if (!buffer_mapped(bh)) { |
| 3305 | BUFFER_TRACE(bh, "still unmapped"); |
| 3306 | goto unlock; |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | /* Ok, it's mapped. Make sure it's up-to-date */ |
| 3311 | if (PageUptodate(page)) |
| 3312 | set_buffer_uptodate(bh); |
| 3313 | |
| 3314 | if (!buffer_uptodate(bh)) { |
| 3315 | err = -EIO; |
| 3316 | ll_rw_block(READ, 1, &bh); |
| 3317 | wait_on_buffer(bh); |
| 3318 | /* Uhhuh. Read error. Complain and punt. */ |
| 3319 | if (!buffer_uptodate(bh)) |
| 3320 | goto unlock; |
| 3321 | } |
| 3322 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3323 | if (ext4_should_journal_data(inode)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3324 | BUFFER_TRACE(bh, "get write access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3325 | err = ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3326 | if (err) |
| 3327 | goto unlock; |
| 3328 | } |
| 3329 | |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 3330 | zero_user(page, offset, length); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3331 | |
| 3332 | BUFFER_TRACE(bh, "zeroed end of block"); |
| 3333 | |
| 3334 | err = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3335 | if (ext4_should_journal_data(inode)) { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3336 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3337 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3338 | if (ext4_should_order_data(inode)) |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3339 | err = ext4_jbd2_file_inode(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3340 | mark_buffer_dirty(bh); |
| 3341 | } |
| 3342 | |
| 3343 | unlock: |
| 3344 | unlock_page(page); |
| 3345 | page_cache_release(page); |
| 3346 | return err; |
| 3347 | } |
| 3348 | |
| 3349 | /* |
| 3350 | * Probably it should be a library function... search for first non-zero word |
| 3351 | * or memcmp with zero_page, whatever is better for particular architecture. |
| 3352 | * Linus? |
| 3353 | */ |
| 3354 | static inline int all_zeroes(__le32 *p, __le32 *q) |
| 3355 | { |
| 3356 | while (p < q) |
| 3357 | if (*p++) |
| 3358 | return 0; |
| 3359 | return 1; |
| 3360 | } |
| 3361 | |
| 3362 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3363 | * ext4_find_shared - find the indirect blocks for partial truncation. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3364 | * @inode: inode in question |
| 3365 | * @depth: depth of the affected branch |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3366 | * @offsets: offsets of pointers in that branch (see ext4_block_to_path) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3367 | * @chain: place to store the pointers to partial indirect blocks |
| 3368 | * @top: place to the (detached) top of branch |
| 3369 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3370 | * This is a helper function used by ext4_truncate(). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3371 | * |
| 3372 | * When we do truncate() we may have to clean the ends of several |
| 3373 | * indirect blocks but leave the blocks themselves alive. Block is |
| 3374 | * partially truncated if some data below the new i_size is refered |
| 3375 | * from it (and it is on the path to the first completely truncated |
| 3376 | * data block, indeed). We have to free the top of that path along |
| 3377 | * with everything to the right of the path. Since no allocation |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3378 | * past the truncation point is possible until ext4_truncate() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3379 | * finishes, we may safely do the latter, but top of branch may |
| 3380 | * require special attention - pageout below the truncation point |
| 3381 | * might try to populate it. |
| 3382 | * |
| 3383 | * We atomically detach the top of branch from the tree, store the |
| 3384 | * block number of its root in *@top, pointers to buffer_heads of |
| 3385 | * partially truncated blocks - in @chain[].bh and pointers to |
| 3386 | * their last elements that should not be removed - in |
| 3387 | * @chain[].p. Return value is the pointer to last filled element |
| 3388 | * of @chain. |
| 3389 | * |
| 3390 | * The work left to caller to do the actual freeing of subtrees: |
| 3391 | * a) free the subtree starting from *@top |
| 3392 | * b) free the subtrees whose roots are stored in |
| 3393 | * (@chain[i].p+1 .. end of @chain[i].bh->b_data) |
| 3394 | * c) free the subtrees growing from the inode past the @chain[0]. |
| 3395 | * (no partially truncated stuff there). */ |
| 3396 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3397 | static Indirect *ext4_find_shared(struct inode *inode, int depth, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3398 | ext4_lblk_t offsets[4], Indirect chain[4], __le32 *top) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3399 | { |
| 3400 | Indirect *partial, *p; |
| 3401 | int k, err; |
| 3402 | |
| 3403 | *top = 0; |
| 3404 | /* Make k index the deepest non-null offest + 1 */ |
| 3405 | for (k = depth; k > 1 && !offsets[k-1]; k--) |
| 3406 | ; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3407 | partial = ext4_get_branch(inode, k, offsets, chain, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3408 | /* Writer: pointers */ |
| 3409 | if (!partial) |
| 3410 | partial = chain + k-1; |
| 3411 | /* |
| 3412 | * If the branch acquired continuation since we've looked at it - |
| 3413 | * fine, it should all survive and (new) top doesn't belong to us. |
| 3414 | */ |
| 3415 | if (!partial->key && *partial->p) |
| 3416 | /* Writer: end */ |
| 3417 | goto no_top; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3418 | 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] | 3419 | ; |
| 3420 | /* |
| 3421 | * OK, we've found the last block that must survive. The rest of our |
| 3422 | * branch should be detached before unlocking. However, if that rest |
| 3423 | * of branch is all ours and does not grow immediately from the inode |
| 3424 | * it's easier to cheat and just decrement partial->p. |
| 3425 | */ |
| 3426 | if (p == chain + k - 1 && p > chain) { |
| 3427 | p->p--; |
| 3428 | } else { |
| 3429 | *top = *p->p; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3430 | /* Nope, don't do this in ext4. Must leave the tree intact */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3431 | #if 0 |
| 3432 | *p->p = 0; |
| 3433 | #endif |
| 3434 | } |
| 3435 | /* Writer: end */ |
| 3436 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3437 | while (partial > p) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3438 | brelse(partial->bh); |
| 3439 | partial--; |
| 3440 | } |
| 3441 | no_top: |
| 3442 | return partial; |
| 3443 | } |
| 3444 | |
| 3445 | /* |
| 3446 | * Zero a number of block pointers in either an inode or an indirect block. |
| 3447 | * If we restart the transaction we must again get write access to the |
| 3448 | * indirect block for further modification. |
| 3449 | * |
| 3450 | * We release `count' blocks on disk, but (last - first) may be greater |
| 3451 | * than `count' because there can be holes in there. |
| 3452 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3453 | static void ext4_clear_blocks(handle_t *handle, struct inode *inode, |
| 3454 | struct buffer_head *bh, ext4_fsblk_t block_to_free, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3455 | unsigned long count, __le32 *first, __le32 *last) |
| 3456 | { |
| 3457 | __le32 *p; |
| 3458 | if (try_to_extend_transaction(handle, inode)) { |
| 3459 | if (bh) { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3460 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
| 3461 | ext4_handle_dirty_metadata(handle, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3462 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3463 | ext4_mark_inode_dirty(handle, inode); |
| 3464 | ext4_journal_test_restart(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3465 | if (bh) { |
| 3466 | BUFFER_TRACE(bh, "retaking write access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3467 | ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | /* |
| 3472 | * Any buffers which are on the journal will be in memory. We find |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3473 | * them on the hash table so jbd2_journal_revoke() will run jbd2_journal_forget() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3474 | * on them. We've already detached each block from the file, so |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3475 | * bforget() in jbd2_journal_forget() should be safe. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3476 | * |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3477 | * AKPM: turn on bforget in jbd2_journal_forget()!!! |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3478 | */ |
| 3479 | for (p = first; p < last; p++) { |
| 3480 | u32 nr = le32_to_cpu(*p); |
| 3481 | if (nr) { |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3482 | struct buffer_head *tbh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3483 | |
| 3484 | *p = 0; |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3485 | tbh = sb_find_get_block(inode->i_sb, nr); |
| 3486 | ext4_forget(handle, 0, inode, tbh, nr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3487 | } |
| 3488 | } |
| 3489 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3490 | ext4_free_blocks(handle, inode, block_to_free, count, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3494 | * ext4_free_data - free a list of data blocks |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3495 | * @handle: handle for this transaction |
| 3496 | * @inode: inode we are dealing with |
| 3497 | * @this_bh: indirect buffer_head which contains *@first and *@last |
| 3498 | * @first: array of block numbers |
| 3499 | * @last: points immediately past the end of array |
| 3500 | * |
| 3501 | * We are freeing all blocks refered from that array (numbers are stored as |
| 3502 | * little-endian 32-bit) and updating @inode->i_blocks appropriately. |
| 3503 | * |
| 3504 | * We accumulate contiguous runs of blocks to free. Conveniently, if these |
| 3505 | * blocks are contiguous then releasing them at one time will only affect one |
| 3506 | * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't |
| 3507 | * actually use a lot of journal space. |
| 3508 | * |
| 3509 | * @this_bh will be %NULL if @first and @last point into the inode's direct |
| 3510 | * block pointers. |
| 3511 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3512 | static void ext4_free_data(handle_t *handle, struct inode *inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3513 | struct buffer_head *this_bh, |
| 3514 | __le32 *first, __le32 *last) |
| 3515 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3516 | ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3517 | unsigned long count = 0; /* Number of blocks in the run */ |
| 3518 | __le32 *block_to_free_p = NULL; /* Pointer into inode/ind |
| 3519 | corresponding to |
| 3520 | block_to_free */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3521 | ext4_fsblk_t nr; /* Current block # */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3522 | __le32 *p; /* Pointer into inode/ind |
| 3523 | for current block */ |
| 3524 | int err; |
| 3525 | |
| 3526 | if (this_bh) { /* For indirect block */ |
| 3527 | BUFFER_TRACE(this_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3528 | err = ext4_journal_get_write_access(handle, this_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3529 | /* Important: if we can't update the indirect pointers |
| 3530 | * to the blocks, we can't free them. */ |
| 3531 | if (err) |
| 3532 | return; |
| 3533 | } |
| 3534 | |
| 3535 | for (p = first; p < last; p++) { |
| 3536 | nr = le32_to_cpu(*p); |
| 3537 | if (nr) { |
| 3538 | /* accumulate blocks to free if they're contiguous */ |
| 3539 | if (count == 0) { |
| 3540 | block_to_free = nr; |
| 3541 | block_to_free_p = p; |
| 3542 | count = 1; |
| 3543 | } else if (nr == block_to_free + count) { |
| 3544 | count++; |
| 3545 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3546 | ext4_clear_blocks(handle, inode, this_bh, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3547 | block_to_free, |
| 3548 | count, block_to_free_p, p); |
| 3549 | block_to_free = nr; |
| 3550 | block_to_free_p = p; |
| 3551 | count = 1; |
| 3552 | } |
| 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | if (count > 0) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3557 | ext4_clear_blocks(handle, inode, this_bh, block_to_free, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3558 | count, block_to_free_p, p); |
| 3559 | |
| 3560 | if (this_bh) { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3561 | BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata"); |
Duane Griffin | 71dc8fb | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3562 | |
| 3563 | /* |
| 3564 | * The buffer head should have an attached journal head at this |
| 3565 | * point. However, if the data is corrupted and an indirect |
| 3566 | * block pointed to itself, it would have been detached when |
| 3567 | * the block was cleared. Check for this instead of OOPSing. |
| 3568 | */ |
| 3569 | if (bh2jh(this_bh)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3570 | ext4_handle_dirty_metadata(handle, inode, this_bh); |
Duane Griffin | 71dc8fb | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3571 | else |
| 3572 | ext4_error(inode->i_sb, __func__, |
| 3573 | "circular indirect block detected, " |
| 3574 | "inode=%lu, block=%llu", |
| 3575 | inode->i_ino, |
| 3576 | (unsigned long long) this_bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3577 | } |
| 3578 | } |
| 3579 | |
| 3580 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3581 | * ext4_free_branches - free an array of branches |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3582 | * @handle: JBD handle for this transaction |
| 3583 | * @inode: inode we are dealing with |
| 3584 | * @parent_bh: the buffer_head which contains *@first and *@last |
| 3585 | * @first: array of block numbers |
| 3586 | * @last: pointer immediately past the end of array |
| 3587 | * @depth: depth of the branches to free |
| 3588 | * |
| 3589 | * We are freeing all blocks refered from these branches (numbers are |
| 3590 | * stored as little-endian 32-bit) and updating @inode->i_blocks |
| 3591 | * appropriately. |
| 3592 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3593 | static void ext4_free_branches(handle_t *handle, struct inode *inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3594 | struct buffer_head *parent_bh, |
| 3595 | __le32 *first, __le32 *last, int depth) |
| 3596 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3597 | ext4_fsblk_t nr; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3598 | __le32 *p; |
| 3599 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3600 | if (ext4_handle_is_aborted(handle)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3601 | return; |
| 3602 | |
| 3603 | if (depth--) { |
| 3604 | struct buffer_head *bh; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3605 | int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3606 | p = last; |
| 3607 | while (--p >= first) { |
| 3608 | nr = le32_to_cpu(*p); |
| 3609 | if (!nr) |
| 3610 | continue; /* A hole */ |
| 3611 | |
| 3612 | /* Go read the buffer for the next level down */ |
| 3613 | bh = sb_bread(inode->i_sb, nr); |
| 3614 | |
| 3615 | /* |
| 3616 | * A read failure? Report error and clear slot |
| 3617 | * (should be rare). |
| 3618 | */ |
| 3619 | if (!bh) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3620 | ext4_error(inode->i_sb, "ext4_free_branches", |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 3621 | "Read failure, inode=%lu, block=%llu", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3622 | inode->i_ino, nr); |
| 3623 | continue; |
| 3624 | } |
| 3625 | |
| 3626 | /* This zaps the entire block. Bottom up. */ |
| 3627 | BUFFER_TRACE(bh, "free child branches"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3628 | ext4_free_branches(handle, inode, bh, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3629 | (__le32 *) bh->b_data, |
| 3630 | (__le32 *) bh->b_data + addr_per_block, |
| 3631 | depth); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3632 | |
| 3633 | /* |
| 3634 | * We've probably journalled the indirect block several |
| 3635 | * times during the truncate. But it's no longer |
| 3636 | * needed and we now drop it from the transaction via |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3637 | * jbd2_journal_revoke(). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3638 | * |
| 3639 | * That's easy if it's exclusively part of this |
| 3640 | * transaction. But if it's part of the committing |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3641 | * transaction then jbd2_journal_forget() will simply |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3642 | * brelse() it. That means that if the underlying |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3643 | * block is reallocated in ext4_get_block(), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3644 | * unmap_underlying_metadata() will find this block |
| 3645 | * and will try to get rid of it. damn, damn. |
| 3646 | * |
| 3647 | * If this block has already been committed to the |
| 3648 | * journal, a revoke record will be written. And |
| 3649 | * revoke records must be emitted *before* clearing |
| 3650 | * this block's bit in the bitmaps. |
| 3651 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3652 | ext4_forget(handle, 1, inode, bh, bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3653 | |
| 3654 | /* |
| 3655 | * Everything below this this pointer has been |
| 3656 | * released. Now let this top-of-subtree go. |
| 3657 | * |
| 3658 | * We want the freeing of this indirect block to be |
| 3659 | * atomic in the journal with the updating of the |
| 3660 | * bitmap block which owns it. So make some room in |
| 3661 | * the journal. |
| 3662 | * |
| 3663 | * We zero the parent pointer *after* freeing its |
| 3664 | * pointee in the bitmaps, so if extend_transaction() |
| 3665 | * for some reason fails to put the bitmap changes and |
| 3666 | * the release into the same transaction, recovery |
| 3667 | * will merely complain about releasing a free block, |
| 3668 | * rather than leaking blocks. |
| 3669 | */ |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3670 | if (ext4_handle_is_aborted(handle)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3671 | return; |
| 3672 | if (try_to_extend_transaction(handle, inode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3673 | ext4_mark_inode_dirty(handle, inode); |
| 3674 | ext4_journal_test_restart(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3675 | } |
| 3676 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3677 | ext4_free_blocks(handle, inode, nr, 1, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3678 | |
| 3679 | if (parent_bh) { |
| 3680 | /* |
| 3681 | * The block which we have just freed is |
| 3682 | * pointed to by an indirect block: journal it |
| 3683 | */ |
| 3684 | BUFFER_TRACE(parent_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3685 | if (!ext4_journal_get_write_access(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3686 | parent_bh)){ |
| 3687 | *p = 0; |
| 3688 | BUFFER_TRACE(parent_bh, |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3689 | "call ext4_handle_dirty_metadata"); |
| 3690 | ext4_handle_dirty_metadata(handle, |
| 3691 | inode, |
| 3692 | parent_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3693 | } |
| 3694 | } |
| 3695 | } |
| 3696 | } else { |
| 3697 | /* We have reached the bottom of the tree. */ |
| 3698 | BUFFER_TRACE(parent_bh, "free data blocks"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3699 | ext4_free_data(handle, inode, parent_bh, first, last); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3703 | int ext4_can_truncate(struct inode *inode) |
| 3704 | { |
| 3705 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 3706 | return 0; |
| 3707 | if (S_ISREG(inode->i_mode)) |
| 3708 | return 1; |
| 3709 | if (S_ISDIR(inode->i_mode)) |
| 3710 | return 1; |
| 3711 | if (S_ISLNK(inode->i_mode)) |
| 3712 | return !ext4_inode_is_fast_symlink(inode); |
| 3713 | return 0; |
| 3714 | } |
| 3715 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3716 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3717 | * ext4_truncate() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3718 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3719 | * We block out ext4_get_block() block instantiations across the entire |
| 3720 | * transaction, and VFS/VM ensures that ext4_truncate() cannot run |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3721 | * simultaneously on behalf of the same inode. |
| 3722 | * |
| 3723 | * As we work through the truncate and commmit bits of it to the journal there |
| 3724 | * is one core, guiding principle: the file's tree must always be consistent on |
| 3725 | * disk. We must be able to restart the truncate after a crash. |
| 3726 | * |
| 3727 | * The file's tree may be transiently inconsistent in memory (although it |
| 3728 | * probably isn't), but whenever we close off and commit a journal transaction, |
| 3729 | * the contents of (the filesystem + the journal) must be consistent and |
| 3730 | * restartable. It's pretty simple, really: bottom up, right to left (although |
| 3731 | * left-to-right works OK too). |
| 3732 | * |
| 3733 | * Note that at recovery time, journal replay occurs *before* the restart of |
| 3734 | * truncate against the orphan inode list. |
| 3735 | * |
| 3736 | * 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] | 3737 | * 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] | 3738 | * 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] | 3739 | * ext4_truncate() to have another go. So there will be instantiated blocks |
| 3740 | * 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] | 3741 | * 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] | 3742 | * ext4_truncate() run will find them and release them. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3743 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3744 | void ext4_truncate(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3745 | { |
| 3746 | handle_t *handle; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3747 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3748 | __le32 *i_data = ei->i_data; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3749 | int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3750 | struct address_space *mapping = inode->i_mapping; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3751 | ext4_lblk_t offsets[4]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3752 | Indirect chain[4]; |
| 3753 | Indirect *partial; |
| 3754 | __le32 nr = 0; |
| 3755 | int n; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3756 | ext4_lblk_t last_block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3757 | unsigned blocksize = inode->i_sb->s_blocksize; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3758 | |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3759 | if (!ext4_can_truncate(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3760 | return; |
| 3761 | |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3762 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3763 | ext4_ext_truncate(inode); |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3764 | return; |
| 3765 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3766 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3767 | handle = start_transaction(inode); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3768 | if (IS_ERR(handle)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3769 | return; /* AKPM: return what? */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3770 | |
| 3771 | last_block = (inode->i_size + blocksize-1) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3772 | >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3773 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3774 | if (inode->i_size & (blocksize - 1)) |
| 3775 | if (ext4_block_truncate_page(handle, mapping, inode->i_size)) |
| 3776 | goto out_stop; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3777 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3778 | n = ext4_block_to_path(inode, last_block, offsets, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3779 | if (n == 0) |
| 3780 | goto out_stop; /* error */ |
| 3781 | |
| 3782 | /* |
| 3783 | * OK. This truncate is going to happen. We add the inode to the |
| 3784 | * orphan list, so that if this truncate spans multiple transactions, |
| 3785 | * and we crash, we will resume the truncate when the filesystem |
| 3786 | * recovers. It also marks the inode dirty, to catch the new size. |
| 3787 | * |
| 3788 | * Implication: the file must always be in a sane, consistent |
| 3789 | * truncatable state while each transaction commits. |
| 3790 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3791 | if (ext4_orphan_add(handle, inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3792 | goto out_stop; |
| 3793 | |
| 3794 | /* |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3795 | * From here we block out all ext4_get_block() callers who want to |
| 3796 | * modify the block allocation tree. |
| 3797 | */ |
| 3798 | down_write(&ei->i_data_sem); |
Theodore Ts'o | b4df203 | 2008-08-13 21:44:34 -0400 | [diff] [blame] | 3799 | |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 3800 | ext4_discard_preallocations(inode); |
Theodore Ts'o | b4df203 | 2008-08-13 21:44:34 -0400 | [diff] [blame] | 3801 | |
Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3802 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3803 | * The orphan list entry will now protect us from any crash which |
| 3804 | * occurs before the truncate completes, so it is now safe to propagate |
| 3805 | * the new, shorter inode size (held for now in i_size) into the |
| 3806 | * 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] | 3807 | * ext4 *really* writes onto the disk inode. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3808 | */ |
| 3809 | ei->i_disksize = inode->i_size; |
| 3810 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3811 | if (n == 1) { /* direct blocks */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3812 | ext4_free_data(handle, inode, NULL, i_data+offsets[0], |
| 3813 | i_data + EXT4_NDIR_BLOCKS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3814 | goto do_indirects; |
| 3815 | } |
| 3816 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3817 | partial = ext4_find_shared(inode, n, offsets, chain, &nr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3818 | /* Kill the top of shared branch (not detached) */ |
| 3819 | if (nr) { |
| 3820 | if (partial == chain) { |
| 3821 | /* Shared branch grows from the inode */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3822 | ext4_free_branches(handle, inode, NULL, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3823 | &nr, &nr+1, (chain+n-1) - partial); |
| 3824 | *partial->p = 0; |
| 3825 | /* |
| 3826 | * We mark the inode dirty prior to restart, |
| 3827 | * and prior to stop. No need for it here. |
| 3828 | */ |
| 3829 | } else { |
| 3830 | /* Shared branch grows from an indirect block */ |
| 3831 | BUFFER_TRACE(partial->bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3832 | ext4_free_branches(handle, inode, partial->bh, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3833 | partial->p, |
| 3834 | partial->p+1, (chain+n-1) - partial); |
| 3835 | } |
| 3836 | } |
| 3837 | /* Clear the ends of indirect blocks on the shared branch */ |
| 3838 | while (partial > chain) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3839 | ext4_free_branches(handle, inode, partial->bh, partial->p + 1, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3840 | (__le32*)partial->bh->b_data+addr_per_block, |
| 3841 | (chain+n-1) - partial); |
| 3842 | BUFFER_TRACE(partial->bh, "call brelse"); |
| 3843 | brelse (partial->bh); |
| 3844 | partial--; |
| 3845 | } |
| 3846 | do_indirects: |
| 3847 | /* Kill the remaining (whole) subtrees */ |
| 3848 | switch (offsets[0]) { |
| 3849 | default: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3850 | nr = i_data[EXT4_IND_BLOCK]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3851 | if (nr) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3852 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); |
| 3853 | i_data[EXT4_IND_BLOCK] = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3854 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3855 | case EXT4_IND_BLOCK: |
| 3856 | nr = i_data[EXT4_DIND_BLOCK]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3857 | if (nr) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3858 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); |
| 3859 | i_data[EXT4_DIND_BLOCK] = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3860 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3861 | case EXT4_DIND_BLOCK: |
| 3862 | nr = i_data[EXT4_TIND_BLOCK]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3863 | if (nr) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3864 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); |
| 3865 | i_data[EXT4_TIND_BLOCK] = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3866 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3867 | case EXT4_TIND_BLOCK: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3868 | ; |
| 3869 | } |
| 3870 | |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3871 | up_write(&ei->i_data_sem); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 3872 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3873 | ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3874 | |
| 3875 | /* |
| 3876 | * In a multi-transaction truncate, we only make the final transaction |
| 3877 | * synchronous |
| 3878 | */ |
| 3879 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3880 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3881 | out_stop: |
| 3882 | /* |
| 3883 | * If this was a simple ftruncate(), and the file will remain alive |
| 3884 | * then we need to clear up the orphan record which we created above. |
| 3885 | * However, if this was a real unlink then we were called by |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3886 | * ext4_delete_inode(), and we allow that function to clean up the |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3887 | * orphan info for us. |
| 3888 | */ |
| 3889 | if (inode->i_nlink) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3890 | ext4_orphan_del(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3891 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3892 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3893 | } |
| 3894 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3895 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3896 | * 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] | 3897 | * underlying buffer_head on success. If 'in_mem' is true, we have all |
| 3898 | * data in memory that is needed to recreate the on-disk version of this |
| 3899 | * inode. |
| 3900 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3901 | static int __ext4_get_inode_loc(struct inode *inode, |
| 3902 | struct ext4_iloc *iloc, int in_mem) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3903 | { |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3904 | struct ext4_group_desc *gdp; |
| 3905 | struct buffer_head *bh; |
| 3906 | struct super_block *sb = inode->i_sb; |
| 3907 | ext4_fsblk_t block; |
| 3908 | int inodes_per_block, inode_offset; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3909 | |
Aneesh Kumar K.V | 3a06d77 | 2008-11-22 15:04:59 -0500 | [diff] [blame^] | 3910 | iloc->bh = NULL; |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3911 | if (!ext4_valid_inum(sb, inode->i_ino)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3912 | return -EIO; |
| 3913 | |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3914 | iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 3915 | gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); |
| 3916 | if (!gdp) |
| 3917 | return -EIO; |
| 3918 | |
| 3919 | /* |
| 3920 | * Figure out the offset within the block group inode table |
| 3921 | */ |
| 3922 | inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb)); |
| 3923 | inode_offset = ((inode->i_ino - 1) % |
| 3924 | EXT4_INODES_PER_GROUP(sb)); |
| 3925 | block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); |
| 3926 | iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); |
| 3927 | |
| 3928 | bh = sb_getblk(sb, block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3929 | if (!bh) { |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3930 | ext4_error(sb, "ext4_get_inode_loc", "unable to read " |
| 3931 | "inode block - inode=%lu, block=%llu", |
| 3932 | inode->i_ino, block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3933 | return -EIO; |
| 3934 | } |
| 3935 | if (!buffer_uptodate(bh)) { |
| 3936 | lock_buffer(bh); |
Hidehiro Kawai | 9c83a92 | 2008-07-26 16:39:26 -0400 | [diff] [blame] | 3937 | |
| 3938 | /* |
| 3939 | * If the buffer has the write error flag, we have failed |
| 3940 | * to write out another inode in the same block. In this |
| 3941 | * case, we don't have to read the block because we may |
| 3942 | * read the old inode data successfully. |
| 3943 | */ |
| 3944 | if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) |
| 3945 | set_buffer_uptodate(bh); |
| 3946 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3947 | if (buffer_uptodate(bh)) { |
| 3948 | /* someone brought it uptodate while we waited */ |
| 3949 | unlock_buffer(bh); |
| 3950 | goto has_buffer; |
| 3951 | } |
| 3952 | |
| 3953 | /* |
| 3954 | * If we have all information of the inode in memory and this |
| 3955 | * is the only valid inode in the block, we need not read the |
| 3956 | * block. |
| 3957 | */ |
| 3958 | if (in_mem) { |
| 3959 | struct buffer_head *bitmap_bh; |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3960 | int i, start; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3961 | |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3962 | start = inode_offset & ~(inodes_per_block - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3963 | |
| 3964 | /* Is the inode bitmap in cache? */ |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3965 | bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3966 | if (!bitmap_bh) |
| 3967 | goto make_io; |
| 3968 | |
| 3969 | /* |
| 3970 | * If the inode bitmap isn't in cache then the |
| 3971 | * optimisation may end up performing two reads instead |
| 3972 | * of one, so skip it. |
| 3973 | */ |
| 3974 | if (!buffer_uptodate(bitmap_bh)) { |
| 3975 | brelse(bitmap_bh); |
| 3976 | goto make_io; |
| 3977 | } |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3978 | for (i = start; i < start + inodes_per_block; i++) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3979 | if (i == inode_offset) |
| 3980 | continue; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3981 | if (ext4_test_bit(i, bitmap_bh->b_data)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3982 | break; |
| 3983 | } |
| 3984 | brelse(bitmap_bh); |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3985 | if (i == start + inodes_per_block) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3986 | /* all other inodes are free, so skip I/O */ |
| 3987 | memset(bh->b_data, 0, bh->b_size); |
| 3988 | set_buffer_uptodate(bh); |
| 3989 | unlock_buffer(bh); |
| 3990 | goto has_buffer; |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | make_io: |
| 3995 | /* |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 3996 | * If we need to do any I/O, try to pre-readahead extra |
| 3997 | * blocks from the inode table. |
| 3998 | */ |
| 3999 | if (EXT4_SB(sb)->s_inode_readahead_blks) { |
| 4000 | ext4_fsblk_t b, end, table; |
| 4001 | unsigned num; |
| 4002 | |
| 4003 | table = ext4_inode_table(sb, gdp); |
| 4004 | /* Make sure s_inode_readahead_blks is a power of 2 */ |
| 4005 | while (EXT4_SB(sb)->s_inode_readahead_blks & |
| 4006 | (EXT4_SB(sb)->s_inode_readahead_blks-1)) |
| 4007 | EXT4_SB(sb)->s_inode_readahead_blks = |
| 4008 | (EXT4_SB(sb)->s_inode_readahead_blks & |
| 4009 | (EXT4_SB(sb)->s_inode_readahead_blks-1)); |
| 4010 | b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); |
| 4011 | if (table > b) |
| 4012 | b = table; |
| 4013 | end = b + EXT4_SB(sb)->s_inode_readahead_blks; |
| 4014 | num = EXT4_INODES_PER_GROUP(sb); |
| 4015 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 4016 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) |
| 4017 | num -= le16_to_cpu(gdp->bg_itable_unused); |
| 4018 | table += num / inodes_per_block; |
| 4019 | if (end > table) |
| 4020 | end = table; |
| 4021 | while (b <= end) |
| 4022 | sb_breadahead(sb, b++); |
| 4023 | } |
| 4024 | |
| 4025 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4026 | * There are other valid inodes in the buffer, this inode |
| 4027 | * has in-inode xattrs, or we don't have this inode in memory. |
| 4028 | * Read the block from disk. |
| 4029 | */ |
| 4030 | get_bh(bh); |
| 4031 | bh->b_end_io = end_buffer_read_sync; |
| 4032 | submit_bh(READ_META, bh); |
| 4033 | wait_on_buffer(bh); |
| 4034 | if (!buffer_uptodate(bh)) { |
Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4035 | ext4_error(sb, __func__, |
| 4036 | "unable to read inode block - inode=%lu, " |
| 4037 | "block=%llu", inode->i_ino, block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4038 | brelse(bh); |
| 4039 | return -EIO; |
| 4040 | } |
| 4041 | } |
| 4042 | has_buffer: |
| 4043 | iloc->bh = bh; |
| 4044 | return 0; |
| 4045 | } |
| 4046 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4047 | int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4048 | { |
| 4049 | /* We have all inode data except xattrs in memory here. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4050 | return __ext4_get_inode_loc(inode, iloc, |
| 4051 | !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4052 | } |
| 4053 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4054 | void ext4_set_inode_flags(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4055 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4056 | unsigned int flags = EXT4_I(inode)->i_flags; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4057 | |
| 4058 | 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] | 4059 | if (flags & EXT4_SYNC_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4060 | inode->i_flags |= S_SYNC; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4061 | if (flags & EXT4_APPEND_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4062 | inode->i_flags |= S_APPEND; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4063 | if (flags & EXT4_IMMUTABLE_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4064 | inode->i_flags |= S_IMMUTABLE; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4065 | if (flags & EXT4_NOATIME_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4066 | inode->i_flags |= S_NOATIME; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4067 | if (flags & EXT4_DIRSYNC_FL) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4068 | inode->i_flags |= S_DIRSYNC; |
| 4069 | } |
| 4070 | |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 4071 | /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ |
| 4072 | void ext4_get_inode_flags(struct ext4_inode_info *ei) |
| 4073 | { |
| 4074 | unsigned int flags = ei->vfs_inode.i_flags; |
| 4075 | |
| 4076 | ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL| |
| 4077 | EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL); |
| 4078 | if (flags & S_SYNC) |
| 4079 | ei->i_flags |= EXT4_SYNC_FL; |
| 4080 | if (flags & S_APPEND) |
| 4081 | ei->i_flags |= EXT4_APPEND_FL; |
| 4082 | if (flags & S_IMMUTABLE) |
| 4083 | ei->i_flags |= EXT4_IMMUTABLE_FL; |
| 4084 | if (flags & S_NOATIME) |
| 4085 | ei->i_flags |= EXT4_NOATIME_FL; |
| 4086 | if (flags & S_DIRSYNC) |
| 4087 | ei->i_flags |= EXT4_DIRSYNC_FL; |
| 4088 | } |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4089 | static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, |
| 4090 | struct ext4_inode_info *ei) |
| 4091 | { |
| 4092 | blkcnt_t i_blocks ; |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4093 | struct inode *inode = &(ei->vfs_inode); |
| 4094 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4095 | |
| 4096 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 4097 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { |
| 4098 | /* we are using combined 48 bit field */ |
| 4099 | i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | |
| 4100 | le32_to_cpu(raw_inode->i_blocks_lo); |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4101 | if (ei->i_flags & EXT4_HUGE_FILE_FL) { |
| 4102 | /* i_blocks represent file system block size */ |
| 4103 | return i_blocks << (inode->i_blkbits - 9); |
| 4104 | } else { |
| 4105 | return i_blocks; |
| 4106 | } |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4107 | } else { |
| 4108 | return le32_to_cpu(raw_inode->i_blocks_lo); |
| 4109 | } |
| 4110 | } |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 4111 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4112 | struct inode *ext4_iget(struct super_block *sb, unsigned long ino) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4113 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4114 | struct ext4_iloc iloc; |
| 4115 | struct ext4_inode *raw_inode; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4116 | struct ext4_inode_info *ei; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4117 | struct buffer_head *bh; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4118 | struct inode *inode; |
| 4119 | long ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4120 | int block; |
| 4121 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4122 | inode = iget_locked(sb, ino); |
| 4123 | if (!inode) |
| 4124 | return ERR_PTR(-ENOMEM); |
| 4125 | if (!(inode->i_state & I_NEW)) |
| 4126 | return inode; |
| 4127 | |
| 4128 | ei = EXT4_I(inode); |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 4129 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4130 | ei->i_acl = EXT4_ACL_NOT_CACHED; |
| 4131 | ei->i_default_acl = EXT4_ACL_NOT_CACHED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4132 | #endif |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4133 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4134 | ret = __ext4_get_inode_loc(inode, &iloc, 0); |
| 4135 | if (ret < 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4136 | goto bad_inode; |
| 4137 | bh = iloc.bh; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4138 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4139 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); |
| 4140 | inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); |
| 4141 | 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] | 4142 | if (!(test_opt(inode->i_sb, NO_UID32))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4143 | inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; |
| 4144 | inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; |
| 4145 | } |
| 4146 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4147 | |
| 4148 | ei->i_state = 0; |
| 4149 | ei->i_dir_start_lookup = 0; |
| 4150 | ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); |
| 4151 | /* We now have enough fields to check if the inode was active or not. |
| 4152 | * This is needed because nfsd might try to access dead inodes |
| 4153 | * the test is that same one that e2fsck uses |
| 4154 | * NeilBrown 1999oct15 |
| 4155 | */ |
| 4156 | if (inode->i_nlink == 0) { |
| 4157 | if (inode->i_mode == 0 || |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4158 | !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4159 | /* this inode is deleted */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4160 | brelse(bh); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4161 | ret = -ESTALE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4162 | goto bad_inode; |
| 4163 | } |
| 4164 | /* The only unlinked inodes we let through here have |
| 4165 | * valid i_mode and are being read by the orphan |
| 4166 | * recovery code: that's fine, we're about to complete |
| 4167 | * the process of deleting those. */ |
| 4168 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4169 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4170 | inode->i_blocks = ext4_inode_blocks(raw_inode, ei); |
Aneesh Kumar K.V | 7973c0c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4171 | ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 4172 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != |
Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4173 | cpu_to_le32(EXT4_OS_HURD)) { |
Badari Pulavarty | a1ddeb7 | 2006-10-11 01:21:09 -0700 | [diff] [blame] | 4174 | ei->i_file_acl |= |
| 4175 | ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4176 | } |
Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4177 | inode->i_size = ext4_isize(raw_inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4178 | ei->i_disksize = inode->i_size; |
| 4179 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); |
| 4180 | ei->i_block_group = iloc.block_group; |
| 4181 | /* |
| 4182 | * NOTE! The in-memory inode i_data array is in little-endian order |
| 4183 | * even on big-endian machines: we do NOT byteswap the block numbers! |
| 4184 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4185 | for (block = 0; block < EXT4_N_BLOCKS; block++) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4186 | ei->i_data[block] = raw_inode->i_block[block]; |
| 4187 | INIT_LIST_HEAD(&ei->i_orphan); |
| 4188 | |
Eric Sandeen | 0040d98 | 2008-02-05 22:36:43 -0500 | [diff] [blame] | 4189 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4190 | ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4191 | if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > |
Kirill Korotaev | e5d2861 | 2007-06-23 17:16:51 -0700 | [diff] [blame] | 4192 | EXT4_INODE_SIZE(inode->i_sb)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4193 | brelse(bh); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4194 | ret = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4195 | goto bad_inode; |
Kirill Korotaev | e5d2861 | 2007-06-23 17:16:51 -0700 | [diff] [blame] | 4196 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4197 | if (ei->i_extra_isize == 0) { |
| 4198 | /* The extra space is currently unused. Use it. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4199 | ei->i_extra_isize = sizeof(struct ext4_inode) - |
| 4200 | EXT4_GOOD_OLD_INODE_SIZE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4201 | } else { |
| 4202 | __le32 *magic = (void *)raw_inode + |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4203 | EXT4_GOOD_OLD_INODE_SIZE + |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4204 | ei->i_extra_isize; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4205 | if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) |
| 4206 | ei->i_state |= EXT4_STATE_XATTR; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4207 | } |
| 4208 | } else |
| 4209 | ei->i_extra_isize = 0; |
| 4210 | |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 4211 | EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); |
| 4212 | EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); |
| 4213 | EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); |
| 4214 | EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); |
| 4215 | |
Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4216 | inode->i_version = le32_to_cpu(raw_inode->i_disk_version); |
| 4217 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { |
| 4218 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) |
| 4219 | inode->i_version |= |
| 4220 | (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; |
| 4221 | } |
| 4222 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4223 | if (S_ISREG(inode->i_mode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4224 | inode->i_op = &ext4_file_inode_operations; |
| 4225 | inode->i_fop = &ext4_file_operations; |
| 4226 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4227 | } else if (S_ISDIR(inode->i_mode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4228 | inode->i_op = &ext4_dir_inode_operations; |
| 4229 | inode->i_fop = &ext4_dir_operations; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4230 | } else if (S_ISLNK(inode->i_mode)) { |
Duane Griffin | e83c139 | 2008-12-19 20:47:15 +0000 | [diff] [blame] | 4231 | if (ext4_inode_is_fast_symlink(inode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4232 | inode->i_op = &ext4_fast_symlink_inode_operations; |
Duane Griffin | e83c139 | 2008-12-19 20:47:15 +0000 | [diff] [blame] | 4233 | nd_terminate_link(ei->i_data, inode->i_size, |
| 4234 | sizeof(ei->i_data) - 1); |
| 4235 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4236 | inode->i_op = &ext4_symlink_inode_operations; |
| 4237 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4238 | } |
| 4239 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4240 | inode->i_op = &ext4_special_inode_operations; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4241 | if (raw_inode->i_block[0]) |
| 4242 | init_special_inode(inode, inode->i_mode, |
| 4243 | old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); |
| 4244 | else |
| 4245 | init_special_inode(inode, inode->i_mode, |
| 4246 | new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); |
| 4247 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4248 | brelse(iloc.bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4249 | ext4_set_inode_flags(inode); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4250 | unlock_new_inode(inode); |
| 4251 | return inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4252 | |
| 4253 | bad_inode: |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4254 | iget_failed(inode); |
| 4255 | return ERR_PTR(ret); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4258 | static int ext4_inode_blocks_set(handle_t *handle, |
| 4259 | struct ext4_inode *raw_inode, |
| 4260 | struct ext4_inode_info *ei) |
| 4261 | { |
| 4262 | struct inode *inode = &(ei->vfs_inode); |
| 4263 | u64 i_blocks = inode->i_blocks; |
| 4264 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4265 | |
| 4266 | if (i_blocks <= ~0U) { |
| 4267 | /* |
| 4268 | * i_blocks can be represnted in a 32 bit variable |
| 4269 | * as multiple of 512 bytes |
| 4270 | */ |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4271 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4272 | raw_inode->i_blocks_high = 0; |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4273 | ei->i_flags &= ~EXT4_HUGE_FILE_FL; |
Theodore Ts'o | f287a1a | 2008-10-16 22:50:48 -0400 | [diff] [blame] | 4274 | return 0; |
| 4275 | } |
| 4276 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) |
| 4277 | return -EFBIG; |
| 4278 | |
| 4279 | if (i_blocks <= 0xffffffffffffULL) { |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4280 | /* |
| 4281 | * i_blocks can be represented in a 48 bit variable |
| 4282 | * as multiple of 512 bytes |
| 4283 | */ |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4284 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4285 | 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] | 4286 | ei->i_flags &= ~EXT4_HUGE_FILE_FL; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4287 | } else { |
Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4288 | ei->i_flags |= EXT4_HUGE_FILE_FL; |
| 4289 | /* i_block is stored in file system block size */ |
| 4290 | i_blocks = i_blocks >> (inode->i_blkbits - 9); |
| 4291 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
| 4292 | 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] | 4293 | } |
Theodore Ts'o | f287a1a | 2008-10-16 22:50:48 -0400 | [diff] [blame] | 4294 | return 0; |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4295 | } |
| 4296 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4297 | /* |
| 4298 | * Post the struct inode info into an on-disk inode location in the |
| 4299 | * buffer-cache. This gobbles the caller's reference to the |
| 4300 | * buffer_head in the inode location struct. |
| 4301 | * |
| 4302 | * The caller must have write access to iloc->bh. |
| 4303 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4304 | static int ext4_do_update_inode(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4305 | struct inode *inode, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4306 | struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4307 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4308 | struct ext4_inode *raw_inode = ext4_raw_inode(iloc); |
| 4309 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4310 | struct buffer_head *bh = iloc->bh; |
| 4311 | int err = 0, rc, block; |
| 4312 | |
| 4313 | /* For fields not not tracking in the in-memory inode, |
| 4314 | * initialise them to zero for new inodes. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4315 | if (ei->i_state & EXT4_STATE_NEW) |
| 4316 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4317 | |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 4318 | ext4_get_inode_flags(ei); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4319 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4320 | if (!(test_opt(inode->i_sb, NO_UID32))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4321 | raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); |
| 4322 | raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); |
| 4323 | /* |
| 4324 | * Fix up interoperability with old kernels. Otherwise, old inodes get |
| 4325 | * re-used with the upper 16 bits of the uid/gid intact |
| 4326 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4327 | if (!ei->i_dtime) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4328 | raw_inode->i_uid_high = |
| 4329 | cpu_to_le16(high_16_bits(inode->i_uid)); |
| 4330 | raw_inode->i_gid_high = |
| 4331 | cpu_to_le16(high_16_bits(inode->i_gid)); |
| 4332 | } else { |
| 4333 | raw_inode->i_uid_high = 0; |
| 4334 | raw_inode->i_gid_high = 0; |
| 4335 | } |
| 4336 | } else { |
| 4337 | raw_inode->i_uid_low = |
| 4338 | cpu_to_le16(fs_high2lowuid(inode->i_uid)); |
| 4339 | raw_inode->i_gid_low = |
| 4340 | cpu_to_le16(fs_high2lowgid(inode->i_gid)); |
| 4341 | raw_inode->i_uid_high = 0; |
| 4342 | raw_inode->i_gid_high = 0; |
| 4343 | } |
| 4344 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 4345 | |
| 4346 | EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); |
| 4347 | EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); |
| 4348 | EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); |
| 4349 | EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); |
| 4350 | |
Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4351 | if (ext4_inode_blocks_set(handle, raw_inode, ei)) |
| 4352 | goto out_brelse; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4353 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); |
Aneesh Kumar K.V | 267e4db | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4354 | /* clear the migrate flag in the raw_inode */ |
| 4355 | raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 4356 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != |
| 4357 | cpu_to_le32(EXT4_OS_HURD)) |
Badari Pulavarty | a1ddeb7 | 2006-10-11 01:21:09 -0700 | [diff] [blame] | 4358 | raw_inode->i_file_acl_high = |
| 4359 | cpu_to_le16(ei->i_file_acl >> 32); |
Aneesh Kumar K.V | 7973c0c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4360 | 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] | 4361 | ext4_isize_set(raw_inode, ei->i_disksize); |
| 4362 | if (ei->i_disksize > 0x7fffffffULL) { |
| 4363 | struct super_block *sb = inode->i_sb; |
| 4364 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 4365 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || |
| 4366 | EXT4_SB(sb)->s_es->s_rev_level == |
| 4367 | cpu_to_le32(EXT4_GOOD_OLD_REV)) { |
| 4368 | /* If this is the first large file |
| 4369 | * created, add a flag to the superblock. |
| 4370 | */ |
| 4371 | err = ext4_journal_get_write_access(handle, |
| 4372 | EXT4_SB(sb)->s_sbh); |
| 4373 | if (err) |
| 4374 | goto out_brelse; |
| 4375 | ext4_update_dynamic_rev(sb); |
| 4376 | EXT4_SET_RO_COMPAT_FEATURE(sb, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4377 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE); |
Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4378 | sb->s_dirt = 1; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4379 | ext4_handle_sync(handle); |
| 4380 | err = ext4_handle_dirty_metadata(handle, inode, |
Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4381 | EXT4_SB(sb)->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4382 | } |
| 4383 | } |
| 4384 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); |
| 4385 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
| 4386 | if (old_valid_dev(inode->i_rdev)) { |
| 4387 | raw_inode->i_block[0] = |
| 4388 | cpu_to_le32(old_encode_dev(inode->i_rdev)); |
| 4389 | raw_inode->i_block[1] = 0; |
| 4390 | } else { |
| 4391 | raw_inode->i_block[0] = 0; |
| 4392 | raw_inode->i_block[1] = |
| 4393 | cpu_to_le32(new_encode_dev(inode->i_rdev)); |
| 4394 | raw_inode->i_block[2] = 0; |
| 4395 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4396 | } else for (block = 0; block < EXT4_N_BLOCKS; block++) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4397 | raw_inode->i_block[block] = ei->i_data[block]; |
| 4398 | |
Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4399 | raw_inode->i_disk_version = cpu_to_le32(inode->i_version); |
| 4400 | if (ei->i_extra_isize) { |
| 4401 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) |
| 4402 | raw_inode->i_version_hi = |
| 4403 | cpu_to_le32(inode->i_version >> 32); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4404 | 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] | 4405 | } |
| 4406 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4407 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
| 4408 | rc = ext4_handle_dirty_metadata(handle, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4409 | if (!err) |
| 4410 | err = rc; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4411 | ei->i_state &= ~EXT4_STATE_NEW; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4412 | |
| 4413 | out_brelse: |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4414 | brelse(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4415 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4416 | return err; |
| 4417 | } |
| 4418 | |
| 4419 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4420 | * ext4_write_inode() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4421 | * |
| 4422 | * We are called from a few places: |
| 4423 | * |
| 4424 | * - Within generic_file_write() for O_SYNC files. |
| 4425 | * Here, there will be no transaction running. We wait for any running |
| 4426 | * trasnaction to commit. |
| 4427 | * |
| 4428 | * - Within sys_sync(), kupdate and such. |
| 4429 | * We wait on commit, if tol to. |
| 4430 | * |
| 4431 | * - Within prune_icache() (PF_MEMALLOC == true) |
| 4432 | * Here we simply return. We can't afford to block kswapd on the |
| 4433 | * journal commit. |
| 4434 | * |
| 4435 | * In all cases it is actually safe for us to return without doing anything, |
| 4436 | * because the inode has been copied into a raw inode buffer in |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4437 | * 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] | 4438 | * knfsd. |
| 4439 | * |
| 4440 | * Note that we are absolutely dependent upon all inode dirtiers doing the |
| 4441 | * right thing: they *must* call mark_inode_dirty() after dirtying info in |
| 4442 | * which we are interested. |
| 4443 | * |
| 4444 | * It would be a bug for them to not do this. The code: |
| 4445 | * |
| 4446 | * mark_inode_dirty(inode) |
| 4447 | * stuff(); |
| 4448 | * inode->i_size = expr; |
| 4449 | * |
| 4450 | * is in error because a kswapd-driven write_inode() could occur while |
| 4451 | * `stuff()' is running, and the new i_size will be lost. Plus the inode |
| 4452 | * will no longer be on the superblock's dirty inode list. |
| 4453 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4454 | int ext4_write_inode(struct inode *inode, int wait) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4455 | { |
| 4456 | if (current->flags & PF_MEMALLOC) |
| 4457 | return 0; |
| 4458 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4459 | if (ext4_journal_current_handle()) { |
Mingming Cao | b38bd33 | 2007-07-19 01:48:35 -0700 | [diff] [blame] | 4460 | jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4461 | dump_stack(); |
| 4462 | return -EIO; |
| 4463 | } |
| 4464 | |
| 4465 | if (!wait) |
| 4466 | return 0; |
| 4467 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4468 | return ext4_force_commit(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4469 | } |
| 4470 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4471 | int __ext4_write_dirty_metadata(struct inode *inode, struct buffer_head *bh) |
| 4472 | { |
| 4473 | int err = 0; |
| 4474 | |
| 4475 | mark_buffer_dirty(bh); |
| 4476 | if (inode && inode_needs_sync(inode)) { |
| 4477 | sync_dirty_buffer(bh); |
| 4478 | if (buffer_req(bh) && !buffer_uptodate(bh)) { |
| 4479 | ext4_error(inode->i_sb, __func__, |
| 4480 | "IO error syncing inode, " |
| 4481 | "inode=%lu, block=%llu", |
| 4482 | inode->i_ino, |
| 4483 | (unsigned long long)bh->b_blocknr); |
| 4484 | err = -EIO; |
| 4485 | } |
| 4486 | } |
| 4487 | return err; |
| 4488 | } |
| 4489 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4490 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4491 | * ext4_setattr() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4492 | * |
| 4493 | * Called from notify_change. |
| 4494 | * |
| 4495 | * We want to trap VFS attempts to truncate the file as soon as |
| 4496 | * possible. In particular, we want to make sure that when the VFS |
| 4497 | * shrinks i_size, we put the inode on the orphan list and modify |
| 4498 | * i_disksize immediately, so that during the subsequent flushing of |
| 4499 | * dirty pages and freeing of disk blocks, we can guarantee that any |
| 4500 | * commit will leave the blocks being flushed in an unused state on |
| 4501 | * disk. (On recovery, the inode will get truncated and the blocks will |
| 4502 | * be freed, so we have a strong guarantee that no future commit will |
| 4503 | * leave these blocks visible to the user.) |
| 4504 | * |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4505 | * Another thing we have to assure is that if we are in ordered mode |
| 4506 | * and inode is still attached to the committing transaction, we must |
| 4507 | * we start writeout of all the dirty pages which are being truncated. |
| 4508 | * This way we are sure that all the data written in the previous |
| 4509 | * transaction are already on disk (truncate waits for pages under |
| 4510 | * writeback). |
| 4511 | * |
| 4512 | * Called with inode->i_mutex down. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4513 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4514 | int ext4_setattr(struct dentry *dentry, struct iattr *attr) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4515 | { |
| 4516 | struct inode *inode = dentry->d_inode; |
| 4517 | int error, rc = 0; |
| 4518 | const unsigned int ia_valid = attr->ia_valid; |
| 4519 | |
| 4520 | error = inode_change_ok(inode, attr); |
| 4521 | if (error) |
| 4522 | return error; |
| 4523 | |
| 4524 | if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || |
| 4525 | (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { |
| 4526 | handle_t *handle; |
| 4527 | |
| 4528 | /* (user+group)*(old+new) structure, inode write (sb, |
| 4529 | * inode block, ? - but truncate inode update has it) */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4530 | handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+ |
| 4531 | EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4532 | if (IS_ERR(handle)) { |
| 4533 | error = PTR_ERR(handle); |
| 4534 | goto err_out; |
| 4535 | } |
| 4536 | error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0; |
| 4537 | if (error) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4538 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4539 | return error; |
| 4540 | } |
| 4541 | /* Update corresponding info in inode so that everything is in |
| 4542 | * one transaction */ |
| 4543 | if (attr->ia_valid & ATTR_UID) |
| 4544 | inode->i_uid = attr->ia_uid; |
| 4545 | if (attr->ia_valid & ATTR_GID) |
| 4546 | inode->i_gid = attr->ia_gid; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4547 | error = ext4_mark_inode_dirty(handle, inode); |
| 4548 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4549 | } |
| 4550 | |
Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4551 | if (attr->ia_valid & ATTR_SIZE) { |
| 4552 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) { |
| 4553 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 4554 | |
| 4555 | if (attr->ia_size > sbi->s_bitmap_maxbytes) { |
| 4556 | error = -EFBIG; |
| 4557 | goto err_out; |
| 4558 | } |
| 4559 | } |
| 4560 | } |
| 4561 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4562 | if (S_ISREG(inode->i_mode) && |
| 4563 | attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) { |
| 4564 | handle_t *handle; |
| 4565 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4566 | handle = ext4_journal_start(inode, 3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4567 | if (IS_ERR(handle)) { |
| 4568 | error = PTR_ERR(handle); |
| 4569 | goto err_out; |
| 4570 | } |
| 4571 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4572 | error = ext4_orphan_add(handle, inode); |
| 4573 | EXT4_I(inode)->i_disksize = attr->ia_size; |
| 4574 | rc = ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4575 | if (!error) |
| 4576 | error = rc; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4577 | ext4_journal_stop(handle); |
Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4578 | |
| 4579 | if (ext4_should_order_data(inode)) { |
| 4580 | error = ext4_begin_ordered_truncate(inode, |
| 4581 | attr->ia_size); |
| 4582 | if (error) { |
| 4583 | /* Do as much error cleanup as possible */ |
| 4584 | handle = ext4_journal_start(inode, 3); |
| 4585 | if (IS_ERR(handle)) { |
| 4586 | ext4_orphan_del(NULL, inode); |
| 4587 | goto err_out; |
| 4588 | } |
| 4589 | ext4_orphan_del(handle, inode); |
| 4590 | ext4_journal_stop(handle); |
| 4591 | goto err_out; |
| 4592 | } |
| 4593 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | rc = inode_setattr(inode, attr); |
| 4597 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4598 | /* If inode_setattr's call to ext4_truncate failed to get a |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4599 | * transaction handle at all, we need to clean up the in-core |
| 4600 | * orphan list manually. */ |
| 4601 | if (inode->i_nlink) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4602 | ext4_orphan_del(NULL, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4603 | |
| 4604 | if (!rc && (ia_valid & ATTR_MODE)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4605 | rc = ext4_acl_chmod(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4606 | |
| 4607 | err_out: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4608 | ext4_std_error(inode->i_sb, error); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4609 | if (!error) |
| 4610 | error = rc; |
| 4611 | return error; |
| 4612 | } |
| 4613 | |
Mingming Cao | 3e3398a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4614 | int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 4615 | struct kstat *stat) |
| 4616 | { |
| 4617 | struct inode *inode; |
| 4618 | unsigned long delalloc_blocks; |
| 4619 | |
| 4620 | inode = dentry->d_inode; |
| 4621 | generic_fillattr(inode, stat); |
| 4622 | |
| 4623 | /* |
| 4624 | * We can't update i_blocks if the block allocation is delayed |
| 4625 | * otherwise in the case of system crash before the real block |
| 4626 | * allocation is done, we will have i_blocks inconsistent with |
| 4627 | * on-disk file blocks. |
| 4628 | * We always keep i_blocks updated together with real |
| 4629 | * allocation. But to not confuse with user, stat |
| 4630 | * will return the blocks that include the delayed allocation |
| 4631 | * blocks for this file. |
| 4632 | */ |
| 4633 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
| 4634 | delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; |
| 4635 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 4636 | |
| 4637 | stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; |
| 4638 | return 0; |
| 4639 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4640 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4641 | static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks, |
| 4642 | int chunk) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4643 | { |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4644 | int indirects; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4645 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4646 | /* if nrblocks are contiguous */ |
| 4647 | if (chunk) { |
| 4648 | /* |
| 4649 | * With N contiguous data blocks, it need at most |
| 4650 | * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks |
| 4651 | * 2 dindirect blocks |
| 4652 | * 1 tindirect block |
| 4653 | */ |
| 4654 | indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb); |
| 4655 | return indirects + 3; |
| 4656 | } |
| 4657 | /* |
| 4658 | * if nrblocks are not contiguous, worse case, each block touch |
| 4659 | * a indirect block, and each indirect block touch a double indirect |
| 4660 | * block, plus a triple indirect block |
| 4661 | */ |
| 4662 | indirects = nrblocks * 2 + 1; |
| 4663 | return indirects; |
| 4664 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4665 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4666 | static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
| 4667 | { |
| 4668 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) |
Theodore Ts'o | ac51d83 | 2008-11-06 16:49:36 -0500 | [diff] [blame] | 4669 | return ext4_indirect_trans_blocks(inode, nrblocks, chunk); |
| 4670 | return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4671 | } |
Theodore Ts'o | ac51d83 | 2008-11-06 16:49:36 -0500 | [diff] [blame] | 4672 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4673 | /* |
| 4674 | * Account for index blocks, block groups bitmaps and block group |
| 4675 | * descriptor blocks if modify datablocks and index blocks |
| 4676 | * worse case, the indexs blocks spread over different block groups |
| 4677 | * |
| 4678 | * If datablocks are discontiguous, they are possible to spread over |
| 4679 | * different block groups too. If they are contiugous, with flexbg, |
| 4680 | * they could still across block group boundary. |
| 4681 | * |
| 4682 | * Also account for superblock, inode, quota and xattr blocks |
| 4683 | */ |
| 4684 | int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
| 4685 | { |
| 4686 | int groups, gdpblocks; |
| 4687 | int idxblocks; |
| 4688 | int ret = 0; |
| 4689 | |
| 4690 | /* |
| 4691 | * How many index blocks need to touch to modify nrblocks? |
| 4692 | * The "Chunk" flag indicating whether the nrblocks is |
| 4693 | * physically contiguous on disk |
| 4694 | * |
| 4695 | * For Direct IO and fallocate, they calls get_block to allocate |
| 4696 | * one single extent at a time, so they could set the "Chunk" flag |
| 4697 | */ |
| 4698 | idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); |
| 4699 | |
| 4700 | ret = idxblocks; |
| 4701 | |
| 4702 | /* |
| 4703 | * Now let's see how many group bitmaps and group descriptors need |
| 4704 | * to account |
| 4705 | */ |
| 4706 | groups = idxblocks; |
| 4707 | if (chunk) |
| 4708 | groups += 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4709 | else |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4710 | groups += nrblocks; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4711 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4712 | gdpblocks = groups; |
| 4713 | if (groups > EXT4_SB(inode->i_sb)->s_groups_count) |
| 4714 | groups = EXT4_SB(inode->i_sb)->s_groups_count; |
| 4715 | if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) |
| 4716 | gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; |
| 4717 | |
| 4718 | /* bitmaps and block group descriptor blocks */ |
| 4719 | ret += groups + gdpblocks; |
| 4720 | |
| 4721 | /* Blocks for super block, inode, quota and xattr blocks */ |
| 4722 | ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4723 | |
| 4724 | return ret; |
| 4725 | } |
| 4726 | |
| 4727 | /* |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4728 | * Calulate the total number of credits to reserve to fit |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 4729 | * the modification of a single pages into a single transaction, |
| 4730 | * which may include multiple chunks of block allocations. |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4731 | * |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 4732 | * This could be called via ext4_write_begin() |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4733 | * |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 4734 | * We need to consider the worse case, when |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4735 | * one new block per extent. |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4736 | */ |
| 4737 | int ext4_writepage_trans_blocks(struct inode *inode) |
| 4738 | { |
| 4739 | int bpp = ext4_journal_blocks_per_page(inode); |
| 4740 | int ret; |
| 4741 | |
| 4742 | ret = ext4_meta_trans_blocks(inode, bpp, 0); |
| 4743 | |
| 4744 | /* Account for data blocks for journalled mode */ |
| 4745 | if (ext4_should_journal_data(inode)) |
| 4746 | ret += bpp; |
| 4747 | return ret; |
| 4748 | } |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 4749 | |
| 4750 | /* |
| 4751 | * Calculate the journal credits for a chunk of data modification. |
| 4752 | * |
| 4753 | * This is called from DIO, fallocate or whoever calling |
| 4754 | * ext4_get_blocks_wrap() to map/allocate a chunk of contigous disk blocks. |
| 4755 | * |
| 4756 | * journal buffers for data blocks are not included here, as DIO |
| 4757 | * and fallocate do no need to journal data buffers. |
| 4758 | */ |
| 4759 | int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) |
| 4760 | { |
| 4761 | return ext4_meta_trans_blocks(inode, nrblocks, 1); |
| 4762 | } |
| 4763 | |
Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4764 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4765 | * The caller must have previously called ext4_reserve_inode_write(). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4766 | * Give this, we know that the caller already has write access to iloc->bh. |
| 4767 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4768 | int ext4_mark_iloc_dirty(handle_t *handle, |
| 4769 | struct inode *inode, struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4770 | { |
| 4771 | int err = 0; |
| 4772 | |
Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4773 | if (test_opt(inode->i_sb, I_VERSION)) |
| 4774 | inode_inc_iversion(inode); |
| 4775 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4776 | /* the do_update_inode consumes one bh->b_count */ |
| 4777 | get_bh(iloc->bh); |
| 4778 | |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 4779 | /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4780 | err = ext4_do_update_inode(handle, inode, iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4781 | put_bh(iloc->bh); |
| 4782 | return err; |
| 4783 | } |
| 4784 | |
| 4785 | /* |
| 4786 | * On success, We end up with an outstanding reference count against |
| 4787 | * iloc->bh. This _must_ be cleaned up later. |
| 4788 | */ |
| 4789 | |
| 4790 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4791 | ext4_reserve_inode_write(handle_t *handle, struct inode *inode, |
| 4792 | struct ext4_iloc *iloc) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4793 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4794 | int err; |
| 4795 | |
| 4796 | err = ext4_get_inode_loc(inode, iloc); |
| 4797 | if (!err) { |
| 4798 | BUFFER_TRACE(iloc->bh, "get_write_access"); |
| 4799 | err = ext4_journal_get_write_access(handle, iloc->bh); |
| 4800 | if (err) { |
| 4801 | brelse(iloc->bh); |
| 4802 | iloc->bh = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4803 | } |
| 4804 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4805 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4806 | return err; |
| 4807 | } |
| 4808 | |
| 4809 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4810 | * Expand an inode by new_extra_isize bytes. |
| 4811 | * Returns 0 on success or negative error number on failure. |
| 4812 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4813 | static int ext4_expand_extra_isize(struct inode *inode, |
| 4814 | unsigned int new_extra_isize, |
| 4815 | struct ext4_iloc iloc, |
| 4816 | handle_t *handle) |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4817 | { |
| 4818 | struct ext4_inode *raw_inode; |
| 4819 | struct ext4_xattr_ibody_header *header; |
| 4820 | struct ext4_xattr_entry *entry; |
| 4821 | |
| 4822 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) |
| 4823 | return 0; |
| 4824 | |
| 4825 | raw_inode = ext4_raw_inode(&iloc); |
| 4826 | |
| 4827 | header = IHDR(inode, raw_inode); |
| 4828 | entry = IFIRST(header); |
| 4829 | |
| 4830 | /* No extended attributes present */ |
| 4831 | if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) || |
| 4832 | header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { |
| 4833 | memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, |
| 4834 | new_extra_isize); |
| 4835 | EXT4_I(inode)->i_extra_isize = new_extra_isize; |
| 4836 | return 0; |
| 4837 | } |
| 4838 | |
| 4839 | /* try to expand with EAs present */ |
| 4840 | return ext4_expand_extra_isize_ea(inode, new_extra_isize, |
| 4841 | raw_inode, handle); |
| 4842 | } |
| 4843 | |
| 4844 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4845 | * What we do here is to mark the in-core inode as clean with respect to inode |
| 4846 | * dirtiness (it may still be data-dirty). |
| 4847 | * This means that the in-core inode may be reaped by prune_icache |
| 4848 | * without having to perform any I/O. This is a very good thing, |
| 4849 | * because *any* task may call prune_icache - even ones which |
| 4850 | * have a transaction open against a different journal. |
| 4851 | * |
| 4852 | * Is this cheating? Not really. Sure, we haven't written the |
| 4853 | * inode out, but prune_icache isn't a user-visible syncing function. |
| 4854 | * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) |
| 4855 | * we start and wait on commits. |
| 4856 | * |
| 4857 | * Is this efficient/effective? Well, we're being nice to the system |
| 4858 | * by cleaning up our inodes proactively so they can be reaped |
| 4859 | * without I/O. But we are potentially leaving up to five seconds' |
| 4860 | * worth of inodes floating about which prune_icache wants us to |
| 4861 | * write out. One way to fix that would be to get prune_icache() |
| 4862 | * to do a write_super() to free up some memory. It has the desired |
| 4863 | * effect. |
| 4864 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4865 | int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4866 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4867 | struct ext4_iloc iloc; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4868 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 4869 | static unsigned int mnt_count; |
| 4870 | int err, ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4871 | |
| 4872 | might_sleep(); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4873 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4874 | if (ext4_handle_valid(handle) && |
| 4875 | EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4876 | !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) { |
| 4877 | /* |
| 4878 | * We need extra buffer credits since we may write into EA block |
| 4879 | * with this same handle. If journal_extend fails, then it will |
| 4880 | * only result in a minor loss of functionality for that inode. |
| 4881 | * If this is felt to be critical, then e2fsck should be run to |
| 4882 | * force a large enough s_min_extra_isize. |
| 4883 | */ |
| 4884 | if ((jbd2_journal_extend(handle, |
| 4885 | EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { |
| 4886 | ret = ext4_expand_extra_isize(inode, |
| 4887 | sbi->s_want_extra_isize, |
| 4888 | iloc, handle); |
| 4889 | if (ret) { |
| 4890 | EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND; |
Aneesh Kumar K.V | c1bddad | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 4891 | if (mnt_count != |
| 4892 | le16_to_cpu(sbi->s_es->s_mnt_count)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 4893 | ext4_warning(inode->i_sb, __func__, |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4894 | "Unable to expand inode %lu. Delete" |
| 4895 | " some EAs or run e2fsck.", |
| 4896 | inode->i_ino); |
Aneesh Kumar K.V | c1bddad | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 4897 | mnt_count = |
| 4898 | le16_to_cpu(sbi->s_es->s_mnt_count); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 4899 | } |
| 4900 | } |
| 4901 | } |
| 4902 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4903 | if (!err) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4904 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4905 | return err; |
| 4906 | } |
| 4907 | |
| 4908 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4909 | * ext4_dirty_inode() is called from __mark_inode_dirty() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4910 | * |
| 4911 | * We're really interested in the case where a file is being extended. |
| 4912 | * i_size has been changed by generic_commit_write() and we thus need |
| 4913 | * to include the updated inode in the current transaction. |
| 4914 | * |
| 4915 | * Also, DQUOT_ALLOC_SPACE() will always dirty the inode when blocks |
| 4916 | * are allocated to the file. |
| 4917 | * |
| 4918 | * If the inode is marked synchronous, we don't honour that here - doing |
| 4919 | * so would cause a commit on atime updates, which we don't bother doing. |
| 4920 | * We handle synchronous inodes at the highest possible level. |
| 4921 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4922 | void ext4_dirty_inode(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4923 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4924 | handle_t *current_handle = ext4_journal_current_handle(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4925 | handle_t *handle; |
| 4926 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4927 | if (!ext4_handle_valid(current_handle)) { |
| 4928 | ext4_mark_inode_dirty(current_handle, inode); |
| 4929 | return; |
| 4930 | } |
| 4931 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4932 | handle = ext4_journal_start(inode, 2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4933 | if (IS_ERR(handle)) |
| 4934 | goto out; |
| 4935 | if (current_handle && |
| 4936 | current_handle->h_transaction != handle->h_transaction) { |
| 4937 | /* This task has a transaction open against a different fs */ |
| 4938 | printk(KERN_EMERG "%s: transactions do not match!\n", |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 4939 | __func__); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4940 | } else { |
| 4941 | jbd_debug(5, "marking dirty. outer handle=%p\n", |
| 4942 | current_handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4943 | ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4944 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4945 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4946 | out: |
| 4947 | return; |
| 4948 | } |
| 4949 | |
| 4950 | #if 0 |
| 4951 | /* |
| 4952 | * Bind an inode's backing buffer_head into this transaction, to prevent |
| 4953 | * it from being flushed to disk early. Unlike |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4954 | * ext4_reserve_inode_write, this leaves behind no bh reference and |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4955 | * returns no iloc structure, so the caller needs to repeat the iloc |
| 4956 | * lookup to mark the inode dirty later. |
| 4957 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4958 | static int ext4_pin_inode(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4959 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4960 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4961 | |
| 4962 | int err = 0; |
| 4963 | if (handle) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4964 | err = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4965 | if (!err) { |
| 4966 | BUFFER_TRACE(iloc.bh, "get_write_access"); |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 4967 | err = jbd2_journal_get_write_access(handle, iloc.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4968 | if (!err) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4969 | err = ext4_handle_dirty_metadata(handle, |
| 4970 | inode, |
| 4971 | iloc.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4972 | brelse(iloc.bh); |
| 4973 | } |
| 4974 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4975 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4976 | return err; |
| 4977 | } |
| 4978 | #endif |
| 4979 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4980 | int ext4_change_inode_journal_flag(struct inode *inode, int val) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4981 | { |
| 4982 | journal_t *journal; |
| 4983 | handle_t *handle; |
| 4984 | int err; |
| 4985 | |
| 4986 | /* |
| 4987 | * We have to be very careful here: changing a data block's |
| 4988 | * journaling status dynamically is dangerous. If we write a |
| 4989 | * data block to the journal, change the status and then delete |
| 4990 | * that block, we risk forgetting to revoke the old log record |
| 4991 | * from the journal and so a subsequent replay can corrupt data. |
| 4992 | * So, first we make sure that the journal is empty and that |
| 4993 | * nobody is changing anything. |
| 4994 | */ |
| 4995 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4996 | journal = EXT4_JOURNAL(inode); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4997 | if (!journal) |
| 4998 | return 0; |
Dave Hansen | d699594 | 2007-07-18 08:33:51 -0400 | [diff] [blame] | 4999 | if (is_journal_aborted(journal)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5000 | return -EROFS; |
| 5001 | |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 5002 | jbd2_journal_lock_updates(journal); |
| 5003 | jbd2_journal_flush(journal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5004 | |
| 5005 | /* |
| 5006 | * OK, there are no updates running now, and all cached data is |
| 5007 | * synced to disk. We are now in a completely consistent state |
| 5008 | * which doesn't have anything in the journal, and we know that |
| 5009 | * no filesystem updates are running, so it is safe to modify |
| 5010 | * the inode's in-core data-journaling state flag now. |
| 5011 | */ |
| 5012 | |
| 5013 | if (val) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5014 | EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5015 | else |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5016 | EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL; |
| 5017 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5018 | |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 5019 | jbd2_journal_unlock_updates(journal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5020 | |
| 5021 | /* Finally we can mark the inode as dirty. */ |
| 5022 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5023 | handle = ext4_journal_start(inode, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5024 | if (IS_ERR(handle)) |
| 5025 | return PTR_ERR(handle); |
| 5026 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5027 | err = ext4_mark_inode_dirty(handle, inode); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5028 | ext4_handle_sync(handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5029 | ext4_journal_stop(handle); |
| 5030 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5031 | |
| 5032 | return err; |
| 5033 | } |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5034 | |
| 5035 | static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) |
| 5036 | { |
| 5037 | return !buffer_mapped(bh); |
| 5038 | } |
| 5039 | |
| 5040 | int ext4_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 5041 | { |
| 5042 | loff_t size; |
| 5043 | unsigned long len; |
| 5044 | int ret = -EINVAL; |
Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 5045 | void *fsdata; |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5046 | struct file *file = vma->vm_file; |
| 5047 | struct inode *inode = file->f_path.dentry->d_inode; |
| 5048 | struct address_space *mapping = inode->i_mapping; |
| 5049 | |
| 5050 | /* |
| 5051 | * Get i_alloc_sem to stop truncates messing with the inode. We cannot |
| 5052 | * get i_mutex because we are already holding mmap_sem. |
| 5053 | */ |
| 5054 | down_read(&inode->i_alloc_sem); |
| 5055 | size = i_size_read(inode); |
| 5056 | if (page->mapping != mapping || size <= page_offset(page) |
| 5057 | || !PageUptodate(page)) { |
| 5058 | /* page got truncated from under us? */ |
| 5059 | goto out_unlock; |
| 5060 | } |
| 5061 | ret = 0; |
| 5062 | if (PageMappedToDisk(page)) |
| 5063 | goto out_unlock; |
| 5064 | |
| 5065 | if (page->index == size >> PAGE_CACHE_SHIFT) |
| 5066 | len = size & ~PAGE_CACHE_MASK; |
| 5067 | else |
| 5068 | len = PAGE_CACHE_SIZE; |
| 5069 | |
| 5070 | if (page_has_buffers(page)) { |
| 5071 | /* return if we have all the buffers mapped */ |
| 5072 | if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, |
| 5073 | ext4_bh_unmapped)) |
| 5074 | goto out_unlock; |
| 5075 | } |
| 5076 | /* |
| 5077 | * OK, we need to fill the hole... Do write_begin write_end |
| 5078 | * to do block allocation/reservation.We are not holding |
| 5079 | * inode.i__mutex here. That allow * parallel write_begin, |
| 5080 | * write_end call. lock_page prevent this from happening |
| 5081 | * on the same page though |
| 5082 | */ |
| 5083 | 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] | 5084 | len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5085 | if (ret < 0) |
| 5086 | goto out_unlock; |
| 5087 | 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] | 5088 | len, len, page, fsdata); |
Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5089 | if (ret < 0) |
| 5090 | goto out_unlock; |
| 5091 | ret = 0; |
| 5092 | out_unlock: |
| 5093 | up_read(&inode->i_alloc_sem); |
| 5094 | return ret; |
| 5095 | } |