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