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