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