| 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 | * | 
|  | 81 | * If the handle isn't valid we're not journaling so there's nothing to do. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 82 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 83 | int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode, | 
|  | 84 | struct buffer_head *bh, ext4_fsblk_t blocknr) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 85 | { | 
|  | 86 | int err; | 
|  | 87 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 88 | if (!ext4_handle_valid(handle)) | 
|  | 89 | return 0; | 
|  | 90 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 91 | might_sleep(); | 
|  | 92 |  | 
|  | 93 | BUFFER_TRACE(bh, "enter"); | 
|  | 94 |  | 
|  | 95 | jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, " | 
|  | 96 | "data mode %lx\n", | 
|  | 97 | bh, is_metadata, inode->i_mode, | 
|  | 98 | test_opt(inode->i_sb, DATA_FLAGS)); | 
|  | 99 |  | 
|  | 100 | /* Never use the revoke function if we are doing full data | 
|  | 101 | * journaling: there is no need to, and a V1 superblock won't | 
|  | 102 | * support it.  Otherwise, only skip the revoke on un-journaled | 
|  | 103 | * data blocks. */ | 
|  | 104 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 105 | if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || | 
|  | 106 | (!is_metadata && !ext4_should_journal_data(inode))) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 107 | if (bh) { | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 108 | BUFFER_TRACE(bh, "call jbd2_journal_forget"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 109 | return ext4_journal_forget(handle, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 110 | } | 
|  | 111 | return 0; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | /* | 
|  | 115 | * data!=journal && (is_metadata || should_journal_data(inode)) | 
|  | 116 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 117 | BUFFER_TRACE(bh, "call ext4_journal_revoke"); | 
|  | 118 | err = ext4_journal_revoke(handle, blocknr, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 119 | if (err) | 
| Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 120 | ext4_abort(inode->i_sb, __func__, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 121 | "error %d when attempting revoke", err); | 
|  | 122 | BUFFER_TRACE(bh, "exit"); | 
|  | 123 | return err; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | /* | 
|  | 127 | * Work out how many blocks we need to proceed with the next chunk of a | 
|  | 128 | * truncate transaction. | 
|  | 129 | */ | 
|  | 130 | static unsigned long blocks_for_truncate(struct inode *inode) | 
|  | 131 | { | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 132 | ext4_lblk_t needed; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); | 
|  | 135 |  | 
|  | 136 | /* Give ourselves just enough room to cope with inodes in which | 
|  | 137 | * i_blocks is corrupt: we've seen disk corruptions in the past | 
|  | 138 | * which resulted in random data in an inode which looked enough | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 139 | * like a regular file for ext4 to try to delete it.  Things | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 140 | * will go a bit crazy if that happens, but at least we should | 
|  | 141 | * try not to panic the whole kernel. */ | 
|  | 142 | if (needed < 2) | 
|  | 143 | needed = 2; | 
|  | 144 |  | 
|  | 145 | /* But we need to bound the transaction so we don't overflow the | 
|  | 146 | * journal. */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 147 | if (needed > EXT4_MAX_TRANS_DATA) | 
|  | 148 | needed = EXT4_MAX_TRANS_DATA; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 149 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 150 | return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
|  | 153 | /* | 
|  | 154 | * Truncate transactions can be complex and absolutely huge.  So we need to | 
|  | 155 | * be able to restart the transaction at a conventient checkpoint to make | 
|  | 156 | * sure we don't overflow the journal. | 
|  | 157 | * | 
|  | 158 | * start_transaction gets us a new handle for a truncate transaction, | 
|  | 159 | * and extend_transaction tries to extend the existing one a bit.  If | 
|  | 160 | * extend fails, we need to propagate the failure up and restart the | 
|  | 161 | * transaction in the top-level truncate loop. --sct | 
|  | 162 | */ | 
|  | 163 | static handle_t *start_transaction(struct inode *inode) | 
|  | 164 | { | 
|  | 165 | handle_t *result; | 
|  | 166 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 167 | result = ext4_journal_start(inode, blocks_for_truncate(inode)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 168 | if (!IS_ERR(result)) | 
|  | 169 | return result; | 
|  | 170 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 171 | ext4_std_error(inode->i_sb, PTR_ERR(result)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 172 | return result; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | /* | 
|  | 176 | * Try to extend this transaction for the purposes of truncation. | 
|  | 177 | * | 
|  | 178 | * Returns 0 if we managed to create more room.  If we can't create more | 
|  | 179 | * room, and the transaction must be restarted we return 1. | 
|  | 180 | */ | 
|  | 181 | static int try_to_extend_transaction(handle_t *handle, struct inode *inode) | 
|  | 182 | { | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 183 | if (!ext4_handle_valid(handle)) | 
|  | 184 | return 0; | 
|  | 185 | if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 186 | return 0; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 187 | if (!ext4_journal_extend(handle, blocks_for_truncate(inode))) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 188 | return 0; | 
|  | 189 | return 1; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | /* | 
|  | 193 | * Restart the transaction associated with *handle.  This does a commit, | 
|  | 194 | * so before we call here everything must be consistently dirtied against | 
|  | 195 | * this transaction. | 
|  | 196 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 197 | static int ext4_journal_test_restart(handle_t *handle, struct inode *inode) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 198 | { | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 199 | BUG_ON(EXT4_JOURNAL(inode) == NULL); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 200 | jbd_debug(2, "restarting handle %p\n", handle); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 201 | return ext4_journal_restart(handle, blocks_for_truncate(inode)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
|  | 204 | /* | 
|  | 205 | * Called at the last iput() if i_nlink is zero. | 
|  | 206 | */ | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 207 | void ext4_delete_inode(struct inode *inode) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 208 | { | 
|  | 209 | handle_t *handle; | 
| Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 210 | int err; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 211 |  | 
| Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 212 | if (ext4_should_order_data(inode)) | 
|  | 213 | ext4_begin_ordered_truncate(inode, 0); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 214 | truncate_inode_pages(&inode->i_data, 0); | 
|  | 215 |  | 
|  | 216 | if (is_bad_inode(inode)) | 
|  | 217 | goto no_delete; | 
|  | 218 |  | 
| Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 219 | handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 220 | if (IS_ERR(handle)) { | 
| Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 221 | ext4_std_error(inode->i_sb, PTR_ERR(handle)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 222 | /* | 
|  | 223 | * If we're going to skip the normal cleanup, we still need to | 
|  | 224 | * make sure that the in-core orphan linked list is properly | 
|  | 225 | * cleaned up. | 
|  | 226 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 227 | ext4_orphan_del(NULL, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 228 | goto no_delete; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | if (IS_SYNC(inode)) | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 232 | ext4_handle_sync(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 233 | inode->i_size = 0; | 
| Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 234 | err = ext4_mark_inode_dirty(handle, inode); | 
|  | 235 | if (err) { | 
|  | 236 | ext4_warning(inode->i_sb, __func__, | 
|  | 237 | "couldn't mark inode dirty (err %d)", err); | 
|  | 238 | goto stop_handle; | 
|  | 239 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 240 | if (inode->i_blocks) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 241 | ext4_truncate(inode); | 
| Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 242 |  | 
|  | 243 | /* | 
|  | 244 | * ext4_ext_truncate() doesn't reserve any slop when it | 
|  | 245 | * restarts journal transactions; therefore there may not be | 
|  | 246 | * enough credits left in the handle to remove the inode from | 
|  | 247 | * the orphan list and set the dtime field. | 
|  | 248 | */ | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 249 | if (!ext4_handle_has_enough_credits(handle, 3)) { | 
| Theodore Ts'o | bc965ab | 2008-08-02 21:10:38 -0400 | [diff] [blame] | 250 | err = ext4_journal_extend(handle, 3); | 
|  | 251 | if (err > 0) | 
|  | 252 | err = ext4_journal_restart(handle, 3); | 
|  | 253 | if (err != 0) { | 
|  | 254 | ext4_warning(inode->i_sb, __func__, | 
|  | 255 | "couldn't extend journal (err %d)", err); | 
|  | 256 | stop_handle: | 
|  | 257 | ext4_journal_stop(handle); | 
|  | 258 | goto no_delete; | 
|  | 259 | } | 
|  | 260 | } | 
|  | 261 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 262 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 263 | * Kill off the orphan record which ext4_truncate created. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 264 | * AKPM: I think this can be inside the above `if'. | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 265 | * 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] | 266 | * deletion of a non-existent orphan - this is because we don't | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 267 | * know if ext4_truncate() actually created an orphan record. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 268 | * (Well, we could do this if we need to, but heck - it works) | 
|  | 269 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 270 | ext4_orphan_del(handle, inode); | 
|  | 271 | EXT4_I(inode)->i_dtime	= get_seconds(); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 272 |  | 
|  | 273 | /* | 
|  | 274 | * One subtle ordering requirement: if anything has gone wrong | 
|  | 275 | * (transaction abort, IO errors, whatever), then we can still | 
|  | 276 | * do these next steps (the fs will already have been marked as | 
|  | 277 | * having errors), but we can't free the inode if the mark_dirty | 
|  | 278 | * fails. | 
|  | 279 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 280 | if (ext4_mark_inode_dirty(handle, inode)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 281 | /* If that failed, just do the required in-core inode clear. */ | 
|  | 282 | clear_inode(inode); | 
|  | 283 | else | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 284 | ext4_free_inode(handle, inode); | 
|  | 285 | ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 286 | return; | 
|  | 287 | no_delete: | 
|  | 288 | clear_inode(inode);	/* We must guarantee clearing of inode... */ | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | typedef struct { | 
|  | 292 | __le32	*p; | 
|  | 293 | __le32	key; | 
|  | 294 | struct buffer_head *bh; | 
|  | 295 | } Indirect; | 
|  | 296 |  | 
|  | 297 | static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) | 
|  | 298 | { | 
|  | 299 | p->key = *(p->p = v); | 
|  | 300 | p->bh = bh; | 
|  | 301 | } | 
|  | 302 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 303 | /** | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 304 | *	ext4_block_to_path - parse the block number into array of offsets | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 305 | *	@inode: inode in question (we are only interested in its superblock) | 
|  | 306 | *	@i_block: block number to be parsed | 
|  | 307 | *	@offsets: array to store the offsets in | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 308 | *	@boundary: set this non-zero if the referred-to block is likely to be | 
|  | 309 | *	       followed (on disk) by an indirect block. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 310 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 311 | *	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] | 312 | *	for UNIX filesystems - tree of pointers anchored in the inode, with | 
|  | 313 | *	data blocks at leaves and indirect blocks in intermediate nodes. | 
|  | 314 | *	This function translates the block number into path in that tree - | 
|  | 315 | *	return value is the path length and @offsets[n] is the offset of | 
|  | 316 | *	pointer to (n+1)th node in the nth one. If @block is out of range | 
|  | 317 | *	(negative or too large) warning is printed and zero returned. | 
|  | 318 | * | 
|  | 319 | *	Note: function doesn't find node addresses, so no IO is needed. All | 
|  | 320 | *	we need to know is the capacity of indirect blocks (taken from the | 
|  | 321 | *	inode->i_sb). | 
|  | 322 | */ | 
|  | 323 |  | 
|  | 324 | /* | 
|  | 325 | * Portability note: the last comparison (check that we fit into triple | 
|  | 326 | * indirect block) is spelled differently, because otherwise on an | 
|  | 327 | * architecture with 32-bit longs and 8Kb pages we might get into trouble | 
|  | 328 | * if our filesystem had 8Kb blocks. We might use long long, but that would | 
|  | 329 | * kill us on x86. Oh, well, at least the sign propagation does not matter - | 
|  | 330 | * i_block would have to be negative in the very beginning, so we would not | 
|  | 331 | * get there at all. | 
|  | 332 | */ | 
|  | 333 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 334 | static int ext4_block_to_path(struct inode *inode, | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 335 | ext4_lblk_t i_block, | 
|  | 336 | ext4_lblk_t offsets[4], int *boundary) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 337 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 338 | int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); | 
|  | 339 | int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); | 
|  | 340 | const long direct_blocks = EXT4_NDIR_BLOCKS, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 341 | indirect_blocks = ptrs, | 
|  | 342 | double_blocks = (1 << (ptrs_bits * 2)); | 
|  | 343 | int n = 0; | 
|  | 344 | int final = 0; | 
|  | 345 |  | 
|  | 346 | if (i_block < 0) { | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 347 | ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0"); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 348 | } else if (i_block < direct_blocks) { | 
|  | 349 | offsets[n++] = i_block; | 
|  | 350 | final = direct_blocks; | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 351 | } else if ((i_block -= direct_blocks) < indirect_blocks) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 352 | offsets[n++] = EXT4_IND_BLOCK; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 353 | offsets[n++] = i_block; | 
|  | 354 | final = ptrs; | 
|  | 355 | } else if ((i_block -= indirect_blocks) < double_blocks) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 356 | offsets[n++] = EXT4_DIND_BLOCK; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 357 | offsets[n++] = i_block >> ptrs_bits; | 
|  | 358 | offsets[n++] = i_block & (ptrs - 1); | 
|  | 359 | final = ptrs; | 
|  | 360 | } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 361 | offsets[n++] = EXT4_TIND_BLOCK; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 362 | offsets[n++] = i_block >> (ptrs_bits * 2); | 
|  | 363 | offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); | 
|  | 364 | offsets[n++] = i_block & (ptrs - 1); | 
|  | 365 | final = ptrs; | 
|  | 366 | } else { | 
| Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 367 | ext4_warning(inode->i_sb, "ext4_block_to_path", | 
| Theodore Ts'o | 06a279d | 2009-01-17 18:41:37 -0500 | [diff] [blame] | 368 | "block %lu > max in inode %lu", | 
| Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 369 | i_block + direct_blocks + | 
| Theodore Ts'o | 06a279d | 2009-01-17 18:41:37 -0500 | [diff] [blame] | 370 | indirect_blocks + double_blocks, inode->i_ino); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 371 | } | 
|  | 372 | if (boundary) | 
|  | 373 | *boundary = final - 1 - (i_block & (ptrs - 1)); | 
|  | 374 | return n; | 
|  | 375 | } | 
|  | 376 |  | 
| Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 377 | static int __ext4_check_blockref(const char *function, struct inode *inode, | 
| Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 378 | __le32 *p, unsigned int max) | 
|  | 379 | { | 
| Thiemo Nagel | f73953c | 2009-04-07 18:46:47 -0400 | [diff] [blame] | 380 | __le32 *bref = p; | 
| Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 381 | unsigned int blk; | 
|  | 382 |  | 
| Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 383 | while (bref < p+max) { | 
| Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 384 | blk = le32_to_cpu(*bref++); | 
|  | 385 | if (blk && | 
|  | 386 | unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb), | 
|  | 387 | blk, 1))) { | 
| Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 388 | ext4_error(inode->i_sb, function, | 
| Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 389 | "invalid block reference %u " | 
|  | 390 | "in inode #%lu", blk, inode->i_ino); | 
| Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 391 | return -EIO; | 
|  | 392 | } | 
| Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 393 | } | 
|  | 394 | return 0; | 
|  | 395 | } | 
|  | 396 |  | 
|  | 397 |  | 
|  | 398 | #define ext4_check_indirect_blockref(inode, bh)                         \ | 
|  | 399 | __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data,  \ | 
|  | 400 | EXT4_ADDR_PER_BLOCK((inode)->i_sb)) | 
|  | 401 |  | 
|  | 402 | #define ext4_check_inode_blockref(inode)                                \ | 
|  | 403 | __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data,   \ | 
|  | 404 | EXT4_NDIR_BLOCKS) | 
|  | 405 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 406 | /** | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 407 | *	ext4_get_branch - read the chain of indirect blocks leading to data | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 408 | *	@inode: inode in question | 
|  | 409 | *	@depth: depth of the chain (1 - direct pointer, etc.) | 
|  | 410 | *	@offsets: offsets of pointers in inode/indirect blocks | 
|  | 411 | *	@chain: place to store the result | 
|  | 412 | *	@err: here we store the error value | 
|  | 413 | * | 
|  | 414 | *	Function fills the array of triples <key, p, bh> and returns %NULL | 
|  | 415 | *	if everything went OK or the pointer to the last filled triple | 
|  | 416 | *	(incomplete one) otherwise. Upon the return chain[i].key contains | 
|  | 417 | *	the number of (i+1)-th block in the chain (as it is stored in memory, | 
|  | 418 | *	i.e. little-endian 32-bit), chain[i].p contains the address of that | 
|  | 419 | *	number (it points into struct inode for i==0 and into the bh->b_data | 
|  | 420 | *	for i>0) and chain[i].bh points to the buffer_head of i-th indirect | 
|  | 421 | *	block for i>0 and NULL for i==0. In other words, it holds the block | 
|  | 422 | *	numbers of the chain, addresses they were taken from (and where we can | 
|  | 423 | *	verify that chain did not change) and buffer_heads hosting these | 
|  | 424 | *	numbers. | 
|  | 425 | * | 
|  | 426 | *	Function stops when it stumbles upon zero pointer (absent block) | 
|  | 427 | *		(pointer to last triple returned, *@err == 0) | 
|  | 428 | *	or when it gets an IO error reading an indirect block | 
|  | 429 | *		(ditto, *@err == -EIO) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 430 | *	or when it reads all @depth-1 indirect blocks successfully and finds | 
|  | 431 | *	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] | 432 | * | 
|  | 433 | *      Need to be called with | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 434 | *      down_read(&EXT4_I(inode)->i_data_sem) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 435 | */ | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 436 | static Indirect *ext4_get_branch(struct inode *inode, int depth, | 
|  | 437 | ext4_lblk_t  *offsets, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 438 | Indirect chain[4], int *err) | 
|  | 439 | { | 
|  | 440 | struct super_block *sb = inode->i_sb; | 
|  | 441 | Indirect *p = chain; | 
|  | 442 | struct buffer_head *bh; | 
|  | 443 |  | 
|  | 444 | *err = 0; | 
|  | 445 | /* i_data is not going away, no lock needed */ | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 446 | add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 447 | if (!p->key) | 
|  | 448 | goto no_block; | 
|  | 449 | while (--depth) { | 
| Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 450 | bh = sb_getblk(sb, le32_to_cpu(p->key)); | 
|  | 451 | if (unlikely(!bh)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 452 | goto failure; | 
| Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 453 |  | 
|  | 454 | if (!bh_uptodate_or_lock(bh)) { | 
|  | 455 | if (bh_submit_read(bh) < 0) { | 
|  | 456 | put_bh(bh); | 
|  | 457 | goto failure; | 
|  | 458 | } | 
|  | 459 | /* validate block references */ | 
|  | 460 | if (ext4_check_indirect_blockref(inode, bh)) { | 
|  | 461 | put_bh(bh); | 
|  | 462 | goto failure; | 
|  | 463 | } | 
|  | 464 | } | 
|  | 465 |  | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 466 | add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 467 | /* Reader: end */ | 
|  | 468 | if (!p->key) | 
|  | 469 | goto no_block; | 
|  | 470 | } | 
|  | 471 | return NULL; | 
|  | 472 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 473 | failure: | 
|  | 474 | *err = -EIO; | 
|  | 475 | no_block: | 
|  | 476 | return p; | 
|  | 477 | } | 
|  | 478 |  | 
|  | 479 | /** | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 480 | *	ext4_find_near - find a place for allocation with sufficient locality | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 481 | *	@inode: owner | 
|  | 482 | *	@ind: descriptor of indirect block. | 
|  | 483 | * | 
| Benoit Boissinot | 1cc8dcf5 | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 484 | *	This function returns the preferred place for block allocation. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 485 | *	It is used when heuristic for sequential allocation fails. | 
|  | 486 | *	Rules are: | 
|  | 487 | *	  + if there is a block to the left of our position - allocate near it. | 
|  | 488 | *	  + if pointer will live in indirect block - allocate near that block. | 
|  | 489 | *	  + if pointer will live in inode - allocate in the same | 
|  | 490 | *	    cylinder group. | 
|  | 491 | * | 
|  | 492 | * In the latter case we colour the starting block by the callers PID to | 
|  | 493 | * prevent it from clashing with concurrent allocations for a different inode | 
|  | 494 | * in the same block group.   The PID is used here so that functionally related | 
|  | 495 | * files will be close-by on-disk. | 
|  | 496 | * | 
|  | 497 | *	Caller must make sure that @ind is valid and will stay that way. | 
|  | 498 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 499 | static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 500 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 501 | struct ext4_inode_info *ei = EXT4_I(inode); | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 502 | __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 503 | __le32 *p; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 504 | ext4_fsblk_t bg_start; | 
| Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 505 | ext4_fsblk_t last_block; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 506 | ext4_grpblk_t colour; | 
| Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 507 | ext4_group_t block_group; | 
|  | 508 | int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 509 |  | 
|  | 510 | /* Try to find previous block */ | 
|  | 511 | for (p = ind->p - 1; p >= start; p--) { | 
|  | 512 | if (*p) | 
|  | 513 | return le32_to_cpu(*p); | 
|  | 514 | } | 
|  | 515 |  | 
|  | 516 | /* No such thing, so let's try location of indirect block */ | 
|  | 517 | if (ind->bh) | 
|  | 518 | return ind->bh->b_blocknr; | 
|  | 519 |  | 
|  | 520 | /* | 
|  | 521 | * It is going to be referred to from the inode itself? OK, just put it | 
|  | 522 | * into the same cylinder group then. | 
|  | 523 | */ | 
| Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 524 | block_group = ei->i_block_group; | 
|  | 525 | if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { | 
|  | 526 | block_group &= ~(flex_size-1); | 
|  | 527 | if (S_ISREG(inode->i_mode)) | 
|  | 528 | block_group++; | 
|  | 529 | } | 
|  | 530 | bg_start = ext4_group_first_block_no(inode->i_sb, block_group); | 
| Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 531 | last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; | 
|  | 532 |  | 
| Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 533 | /* | 
|  | 534 | * If we are doing delayed allocation, we don't need take | 
|  | 535 | * colour into account. | 
|  | 536 | */ | 
|  | 537 | if (test_opt(inode->i_sb, DELALLOC)) | 
|  | 538 | return bg_start; | 
|  | 539 |  | 
| Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 540 | if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) | 
|  | 541 | colour = (current->pid % 16) * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 542 | (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); | 
| Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 543 | else | 
|  | 544 | colour = (current->pid % 16) * ((last_block - bg_start) / 16); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 545 | return bg_start + colour; | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | /** | 
| Benoit Boissinot | 1cc8dcf5 | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 549 | *	ext4_find_goal - find a preferred place for allocation. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 550 | *	@inode: owner | 
|  | 551 | *	@block:  block we want | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 552 | *	@partial: pointer to the last triple within a chain | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 553 | * | 
| Benoit Boissinot | 1cc8dcf5 | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 554 | *	Normally this function find the preferred place for block allocation, | 
| Akinobu Mita | fb01bfd | 2008-02-06 01:40:16 -0800 | [diff] [blame] | 555 | *	returns it. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 556 | */ | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 557 | static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, | 
| Akinobu Mita | fb01bfd | 2008-02-06 01:40:16 -0800 | [diff] [blame] | 558 | Indirect *partial) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 559 | { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 560 | /* | 
| Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 561 | * XXX need to get goal block from mballoc's data structures | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 562 | */ | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 563 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 564 | return ext4_find_near(inode, partial); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 565 | } | 
|  | 566 |  | 
|  | 567 | /** | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 568 | *	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] | 569 | *	of direct blocks need to be allocated for the given branch. | 
|  | 570 | * | 
|  | 571 | *	@branch: chain of indirect blocks | 
|  | 572 | *	@k: number of blocks need for indirect blocks | 
|  | 573 | *	@blks: number of data blocks to be mapped. | 
|  | 574 | *	@blocks_to_boundary:  the offset in the indirect block | 
|  | 575 | * | 
|  | 576 | *	return the total number of blocks to be allocate, including the | 
|  | 577 | *	direct and indirect blocks. | 
|  | 578 | */ | 
| Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 579 | static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 580 | int blocks_to_boundary) | 
|  | 581 | { | 
| Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 582 | unsigned int count = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 583 |  | 
|  | 584 | /* | 
|  | 585 | * Simple case, [t,d]Indirect block(s) has not allocated yet | 
|  | 586 | * then it's clear blocks on that path have not allocated | 
|  | 587 | */ | 
|  | 588 | if (k > 0) { | 
|  | 589 | /* right now we don't handle cross boundary allocation */ | 
|  | 590 | if (blks < blocks_to_boundary + 1) | 
|  | 591 | count += blks; | 
|  | 592 | else | 
|  | 593 | count += blocks_to_boundary + 1; | 
|  | 594 | return count; | 
|  | 595 | } | 
|  | 596 |  | 
|  | 597 | count++; | 
|  | 598 | while (count < blks && count <= blocks_to_boundary && | 
|  | 599 | le32_to_cpu(*(branch[0].p + count)) == 0) { | 
|  | 600 | count++; | 
|  | 601 | } | 
|  | 602 | return count; | 
|  | 603 | } | 
|  | 604 |  | 
|  | 605 | /** | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 606 | *	ext4_alloc_blocks: multiple allocate blocks needed for a branch | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 607 | *	@indirect_blks: the number of blocks need to allocate for indirect | 
|  | 608 | *			blocks | 
|  | 609 | * | 
|  | 610 | *	@new_blocks: on return it will store the new block numbers for | 
|  | 611 | *	the indirect blocks(if needed) and the first direct block, | 
|  | 612 | *	@blks:	on return it will store the total number of allocated | 
|  | 613 | *		direct blocks | 
|  | 614 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 615 | static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 616 | ext4_lblk_t iblock, ext4_fsblk_t goal, | 
|  | 617 | int indirect_blks, int blks, | 
|  | 618 | ext4_fsblk_t new_blocks[4], int *err) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 619 | { | 
| Theodore Ts'o | 815a113 | 2009-01-01 23:59:43 -0500 | [diff] [blame] | 620 | struct ext4_allocation_request ar; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 621 | int target, i; | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 622 | unsigned long count = 0, blk_allocated = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 623 | int index = 0; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 624 | ext4_fsblk_t current_block = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 625 | int ret = 0; | 
|  | 626 |  | 
|  | 627 | /* | 
|  | 628 | * Here we try to allocate the requested multiple blocks at once, | 
|  | 629 | * on a best-effort basis. | 
|  | 630 | * To build a branch, we should allocate blocks for | 
|  | 631 | * the indirect blocks(if not allocated yet), and at least | 
|  | 632 | * the first direct block of this branch.  That's the | 
|  | 633 | * minimum number of blocks need to allocate(required) | 
|  | 634 | */ | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 635 | /* first we try to allocate the indirect blocks */ | 
|  | 636 | target = indirect_blks; | 
|  | 637 | while (target > 0) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 638 | count = target; | 
|  | 639 | /* allocating blocks for indirect blocks and direct blocks */ | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 640 | current_block = ext4_new_meta_blocks(handle, inode, | 
|  | 641 | goal, &count, err); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 642 | if (*err) | 
|  | 643 | goto failed_out; | 
|  | 644 |  | 
|  | 645 | target -= count; | 
|  | 646 | /* allocate blocks for indirect blocks */ | 
|  | 647 | while (index < indirect_blks && count) { | 
|  | 648 | new_blocks[index++] = current_block++; | 
|  | 649 | count--; | 
|  | 650 | } | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 651 | if (count > 0) { | 
|  | 652 | /* | 
|  | 653 | * save the new block number | 
|  | 654 | * for the first direct block | 
|  | 655 | */ | 
|  | 656 | new_blocks[index] = current_block; | 
|  | 657 | printk(KERN_INFO "%s returned more blocks than " | 
|  | 658 | "requested\n", __func__); | 
|  | 659 | WARN_ON(1); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 660 | break; | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 661 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 662 | } | 
|  | 663 |  | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 664 | target = blks - count ; | 
|  | 665 | blk_allocated = count; | 
|  | 666 | if (!target) | 
|  | 667 | goto allocated; | 
|  | 668 | /* Now allocate data blocks */ | 
| Theodore Ts'o | 815a113 | 2009-01-01 23:59:43 -0500 | [diff] [blame] | 669 | memset(&ar, 0, sizeof(ar)); | 
|  | 670 | ar.inode = inode; | 
|  | 671 | ar.goal = goal; | 
|  | 672 | ar.len = target; | 
|  | 673 | ar.logical = iblock; | 
|  | 674 | if (S_ISREG(inode->i_mode)) | 
|  | 675 | /* enable in-core preallocation only for regular files */ | 
|  | 676 | ar.flags = EXT4_MB_HINT_DATA; | 
|  | 677 |  | 
|  | 678 | current_block = ext4_mb_new_blocks(handle, &ar, err); | 
|  | 679 |  | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 680 | if (*err && (target == blks)) { | 
|  | 681 | /* | 
|  | 682 | * if the allocation failed and we didn't allocate | 
|  | 683 | * any blocks before | 
|  | 684 | */ | 
|  | 685 | goto failed_out; | 
|  | 686 | } | 
|  | 687 | if (!*err) { | 
|  | 688 | if (target == blks) { | 
|  | 689 | /* | 
|  | 690 | * save the new block number | 
|  | 691 | * for the first direct block | 
|  | 692 | */ | 
|  | 693 | new_blocks[index] = current_block; | 
|  | 694 | } | 
| Theodore Ts'o | 815a113 | 2009-01-01 23:59:43 -0500 | [diff] [blame] | 695 | blk_allocated += ar.len; | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 696 | } | 
|  | 697 | allocated: | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 698 | /* total number of blocks allocated for direct blocks */ | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 699 | ret = blk_allocated; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 700 | *err = 0; | 
|  | 701 | return ret; | 
|  | 702 | failed_out: | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 703 | for (i = 0; i < index; i++) | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 704 | ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 705 | return ret; | 
|  | 706 | } | 
|  | 707 |  | 
|  | 708 | /** | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 709 | *	ext4_alloc_branch - allocate and set up a chain of blocks. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 710 | *	@inode: owner | 
|  | 711 | *	@indirect_blks: number of allocated indirect blocks | 
|  | 712 | *	@blks: number of allocated direct blocks | 
|  | 713 | *	@offsets: offsets (in the blocks) to store the pointers to next. | 
|  | 714 | *	@branch: place to store the chain in. | 
|  | 715 | * | 
|  | 716 | *	This function allocates blocks, zeroes out all but the last one, | 
|  | 717 | *	links them into chain and (if we are synchronous) writes them to disk. | 
|  | 718 | *	In other words, it prepares a branch that can be spliced onto the | 
|  | 719 | *	inode. It stores the information about that chain in the branch[], in | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 720 | *	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] | 721 | *	we had read the existing part of chain and partial points to the last | 
|  | 722 | *	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] | 723 | *	picture as after the successful ext4_get_block(), except that in one | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 724 | *	place chain is disconnected - *branch->p is still zero (we did not | 
|  | 725 | *	set the last link), but branch->key contains the number that should | 
|  | 726 | *	be placed into *branch->p to fill that gap. | 
|  | 727 | * | 
|  | 728 | *	If allocation fails we free all blocks we've allocated (and forget | 
|  | 729 | *	their buffer_heads) and return the error value the from failed | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 730 | *	ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 731 | *	as described above and return 0. | 
|  | 732 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 733 | static int ext4_alloc_branch(handle_t *handle, struct inode *inode, | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 734 | ext4_lblk_t iblock, int indirect_blks, | 
|  | 735 | int *blks, ext4_fsblk_t goal, | 
|  | 736 | ext4_lblk_t *offsets, Indirect *branch) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 737 | { | 
|  | 738 | int blocksize = inode->i_sb->s_blocksize; | 
|  | 739 | int i, n = 0; | 
|  | 740 | int err = 0; | 
|  | 741 | struct buffer_head *bh; | 
|  | 742 | int num; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 743 | ext4_fsblk_t new_blocks[4]; | 
|  | 744 | ext4_fsblk_t current_block; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 745 |  | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 746 | num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 747 | *blks, new_blocks, &err); | 
|  | 748 | if (err) | 
|  | 749 | return err; | 
|  | 750 |  | 
|  | 751 | branch[0].key = cpu_to_le32(new_blocks[0]); | 
|  | 752 | /* | 
|  | 753 | * metadata blocks and data blocks are allocated. | 
|  | 754 | */ | 
|  | 755 | for (n = 1; n <= indirect_blks;  n++) { | 
|  | 756 | /* | 
|  | 757 | * Get buffer_head for parent block, zero it out | 
|  | 758 | * and set the pointer to new one, then send | 
|  | 759 | * parent to disk. | 
|  | 760 | */ | 
|  | 761 | bh = sb_getblk(inode->i_sb, new_blocks[n-1]); | 
|  | 762 | branch[n].bh = bh; | 
|  | 763 | lock_buffer(bh); | 
|  | 764 | BUFFER_TRACE(bh, "call get_create_access"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 765 | err = ext4_journal_get_create_access(handle, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 766 | if (err) { | 
|  | 767 | unlock_buffer(bh); | 
|  | 768 | brelse(bh); | 
|  | 769 | goto failed; | 
|  | 770 | } | 
|  | 771 |  | 
|  | 772 | memset(bh->b_data, 0, blocksize); | 
|  | 773 | branch[n].p = (__le32 *) bh->b_data + offsets[n]; | 
|  | 774 | branch[n].key = cpu_to_le32(new_blocks[n]); | 
|  | 775 | *branch[n].p = branch[n].key; | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 776 | if (n == indirect_blks) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 777 | current_block = new_blocks[n]; | 
|  | 778 | /* | 
|  | 779 | * End of chain, update the last new metablock of | 
|  | 780 | * the chain to point to the new allocated | 
|  | 781 | * data blocks numbers | 
|  | 782 | */ | 
|  | 783 | for (i=1; i < num; i++) | 
|  | 784 | *(branch[n].p + i) = cpu_to_le32(++current_block); | 
|  | 785 | } | 
|  | 786 | BUFFER_TRACE(bh, "marking uptodate"); | 
|  | 787 | set_buffer_uptodate(bh); | 
|  | 788 | unlock_buffer(bh); | 
|  | 789 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 790 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); | 
|  | 791 | err = ext4_handle_dirty_metadata(handle, inode, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 792 | if (err) | 
|  | 793 | goto failed; | 
|  | 794 | } | 
|  | 795 | *blks = num; | 
|  | 796 | return err; | 
|  | 797 | failed: | 
|  | 798 | /* Allocation failed, free what we already allocated */ | 
|  | 799 | for (i = 1; i <= n ; i++) { | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 800 | BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 801 | ext4_journal_forget(handle, branch[i].bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 802 | } | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 803 | for (i = 0; i < indirect_blks; i++) | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 804 | ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 805 |  | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 806 | ext4_free_blocks(handle, inode, new_blocks[i], num, 0); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 807 |  | 
|  | 808 | return err; | 
|  | 809 | } | 
|  | 810 |  | 
|  | 811 | /** | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 812 | * ext4_splice_branch - splice the allocated branch onto inode. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 813 | * @inode: owner | 
|  | 814 | * @block: (logical) number of block we are adding | 
|  | 815 | * @chain: chain of indirect blocks (with a missing link - see | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 816 | *	ext4_alloc_branch) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 817 | * @where: location of missing link | 
|  | 818 | * @num:   number of indirect blocks we are adding | 
|  | 819 | * @blks:  number of direct blocks we are adding | 
|  | 820 | * | 
|  | 821 | * This function fills the missing link and does all housekeeping needed in | 
|  | 822 | * inode (->i_blocks, etc.). In case of success we end up with the full | 
|  | 823 | * chain to new block and return 0. | 
|  | 824 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 825 | static int ext4_splice_branch(handle_t *handle, struct inode *inode, | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 826 | ext4_lblk_t block, Indirect *where, int num, int blks) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 827 | { | 
|  | 828 | int i; | 
|  | 829 | int err = 0; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 830 | ext4_fsblk_t current_block; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 831 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 832 | /* | 
|  | 833 | * If we're splicing into a [td]indirect block (as opposed to the | 
|  | 834 | * inode) then we need to get write access to the [td]indirect block | 
|  | 835 | * before the splice. | 
|  | 836 | */ | 
|  | 837 | if (where->bh) { | 
|  | 838 | BUFFER_TRACE(where->bh, "get_write_access"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 839 | err = ext4_journal_get_write_access(handle, where->bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 840 | if (err) | 
|  | 841 | goto err_out; | 
|  | 842 | } | 
|  | 843 | /* That's it */ | 
|  | 844 |  | 
|  | 845 | *where->p = where->key; | 
|  | 846 |  | 
|  | 847 | /* | 
|  | 848 | * Update the host buffer_head or inode to point to more just allocated | 
|  | 849 | * direct blocks blocks | 
|  | 850 | */ | 
|  | 851 | if (num == 0 && blks > 1) { | 
|  | 852 | current_block = le32_to_cpu(where->key) + 1; | 
|  | 853 | for (i = 1; i < blks; i++) | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 854 | *(where->p + i) = cpu_to_le32(current_block++); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 855 | } | 
|  | 856 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 857 | /* We are done with atomic stuff, now do the rest of housekeeping */ | 
|  | 858 |  | 
| Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 859 | inode->i_ctime = ext4_current_time(inode); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 860 | ext4_mark_inode_dirty(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 861 |  | 
|  | 862 | /* had we spliced it onto indirect block? */ | 
|  | 863 | if (where->bh) { | 
|  | 864 | /* | 
|  | 865 | * If we spliced it onto an indirect block, we haven't | 
|  | 866 | * altered the inode.  Note however that if it is being spliced | 
|  | 867 | * onto an indirect block at the very end of the file (the | 
|  | 868 | * file is growing) then we *will* alter the inode to reflect | 
|  | 869 | * 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] | 870 | * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 871 | */ | 
|  | 872 | jbd_debug(5, "splicing indirect only\n"); | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 873 | BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata"); | 
|  | 874 | err = ext4_handle_dirty_metadata(handle, inode, where->bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 875 | if (err) | 
|  | 876 | goto err_out; | 
|  | 877 | } else { | 
|  | 878 | /* | 
|  | 879 | * OK, we spliced it into the inode itself on a direct block. | 
|  | 880 | * Inode was dirtied above. | 
|  | 881 | */ | 
|  | 882 | jbd_debug(5, "splicing direct\n"); | 
|  | 883 | } | 
|  | 884 | return err; | 
|  | 885 |  | 
|  | 886 | err_out: | 
|  | 887 | for (i = 1; i <= num; i++) { | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 888 | BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 889 | ext4_journal_forget(handle, where[i].bh); | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 890 | ext4_free_blocks(handle, inode, | 
|  | 891 | le32_to_cpu(where[i-1].key), 1, 0); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 892 | } | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 893 | 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] | 894 |  | 
|  | 895 | return err; | 
|  | 896 | } | 
|  | 897 |  | 
|  | 898 | /* | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 899 | * The ext4_ind_get_blocks() function handles non-extents inodes | 
|  | 900 | * (i.e., using the traditional indirect/double-indirect i_blocks | 
|  | 901 | * scheme) for ext4_get_blocks(). | 
|  | 902 | * | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 903 | * Allocation strategy is simple: if we have to allocate something, we will | 
|  | 904 | * have to go the whole way to leaf. So let's do it before attaching anything | 
|  | 905 | * to tree, set linkage between the newborn blocks, write them if sync is | 
|  | 906 | * required, recheck the path, free and repeat if check fails, otherwise | 
|  | 907 | * set the last missing link (that will protect us from any truncate-generated | 
|  | 908 | * removals - all blocks on the path are immune now) and possibly force the | 
|  | 909 | * write on the parent block. | 
|  | 910 | * That has a nice additional property: no special recovery from the failed | 
|  | 911 | * allocations is needed - we simply release blocks and do not touch anything | 
|  | 912 | * reachable from inode. | 
|  | 913 | * | 
|  | 914 | * `handle' can be NULL if create == 0. | 
|  | 915 | * | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 916 | * return > 0, # of blocks mapped or allocated. | 
|  | 917 | * return = 0, if plain lookup failed. | 
|  | 918 | * return < 0, error case. | 
| Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 919 | * | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 920 | * The ext4_ind_get_blocks() function should be called with | 
|  | 921 | * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem | 
|  | 922 | * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or | 
|  | 923 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system | 
|  | 924 | * blocks. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 925 | */ | 
| Theodore Ts'o | e4d996c | 2009-05-12 00:25:28 -0400 | [diff] [blame] | 926 | static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, | 
| Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 927 | ext4_lblk_t iblock, unsigned int maxblocks, | 
|  | 928 | struct buffer_head *bh_result, | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 929 | int flags) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 930 | { | 
|  | 931 | int err = -EIO; | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 932 | ext4_lblk_t offsets[4]; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 933 | Indirect chain[4]; | 
|  | 934 | Indirect *partial; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 935 | ext4_fsblk_t goal; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 936 | int indirect_blks; | 
|  | 937 | int blocks_to_boundary = 0; | 
|  | 938 | int depth; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 939 | int count = 0; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 940 | ext4_fsblk_t first_block = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 941 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 942 | J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 943 | J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0); | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 944 | depth = ext4_block_to_path(inode, iblock, offsets, | 
|  | 945 | &blocks_to_boundary); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 946 |  | 
|  | 947 | if (depth == 0) | 
|  | 948 | goto out; | 
|  | 949 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 950 | partial = ext4_get_branch(inode, depth, offsets, chain, &err); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 951 |  | 
|  | 952 | /* Simplest case - block found, no allocation needed */ | 
|  | 953 | if (!partial) { | 
|  | 954 | first_block = le32_to_cpu(chain[depth - 1].key); | 
|  | 955 | clear_buffer_new(bh_result); | 
|  | 956 | count++; | 
|  | 957 | /*map more blocks*/ | 
|  | 958 | while (count < maxblocks && count <= blocks_to_boundary) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 959 | ext4_fsblk_t blk; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 960 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 961 | blk = le32_to_cpu(*(chain[depth-1].p + count)); | 
|  | 962 |  | 
|  | 963 | if (blk == first_block + count) | 
|  | 964 | count++; | 
|  | 965 | else | 
|  | 966 | break; | 
|  | 967 | } | 
| Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 968 | goto got_it; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 969 | } | 
|  | 970 |  | 
|  | 971 | /* Next simple case - plain lookup or failed read of indirect block */ | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 972 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 973 | goto cleanup; | 
|  | 974 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 975 | /* | 
| Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 976 | * Okay, we need to do block allocation. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 977 | */ | 
| Akinobu Mita | fb01bfd | 2008-02-06 01:40:16 -0800 | [diff] [blame] | 978 | goal = ext4_find_goal(inode, iblock, partial); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 979 |  | 
|  | 980 | /* the number of blocks need to allocate for [d,t]indirect blocks */ | 
|  | 981 | indirect_blks = (chain + depth) - partial - 1; | 
|  | 982 |  | 
|  | 983 | /* | 
|  | 984 | * Next look up the indirect map to count the totoal number of | 
|  | 985 | * direct blocks to allocate for this branch. | 
|  | 986 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 987 | count = ext4_blks_to_allocate(partial, indirect_blks, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 988 | maxblocks, blocks_to_boundary); | 
|  | 989 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 990 | * Block out ext4_truncate while we alter the tree | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 991 | */ | 
| Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 992 | err = ext4_alloc_branch(handle, inode, iblock, indirect_blks, | 
|  | 993 | &count, goal, | 
|  | 994 | offsets + (partial - chain), partial); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 995 |  | 
|  | 996 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 997 | * The ext4_splice_branch call will free and forget any buffers | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 998 | * on the new chain if there is a failure, but that risks using | 
|  | 999 | * up transaction credits, especially for bitmaps where the | 
|  | 1000 | * credits cannot be returned.  Can we handle this somehow?  We | 
|  | 1001 | * may need to return -EAGAIN upwards in the worst case.  --sct | 
|  | 1002 | */ | 
|  | 1003 | if (!err) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1004 | err = ext4_splice_branch(handle, inode, iblock, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1005 | partial, indirect_blks, count); | 
| Jan Kara | 03f5d8b | 2009-06-09 00:17:05 -0400 | [diff] [blame] | 1006 | else | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1007 | goto cleanup; | 
|  | 1008 |  | 
|  | 1009 | set_buffer_new(bh_result); | 
|  | 1010 | got_it: | 
|  | 1011 | map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); | 
|  | 1012 | if (count > blocks_to_boundary) | 
|  | 1013 | set_buffer_boundary(bh_result); | 
|  | 1014 | err = count; | 
|  | 1015 | /* Clean up and exit */ | 
|  | 1016 | partial = chain + depth - 1;	/* the whole chain */ | 
|  | 1017 | cleanup: | 
|  | 1018 | while (partial > chain) { | 
|  | 1019 | BUFFER_TRACE(partial->bh, "call brelse"); | 
|  | 1020 | brelse(partial->bh); | 
|  | 1021 | partial--; | 
|  | 1022 | } | 
|  | 1023 | BUFFER_TRACE(bh_result, "returned"); | 
|  | 1024 | out: | 
|  | 1025 | return err; | 
|  | 1026 | } | 
|  | 1027 |  | 
| Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1028 | qsize_t ext4_get_reserved_space(struct inode *inode) | 
|  | 1029 | { | 
|  | 1030 | unsigned long long total; | 
|  | 1031 |  | 
|  | 1032 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | 
|  | 1033 | total = EXT4_I(inode)->i_reserved_data_blocks + | 
|  | 1034 | EXT4_I(inode)->i_reserved_meta_blocks; | 
|  | 1035 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 
|  | 1036 |  | 
|  | 1037 | return total; | 
|  | 1038 | } | 
| Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1039 | /* | 
|  | 1040 | * Calculate the number of metadata blocks need to reserve | 
|  | 1041 | * to allocate @blocks for non extent file based file | 
|  | 1042 | */ | 
|  | 1043 | static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks) | 
|  | 1044 | { | 
|  | 1045 | int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb); | 
|  | 1046 | int ind_blks, dind_blks, tind_blks; | 
|  | 1047 |  | 
|  | 1048 | /* number of new indirect blocks needed */ | 
|  | 1049 | ind_blks = (blocks + icap - 1) / icap; | 
|  | 1050 |  | 
|  | 1051 | dind_blks = (ind_blks + icap - 1) / icap; | 
|  | 1052 |  | 
|  | 1053 | tind_blks = 1; | 
|  | 1054 |  | 
|  | 1055 | return ind_blks + dind_blks + tind_blks; | 
|  | 1056 | } | 
|  | 1057 |  | 
|  | 1058 | /* | 
|  | 1059 | * Calculate the number of metadata blocks need to reserve | 
|  | 1060 | * to allocate given number of blocks | 
|  | 1061 | */ | 
|  | 1062 | static int ext4_calc_metadata_amount(struct inode *inode, int blocks) | 
|  | 1063 | { | 
| Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1064 | if (!blocks) | 
|  | 1065 | return 0; | 
|  | 1066 |  | 
| Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1067 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) | 
|  | 1068 | return ext4_ext_calc_metadata_amount(inode, blocks); | 
|  | 1069 |  | 
|  | 1070 | return ext4_indirect_calc_metadata_amount(inode, blocks); | 
|  | 1071 | } | 
|  | 1072 |  | 
|  | 1073 | static void ext4_da_update_reserve_space(struct inode *inode, int used) | 
|  | 1074 | { | 
|  | 1075 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
|  | 1076 | int total, mdb, mdb_free; | 
|  | 1077 |  | 
|  | 1078 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | 
|  | 1079 | /* recalculate the number of metablocks still need to be reserved */ | 
|  | 1080 | total = EXT4_I(inode)->i_reserved_data_blocks - used; | 
|  | 1081 | mdb = ext4_calc_metadata_amount(inode, total); | 
|  | 1082 |  | 
|  | 1083 | /* figure out how many metablocks to release */ | 
|  | 1084 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); | 
|  | 1085 | mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; | 
|  | 1086 |  | 
| Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 1087 | if (mdb_free) { | 
|  | 1088 | /* Account for allocated meta_blocks */ | 
|  | 1089 | mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks; | 
| Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1090 |  | 
| Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 1091 | /* update fs dirty blocks counter */ | 
|  | 1092 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free); | 
|  | 1093 | EXT4_I(inode)->i_allocated_meta_blocks = 0; | 
|  | 1094 | EXT4_I(inode)->i_reserved_meta_blocks = mdb; | 
|  | 1095 | } | 
| Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1096 |  | 
|  | 1097 | /* update per-inode reservations */ | 
|  | 1098 | BUG_ON(used  > EXT4_I(inode)->i_reserved_data_blocks); | 
|  | 1099 | EXT4_I(inode)->i_reserved_data_blocks -= used; | 
| Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1100 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 
| Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1101 |  | 
|  | 1102 | /* | 
|  | 1103 | * free those over-booking quota for metadata blocks | 
|  | 1104 | */ | 
| Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1105 | if (mdb_free) | 
|  | 1106 | vfs_dq_release_reservation_block(inode, mdb_free); | 
| Aneesh Kumar K.V | d601430 | 2009-03-27 22:36:43 -0400 | [diff] [blame] | 1107 |  | 
|  | 1108 | /* | 
|  | 1109 | * If we have done all the pending block allocations and if | 
|  | 1110 | * there aren't any writers on the inode, we can discard the | 
|  | 1111 | * inode's preallocations. | 
|  | 1112 | */ | 
|  | 1113 | if (!total && (atomic_read(&inode->i_writecount) == 0)) | 
|  | 1114 | ext4_discard_preallocations(inode); | 
| Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1115 | } | 
|  | 1116 |  | 
| Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 1117 | static int check_block_validity(struct inode *inode, sector_t logical, | 
|  | 1118 | sector_t phys, int len) | 
|  | 1119 | { | 
|  | 1120 | if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) { | 
|  | 1121 | ext4_error(inode->i_sb, "check_block_validity", | 
|  | 1122 | "inode #%lu logical block %llu mapped to %llu " | 
|  | 1123 | "(size %d)", inode->i_ino, | 
|  | 1124 | (unsigned long long) logical, | 
|  | 1125 | (unsigned long long) phys, len); | 
|  | 1126 | WARN_ON(1); | 
|  | 1127 | return -EIO; | 
|  | 1128 | } | 
|  | 1129 | return 0; | 
|  | 1130 | } | 
|  | 1131 |  | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1132 | /* | 
| Theodore Ts'o | 12b7ac1 | 2009-05-14 00:57:44 -0400 | [diff] [blame] | 1133 | * 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] | 1134 | * and returns if the blocks are already mapped. | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1135 | * | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1136 | * Otherwise it takes the write lock of the i_data_sem and allocate blocks | 
|  | 1137 | * and store the allocated blocks in the result buffer head and mark it | 
|  | 1138 | * mapped. | 
|  | 1139 | * | 
|  | 1140 | * 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] | 1141 | * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1142 | * based files | 
|  | 1143 | * | 
|  | 1144 | * On success, it returns the number of blocks being mapped or allocate. | 
|  | 1145 | * if create==0 and the blocks are pre-allocated and uninitialized block, | 
|  | 1146 | * the result buffer head is unmapped. If the create ==1, it will make sure | 
|  | 1147 | * the buffer head is mapped. | 
|  | 1148 | * | 
|  | 1149 | * It returns 0 if plain look up failed (blocks have not been allocated), in | 
|  | 1150 | * that casem, buffer head is unmapped | 
|  | 1151 | * | 
|  | 1152 | * It returns the error in case of allocation failure. | 
|  | 1153 | */ | 
| Theodore Ts'o | 12b7ac1 | 2009-05-14 00:57:44 -0400 | [diff] [blame] | 1154 | int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, | 
|  | 1155 | unsigned int max_blocks, struct buffer_head *bh, | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1156 | int flags) | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1157 | { | 
|  | 1158 | int retval; | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1159 |  | 
|  | 1160 | clear_buffer_mapped(bh); | 
| Aneesh Kumar K.V | 2a8964d | 2009-05-14 17:05:39 -0400 | [diff] [blame] | 1161 | clear_buffer_unwritten(bh); | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1162 |  | 
| Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1163 | /* | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 1164 | * Try to see if we can get the block without requesting a new | 
|  | 1165 | * file system block. | 
| Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1166 | */ | 
|  | 1167 | down_read((&EXT4_I(inode)->i_data_sem)); | 
|  | 1168 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { | 
|  | 1169 | retval =  ext4_ext_get_blocks(handle, inode, block, max_blocks, | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1170 | bh, 0); | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1171 | } else { | 
| Theodore Ts'o | e4d996c | 2009-05-12 00:25:28 -0400 | [diff] [blame] | 1172 | retval = ext4_ind_get_blocks(handle, inode, block, max_blocks, | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1173 | bh, 0); | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1174 | } | 
| Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1175 | up_read((&EXT4_I(inode)->i_data_sem)); | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1176 |  | 
| Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 1177 | if (retval > 0 && buffer_mapped(bh)) { | 
|  | 1178 | int ret = check_block_validity(inode, block, | 
|  | 1179 | bh->b_blocknr, retval); | 
|  | 1180 | if (ret != 0) | 
|  | 1181 | return ret; | 
|  | 1182 | } | 
|  | 1183 |  | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1184 | /* If it is only a block(s) look up */ | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1185 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) | 
| Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1186 | return retval; | 
|  | 1187 |  | 
|  | 1188 | /* | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1189 | * Returns if the blocks have already allocated | 
|  | 1190 | * | 
|  | 1191 | * Note that if blocks have been preallocated | 
|  | 1192 | * ext4_ext_get_block() returns th create = 0 | 
|  | 1193 | * with buffer head unmapped. | 
|  | 1194 | */ | 
|  | 1195 | if (retval > 0 && buffer_mapped(bh)) | 
|  | 1196 | return retval; | 
|  | 1197 |  | 
|  | 1198 | /* | 
| Aneesh Kumar K.V | 2a8964d | 2009-05-14 17:05:39 -0400 | [diff] [blame] | 1199 | * When we call get_blocks without the create flag, the | 
|  | 1200 | * BH_Unwritten flag could have gotten set if the blocks | 
|  | 1201 | * requested were part of a uninitialized extent.  We need to | 
|  | 1202 | * clear this flag now that we are committed to convert all or | 
|  | 1203 | * part of the uninitialized extent to be an initialized | 
|  | 1204 | * extent.  This is because we need to avoid the combination | 
|  | 1205 | * of BH_Unwritten and BH_Mapped flags being simultaneously | 
|  | 1206 | * set on the buffer_head. | 
|  | 1207 | */ | 
|  | 1208 | clear_buffer_unwritten(bh); | 
|  | 1209 |  | 
|  | 1210 | /* | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 1211 | * New blocks allocate and/or writing to uninitialized extent | 
|  | 1212 | * will possibly result in updating i_data, so we take | 
|  | 1213 | * the write lock of i_data_sem, and call get_blocks() | 
|  | 1214 | * with create == 1 flag. | 
| Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1215 | */ | 
|  | 1216 | down_write((&EXT4_I(inode)->i_data_sem)); | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1217 |  | 
|  | 1218 | /* | 
|  | 1219 | * if the caller is from delayed allocation writeout path | 
|  | 1220 | * we have already reserved fs blocks for allocation | 
|  | 1221 | * let the underlying get_block() function know to | 
|  | 1222 | * avoid double accounting | 
|  | 1223 | */ | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1224 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1225 | EXT4_I(inode)->i_delalloc_reserved_flag = 1; | 
| Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1226 | /* | 
|  | 1227 | * We need to check for EXT4 here because migrate | 
|  | 1228 | * could have changed the inode type in between | 
|  | 1229 | */ | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1230 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { | 
|  | 1231 | retval =  ext4_ext_get_blocks(handle, inode, block, max_blocks, | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1232 | bh, flags); | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1233 | } else { | 
| Theodore Ts'o | e4d996c | 2009-05-12 00:25:28 -0400 | [diff] [blame] | 1234 | retval = ext4_ind_get_blocks(handle, inode, block, | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1235 | max_blocks, bh, flags); | 
| Aneesh Kumar K.V | 267e4db | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 1236 |  | 
|  | 1237 | if (retval > 0 && buffer_new(bh)) { | 
|  | 1238 | /* | 
|  | 1239 | * We allocated new blocks which will result in | 
|  | 1240 | * i_data's format changing.  Force the migrate | 
|  | 1241 | * to fail by clearing migrate flags | 
|  | 1242 | */ | 
|  | 1243 | EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags & | 
|  | 1244 | ~EXT4_EXT_MIGRATE; | 
|  | 1245 | } | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1246 | } | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1247 |  | 
| Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 1248 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1249 | EXT4_I(inode)->i_delalloc_reserved_flag = 0; | 
| Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 1250 |  | 
|  | 1251 | /* | 
|  | 1252 | * Update reserved blocks/metadata blocks after successful | 
|  | 1253 | * block allocation which had been deferred till now. | 
|  | 1254 | */ | 
|  | 1255 | if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE)) | 
|  | 1256 | ext4_da_update_reserve_space(inode, retval); | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1257 |  | 
| Aneesh Kumar K.V | 4df3d26 | 2008-01-28 23:58:29 -0500 | [diff] [blame] | 1258 | up_write((&EXT4_I(inode)->i_data_sem)); | 
| Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 1259 | if (retval > 0 && buffer_mapped(bh)) { | 
|  | 1260 | int ret = check_block_validity(inode, block, | 
|  | 1261 | bh->b_blocknr, retval); | 
|  | 1262 | if (ret != 0) | 
|  | 1263 | return ret; | 
|  | 1264 | } | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1265 | return retval; | 
|  | 1266 | } | 
|  | 1267 |  | 
| Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 1268 | /* Maximum number of blocks we map for direct IO at once. */ | 
|  | 1269 | #define DIO_MAX_BLOCKS 4096 | 
|  | 1270 |  | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1271 | int ext4_get_block(struct inode *inode, sector_t iblock, | 
|  | 1272 | struct buffer_head *bh_result, int create) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1273 | { | 
| Dmitriy Monakhov | 3e4fdaf | 2007-02-10 01:46:35 -0800 | [diff] [blame] | 1274 | handle_t *handle = ext4_journal_current_handle(); | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1275 | int ret = 0, started = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1276 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; | 
| Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 1277 | int dio_credits; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1278 |  | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1279 | if (create && !handle) { | 
|  | 1280 | /* Direct IO write... */ | 
|  | 1281 | if (max_blocks > DIO_MAX_BLOCKS) | 
|  | 1282 | max_blocks = DIO_MAX_BLOCKS; | 
| Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 1283 | dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); | 
|  | 1284 | handle = ext4_journal_start(inode, dio_credits); | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1285 | if (IS_ERR(handle)) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1286 | ret = PTR_ERR(handle); | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1287 | goto out; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1288 | } | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1289 | started = 1; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1290 | } | 
|  | 1291 |  | 
| Theodore Ts'o | 12b7ac1 | 2009-05-14 00:57:44 -0400 | [diff] [blame] | 1292 | ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1293 | create ? EXT4_GET_BLOCKS_CREATE : 0); | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1294 | if (ret > 0) { | 
|  | 1295 | bh_result->b_size = (ret << inode->i_blkbits); | 
|  | 1296 | ret = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1297 | } | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 1298 | if (started) | 
|  | 1299 | ext4_journal_stop(handle); | 
|  | 1300 | out: | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1301 | return ret; | 
|  | 1302 | } | 
|  | 1303 |  | 
|  | 1304 | /* | 
|  | 1305 | * `handle' can be NULL if create is zero | 
|  | 1306 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1307 | 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] | 1308 | ext4_lblk_t block, int create, int *errp) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1309 | { | 
|  | 1310 | struct buffer_head dummy; | 
|  | 1311 | int fatal = 0, err; | 
| Jan Kara | 03f5d8b | 2009-06-09 00:17:05 -0400 | [diff] [blame] | 1312 | int flags = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1313 |  | 
|  | 1314 | J_ASSERT(handle != NULL || create == 0); | 
|  | 1315 |  | 
|  | 1316 | dummy.b_state = 0; | 
|  | 1317 | dummy.b_blocknr = -1000; | 
|  | 1318 | buffer_trace_init(&dummy.b_history); | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1319 | if (create) | 
|  | 1320 | flags |= EXT4_GET_BLOCKS_CREATE; | 
|  | 1321 | err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1322 | /* | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 1323 | * ext4_get_blocks() returns number of blocks mapped. 0 in | 
|  | 1324 | * case of a HOLE. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1325 | */ | 
|  | 1326 | if (err > 0) { | 
|  | 1327 | if (err > 1) | 
|  | 1328 | WARN_ON(1); | 
|  | 1329 | err = 0; | 
|  | 1330 | } | 
|  | 1331 | *errp = err; | 
|  | 1332 | if (!err && buffer_mapped(&dummy)) { | 
|  | 1333 | struct buffer_head *bh; | 
|  | 1334 | bh = sb_getblk(inode->i_sb, dummy.b_blocknr); | 
|  | 1335 | if (!bh) { | 
|  | 1336 | *errp = -EIO; | 
|  | 1337 | goto err; | 
|  | 1338 | } | 
|  | 1339 | if (buffer_new(&dummy)) { | 
|  | 1340 | J_ASSERT(create != 0); | 
| Aneesh Kumar K.V | ac39849 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1341 | J_ASSERT(handle != NULL); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1342 |  | 
|  | 1343 | /* | 
|  | 1344 | * Now that we do not always journal data, we should | 
|  | 1345 | * keep in mind whether this should always journal the | 
|  | 1346 | * new buffer as metadata.  For now, regular file | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1347 | * writes use ext4_get_block instead, so it's not a | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1348 | * problem. | 
|  | 1349 | */ | 
|  | 1350 | lock_buffer(bh); | 
|  | 1351 | BUFFER_TRACE(bh, "call get_create_access"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1352 | fatal = ext4_journal_get_create_access(handle, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1353 | if (!fatal && !buffer_uptodate(bh)) { | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1354 | memset(bh->b_data, 0, inode->i_sb->s_blocksize); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1355 | set_buffer_uptodate(bh); | 
|  | 1356 | } | 
|  | 1357 | unlock_buffer(bh); | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1358 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); | 
|  | 1359 | err = ext4_handle_dirty_metadata(handle, inode, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1360 | if (!fatal) | 
|  | 1361 | fatal = err; | 
|  | 1362 | } else { | 
|  | 1363 | BUFFER_TRACE(bh, "not a new buffer"); | 
|  | 1364 | } | 
|  | 1365 | if (fatal) { | 
|  | 1366 | *errp = fatal; | 
|  | 1367 | brelse(bh); | 
|  | 1368 | bh = NULL; | 
|  | 1369 | } | 
|  | 1370 | return bh; | 
|  | 1371 | } | 
|  | 1372 | err: | 
|  | 1373 | return NULL; | 
|  | 1374 | } | 
|  | 1375 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1376 | 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] | 1377 | ext4_lblk_t block, int create, int *err) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1378 | { | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1379 | struct buffer_head *bh; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1380 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1381 | bh = ext4_getblk(handle, inode, block, create, err); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1382 | if (!bh) | 
|  | 1383 | return bh; | 
|  | 1384 | if (buffer_uptodate(bh)) | 
|  | 1385 | return bh; | 
|  | 1386 | ll_rw_block(READ_META, 1, &bh); | 
|  | 1387 | wait_on_buffer(bh); | 
|  | 1388 | if (buffer_uptodate(bh)) | 
|  | 1389 | return bh; | 
|  | 1390 | put_bh(bh); | 
|  | 1391 | *err = -EIO; | 
|  | 1392 | return NULL; | 
|  | 1393 | } | 
|  | 1394 |  | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1395 | static int walk_page_buffers(handle_t *handle, | 
|  | 1396 | struct buffer_head *head, | 
|  | 1397 | unsigned from, | 
|  | 1398 | unsigned to, | 
|  | 1399 | int *partial, | 
|  | 1400 | int (*fn)(handle_t *handle, | 
|  | 1401 | struct buffer_head *bh)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1402 | { | 
|  | 1403 | struct buffer_head *bh; | 
|  | 1404 | unsigned block_start, block_end; | 
|  | 1405 | unsigned blocksize = head->b_size; | 
|  | 1406 | int err, ret = 0; | 
|  | 1407 | struct buffer_head *next; | 
|  | 1408 |  | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1409 | for (bh = head, block_start = 0; | 
|  | 1410 | ret == 0 && (bh != head || !block_start); | 
|  | 1411 | block_start = block_end, bh = next) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1412 | { | 
|  | 1413 | next = bh->b_this_page; | 
|  | 1414 | block_end = block_start + blocksize; | 
|  | 1415 | if (block_end <= from || block_start >= to) { | 
|  | 1416 | if (partial && !buffer_uptodate(bh)) | 
|  | 1417 | *partial = 1; | 
|  | 1418 | continue; | 
|  | 1419 | } | 
|  | 1420 | err = (*fn)(handle, bh); | 
|  | 1421 | if (!ret) | 
|  | 1422 | ret = err; | 
|  | 1423 | } | 
|  | 1424 | return ret; | 
|  | 1425 | } | 
|  | 1426 |  | 
|  | 1427 | /* | 
|  | 1428 | * To preserve ordering, it is essential that the hole instantiation and | 
|  | 1429 | * the data write be encapsulated in a single transaction.  We cannot | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1430 | * 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] | 1431 | * 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] | 1432 | * prepare_write() is the right place. | 
|  | 1433 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1434 | * Also, this function can nest inside ext4_writepage() -> | 
|  | 1435 | * block_write_full_page(). In that case, we *know* that ext4_writepage() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1436 | * has generated enough buffer credits to do the whole page.  So we won't | 
|  | 1437 | * block on the journal in that case, which is good, because the caller may | 
|  | 1438 | * be PF_MEMALLOC. | 
|  | 1439 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1440 | * By accident, ext4 can be reentered when a transaction is open via | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1441 | * quota file writes.  If we were to commit the transaction while thus | 
|  | 1442 | * reentered, there can be a deadlock - we would be holding a quota | 
|  | 1443 | * lock, and the commit would never complete if another thread had a | 
|  | 1444 | * transaction open and was blocking on the quota lock - a ranking | 
|  | 1445 | * violation. | 
|  | 1446 | * | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 1447 | * 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] | 1448 | * will _not_ run commit under these circumstances because handle->h_ref | 
|  | 1449 | * is elevated.  We'll still have enough credits for the tiny quotafile | 
|  | 1450 | * write. | 
|  | 1451 | */ | 
|  | 1452 | static int do_journal_get_write_access(handle_t *handle, | 
|  | 1453 | struct buffer_head *bh) | 
|  | 1454 | { | 
|  | 1455 | if (!buffer_mapped(bh) || buffer_freed(bh)) | 
|  | 1456 | return 0; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1457 | return ext4_journal_get_write_access(handle, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1458 | } | 
|  | 1459 |  | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1460 | static int ext4_write_begin(struct file *file, struct address_space *mapping, | 
|  | 1461 | loff_t pos, unsigned len, unsigned flags, | 
|  | 1462 | struct page **pagep, void **fsdata) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1463 | { | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1464 | struct inode *inode = mapping->host; | 
| Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 1465 | int ret, needed_blocks; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1466 | handle_t *handle; | 
|  | 1467 | int retries = 0; | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1468 | struct page *page; | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1469 | pgoff_t index; | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1470 | unsigned from, to; | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1471 |  | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 1472 | trace_ext4_write_begin(inode, pos, len, flags); | 
| Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 1473 | /* | 
|  | 1474 | * Reserve one block more for addition to orphan list in case | 
|  | 1475 | * we allocate blocks but write fails for some reason | 
|  | 1476 | */ | 
|  | 1477 | needed_blocks = ext4_writepage_trans_blocks(inode) + 1; | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1478 | index = pos >> PAGE_CACHE_SHIFT; | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1479 | from = pos & (PAGE_CACHE_SIZE - 1); | 
|  | 1480 | to = from + len; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1481 |  | 
|  | 1482 | retry: | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1483 | handle = ext4_journal_start(inode, needed_blocks); | 
|  | 1484 | if (IS_ERR(handle)) { | 
|  | 1485 | ret = PTR_ERR(handle); | 
|  | 1486 | goto out; | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1487 | } | 
|  | 1488 |  | 
| Jan Kara | ebd3610 | 2009-02-22 21:09:59 -0500 | [diff] [blame] | 1489 | /* We cannot recurse into the filesystem as the transaction is already | 
|  | 1490 | * started */ | 
|  | 1491 | flags |= AOP_FLAG_NOFS; | 
|  | 1492 |  | 
| Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 1493 | page = grab_cache_page_write_begin(mapping, index, flags); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1494 | if (!page) { | 
|  | 1495 | ext4_journal_stop(handle); | 
|  | 1496 | ret = -ENOMEM; | 
|  | 1497 | goto out; | 
|  | 1498 | } | 
|  | 1499 | *pagep = page; | 
|  | 1500 |  | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1501 | ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, | 
| Jan Kara | ebd3610 | 2009-02-22 21:09:59 -0500 | [diff] [blame] | 1502 | ext4_get_block); | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1503 |  | 
|  | 1504 | if (!ret && ext4_should_journal_data(inode)) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1505 | ret = walk_page_buffers(handle, page_buffers(page), | 
|  | 1506 | from, to, NULL, do_journal_get_write_access); | 
|  | 1507 | } | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1508 |  | 
|  | 1509 | if (ret) { | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1510 | unlock_page(page); | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1511 | page_cache_release(page); | 
| Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 1512 | /* | 
|  | 1513 | * block_write_begin may have instantiated a few blocks | 
|  | 1514 | * outside i_size.  Trim these off again. Don't need | 
|  | 1515 | * i_size_read because we hold i_mutex. | 
| Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 1516 | * | 
|  | 1517 | * Add inode to orphan list in case we crash before | 
|  | 1518 | * truncate finishes | 
| Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 1519 | */ | 
|  | 1520 | if (pos + len > inode->i_size) | 
| Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 1521 | ext4_orphan_add(handle, inode); | 
|  | 1522 |  | 
|  | 1523 | ext4_journal_stop(handle); | 
|  | 1524 | if (pos + len > inode->i_size) { | 
| Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 1525 | vmtruncate(inode, inode->i_size); | 
| Aneesh Kumar K.V | 1938a15 | 2009-06-05 01:00:26 -0400 | [diff] [blame] | 1526 | /* | 
|  | 1527 | * If vmtruncate failed early the inode might | 
|  | 1528 | * still be on the orphan list; we need to | 
|  | 1529 | * make sure the inode is removed from the | 
|  | 1530 | * orphan list in that case. | 
|  | 1531 | */ | 
|  | 1532 | if (inode->i_nlink) | 
|  | 1533 | ext4_orphan_del(NULL, inode); | 
|  | 1534 | } | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1535 | } | 
|  | 1536 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1537 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1538 | goto retry; | 
| Andrew Morton | 7479d2b | 2007-04-01 23:49:44 -0700 | [diff] [blame] | 1539 | out: | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1540 | return ret; | 
|  | 1541 | } | 
|  | 1542 |  | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1543 | /* For write_end() in data=journal mode */ | 
|  | 1544 | static int write_end_fn(handle_t *handle, struct buffer_head *bh) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1545 | { | 
|  | 1546 | if (!buffer_mapped(bh) || buffer_freed(bh)) | 
|  | 1547 | return 0; | 
|  | 1548 | set_buffer_uptodate(bh); | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1549 | return ext4_handle_dirty_metadata(handle, NULL, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1550 | } | 
|  | 1551 |  | 
| Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1552 | static int ext4_generic_write_end(struct file *file, | 
|  | 1553 | struct address_space *mapping, | 
|  | 1554 | loff_t pos, unsigned len, unsigned copied, | 
|  | 1555 | struct page *page, void *fsdata) | 
|  | 1556 | { | 
|  | 1557 | int i_size_changed = 0; | 
|  | 1558 | struct inode *inode = mapping->host; | 
|  | 1559 | handle_t *handle = ext4_journal_current_handle(); | 
|  | 1560 |  | 
|  | 1561 | copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); | 
|  | 1562 |  | 
|  | 1563 | /* | 
|  | 1564 | * No need to use i_size_read() here, the i_size | 
|  | 1565 | * cannot change under us because we hold i_mutex. | 
|  | 1566 | * | 
|  | 1567 | * But it's important to update i_size while still holding page lock: | 
|  | 1568 | * page writeout could otherwise come in and zero beyond i_size. | 
|  | 1569 | */ | 
|  | 1570 | if (pos + copied > inode->i_size) { | 
|  | 1571 | i_size_write(inode, pos + copied); | 
|  | 1572 | i_size_changed = 1; | 
|  | 1573 | } | 
|  | 1574 |  | 
|  | 1575 | if (pos + copied >  EXT4_I(inode)->i_disksize) { | 
|  | 1576 | /* We need to mark inode dirty even if | 
|  | 1577 | * new_i_size is less that inode->i_size | 
|  | 1578 | * bu greater than i_disksize.(hint delalloc) | 
|  | 1579 | */ | 
|  | 1580 | ext4_update_i_disksize(inode, (pos + copied)); | 
|  | 1581 | i_size_changed = 1; | 
|  | 1582 | } | 
|  | 1583 | unlock_page(page); | 
|  | 1584 | page_cache_release(page); | 
|  | 1585 |  | 
|  | 1586 | /* | 
|  | 1587 | * Don't mark the inode dirty under page lock. First, it unnecessarily | 
|  | 1588 | * makes the holding time of page lock longer. Second, it forces lock | 
|  | 1589 | * ordering of page lock and transaction start for journaling | 
|  | 1590 | * filesystems. | 
|  | 1591 | */ | 
|  | 1592 | if (i_size_changed) | 
|  | 1593 | ext4_mark_inode_dirty(handle, inode); | 
|  | 1594 |  | 
|  | 1595 | return copied; | 
|  | 1596 | } | 
|  | 1597 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1598 | /* | 
|  | 1599 | * We need to pick up the new inode size which generic_commit_write gave us | 
|  | 1600 | * `file' can be NULL - eg, when called from page_symlink(). | 
|  | 1601 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1602 | * ext4 never places buffers on inode->i_mapping->private_list.  metadata | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1603 | * buffers are managed internally. | 
|  | 1604 | */ | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1605 | static int ext4_ordered_write_end(struct file *file, | 
|  | 1606 | struct address_space *mapping, | 
|  | 1607 | loff_t pos, unsigned len, unsigned copied, | 
|  | 1608 | struct page *page, void *fsdata) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1609 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1610 | handle_t *handle = ext4_journal_current_handle(); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1611 | struct inode *inode = mapping->host; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1612 | int ret = 0, ret2; | 
|  | 1613 |  | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 1614 | trace_ext4_ordered_write_end(inode, pos, len, copied); | 
| Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1615 | ret = ext4_jbd2_file_inode(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1616 |  | 
|  | 1617 | if (ret == 0) { | 
| Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1618 | ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1619 | page, fsdata); | 
| Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 1620 | copied = ret2; | 
| Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1621 | if (pos + len > inode->i_size) | 
|  | 1622 | /* if we have allocated more blocks and copied | 
|  | 1623 | * less. We will have blocks allocated outside | 
|  | 1624 | * inode->i_size. So truncate them | 
|  | 1625 | */ | 
|  | 1626 | ext4_orphan_add(handle, inode); | 
| Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 1627 | if (ret2 < 0) | 
|  | 1628 | ret = ret2; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1629 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1630 | ret2 = ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1631 | if (!ret) | 
|  | 1632 | ret = ret2; | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1633 |  | 
| Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1634 | if (pos + len > inode->i_size) { | 
|  | 1635 | vmtruncate(inode, inode->i_size); | 
|  | 1636 | /* | 
|  | 1637 | * If vmtruncate failed early the inode might still be | 
|  | 1638 | * on the orphan list; we need to make sure the inode | 
|  | 1639 | * is removed from the orphan list in that case. | 
|  | 1640 | */ | 
|  | 1641 | if (inode->i_nlink) | 
|  | 1642 | ext4_orphan_del(NULL, inode); | 
|  | 1643 | } | 
|  | 1644 |  | 
|  | 1645 |  | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1646 | return ret ? ret : copied; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1647 | } | 
|  | 1648 |  | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1649 | static int ext4_writeback_write_end(struct file *file, | 
|  | 1650 | struct address_space *mapping, | 
|  | 1651 | loff_t pos, unsigned len, unsigned copied, | 
|  | 1652 | struct page *page, void *fsdata) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1653 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1654 | handle_t *handle = ext4_journal_current_handle(); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1655 | struct inode *inode = mapping->host; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1656 | int ret = 0, ret2; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1657 |  | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 1658 | trace_ext4_writeback_write_end(inode, pos, len, copied); | 
| Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1659 | ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1660 | page, fsdata); | 
| Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 1661 | copied = ret2; | 
| Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1662 | if (pos + len > inode->i_size) | 
|  | 1663 | /* if we have allocated more blocks and copied | 
|  | 1664 | * less. We will have blocks allocated outside | 
|  | 1665 | * inode->i_size. So truncate them | 
|  | 1666 | */ | 
|  | 1667 | ext4_orphan_add(handle, inode); | 
|  | 1668 |  | 
| Roel Kluin | f8a87d8 | 2008-04-29 22:01:18 -0400 | [diff] [blame] | 1669 | if (ret2 < 0) | 
|  | 1670 | ret = ret2; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1671 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1672 | ret2 = ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1673 | if (!ret) | 
|  | 1674 | ret = ret2; | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1675 |  | 
| Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1676 | if (pos + len > inode->i_size) { | 
|  | 1677 | vmtruncate(inode, inode->i_size); | 
|  | 1678 | /* | 
|  | 1679 | * If vmtruncate failed early the inode might still be | 
|  | 1680 | * on the orphan list; we need to make sure the inode | 
|  | 1681 | * is removed from the orphan list in that case. | 
|  | 1682 | */ | 
|  | 1683 | if (inode->i_nlink) | 
|  | 1684 | ext4_orphan_del(NULL, inode); | 
|  | 1685 | } | 
|  | 1686 |  | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1687 | return ret ? ret : copied; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1688 | } | 
|  | 1689 |  | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1690 | static int ext4_journalled_write_end(struct file *file, | 
|  | 1691 | struct address_space *mapping, | 
|  | 1692 | loff_t pos, unsigned len, unsigned copied, | 
|  | 1693 | struct page *page, void *fsdata) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1694 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1695 | handle_t *handle = ext4_journal_current_handle(); | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1696 | struct inode *inode = mapping->host; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1697 | int ret = 0, ret2; | 
|  | 1698 | int partial = 0; | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1699 | unsigned from, to; | 
| Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1700 | loff_t new_i_size; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1701 |  | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 1702 | trace_ext4_journalled_write_end(inode, pos, len, copied); | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1703 | from = pos & (PAGE_CACHE_SIZE - 1); | 
|  | 1704 | to = from + len; | 
|  | 1705 |  | 
|  | 1706 | if (copied < len) { | 
|  | 1707 | if (!PageUptodate(page)) | 
|  | 1708 | copied = 0; | 
|  | 1709 | page_zero_new_buffers(page, from+copied, to); | 
|  | 1710 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1711 |  | 
|  | 1712 | ret = walk_page_buffers(handle, page_buffers(page), from, | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1713 | to, &partial, write_end_fn); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1714 | if (!partial) | 
|  | 1715 | SetPageUptodate(page); | 
| Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1716 | new_i_size = pos + copied; | 
|  | 1717 | if (new_i_size > inode->i_size) | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1718 | i_size_write(inode, pos+copied); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1719 | EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; | 
| Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 1720 | if (new_i_size > EXT4_I(inode)->i_disksize) { | 
|  | 1721 | ext4_update_i_disksize(inode, new_i_size); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1722 | ret2 = ext4_mark_inode_dirty(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1723 | if (!ret) | 
|  | 1724 | ret = ret2; | 
|  | 1725 | } | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1726 |  | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1727 | unlock_page(page); | 
| Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1728 | page_cache_release(page); | 
|  | 1729 | if (pos + len > inode->i_size) | 
|  | 1730 | /* if we have allocated more blocks and copied | 
|  | 1731 | * less. We will have blocks allocated outside | 
|  | 1732 | * inode->i_size. So truncate them | 
|  | 1733 | */ | 
|  | 1734 | ext4_orphan_add(handle, inode); | 
|  | 1735 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1736 | ret2 = ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1737 | if (!ret) | 
|  | 1738 | ret = ret2; | 
| Aneesh Kumar K.V | f851408 | 2009-06-05 00:56:49 -0400 | [diff] [blame] | 1739 | if (pos + len > inode->i_size) { | 
|  | 1740 | vmtruncate(inode, inode->i_size); | 
|  | 1741 | /* | 
|  | 1742 | * If vmtruncate failed early the inode might still be | 
|  | 1743 | * on the orphan list; we need to make sure the inode | 
|  | 1744 | * is removed from the orphan list in that case. | 
|  | 1745 | */ | 
|  | 1746 | if (inode->i_nlink) | 
|  | 1747 | ext4_orphan_del(NULL, inode); | 
|  | 1748 | } | 
| Nick Piggin | bfc1af6 | 2007-10-16 01:25:05 -0700 | [diff] [blame] | 1749 |  | 
|  | 1750 | return ret ? ret : copied; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1751 | } | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1752 |  | 
|  | 1753 | static int ext4_da_reserve_space(struct inode *inode, int nrblocks) | 
|  | 1754 | { | 
| Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1755 | int retries = 0; | 
| Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1756 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
|  | 1757 | unsigned long md_needed, mdblocks, total = 0; | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1758 |  | 
|  | 1759 | /* | 
|  | 1760 | * recalculate the amount of metadata blocks to reserve | 
|  | 1761 | * in order to allocate nrblocks | 
|  | 1762 | * worse case is one extent per block | 
|  | 1763 | */ | 
| Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1764 | repeat: | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1765 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | 
|  | 1766 | total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks; | 
|  | 1767 | mdblocks = ext4_calc_metadata_amount(inode, total); | 
|  | 1768 | BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks); | 
|  | 1769 |  | 
|  | 1770 | md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks; | 
|  | 1771 | total = md_needed + nrblocks; | 
|  | 1772 |  | 
| Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1773 | /* | 
|  | 1774 | * Make quota reservation here to prevent quota overflow | 
|  | 1775 | * later. Real quota accounting is done at pages writeout | 
|  | 1776 | * time. | 
|  | 1777 | */ | 
|  | 1778 | if (vfs_dq_reserve_block(inode, total)) { | 
|  | 1779 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 
|  | 1780 | return -EDQUOT; | 
|  | 1781 | } | 
|  | 1782 |  | 
| Aneesh Kumar K.V | a30d542a | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 1783 | if (ext4_claim_free_blocks(sbi, total)) { | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1784 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 
| Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 1785 | if (ext4_should_retry_alloc(inode->i_sb, &retries)) { | 
|  | 1786 | yield(); | 
|  | 1787 | goto repeat; | 
|  | 1788 | } | 
| Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1789 | vfs_dq_release_reservation_block(inode, total); | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1790 | return -ENOSPC; | 
|  | 1791 | } | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1792 | EXT4_I(inode)->i_reserved_data_blocks += nrblocks; | 
|  | 1793 | EXT4_I(inode)->i_reserved_meta_blocks = mdblocks; | 
|  | 1794 |  | 
|  | 1795 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 
|  | 1796 | return 0;       /* success */ | 
|  | 1797 | } | 
|  | 1798 |  | 
| Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1799 | static void ext4_da_release_space(struct inode *inode, int to_free) | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1800 | { | 
|  | 1801 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
|  | 1802 | int total, mdb, mdb_free, release; | 
|  | 1803 |  | 
| Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1804 | if (!to_free) | 
|  | 1805 | return;		/* Nothing to release, exit */ | 
|  | 1806 |  | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1807 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | 
| Mingming Cao | cd21322 | 2008-08-19 22:16:59 -0400 | [diff] [blame] | 1808 |  | 
|  | 1809 | if (!EXT4_I(inode)->i_reserved_data_blocks) { | 
|  | 1810 | /* | 
|  | 1811 | * if there is no reserved blocks, but we try to free some | 
|  | 1812 | * then the counter is messed up somewhere. | 
|  | 1813 | * but since this function is called from invalidate | 
|  | 1814 | * page, it's harmless to return without any action | 
|  | 1815 | */ | 
|  | 1816 | printk(KERN_INFO "ext4 delalloc try to release %d reserved " | 
|  | 1817 | "blocks for inode %lu, but there is no reserved " | 
|  | 1818 | "data blocks\n", to_free, inode->i_ino); | 
|  | 1819 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 
|  | 1820 | return; | 
|  | 1821 | } | 
|  | 1822 |  | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1823 | /* recalculate the number of metablocks still need to be reserved */ | 
| Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1824 | total = EXT4_I(inode)->i_reserved_data_blocks - to_free; | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1825 | mdb = ext4_calc_metadata_amount(inode, total); | 
|  | 1826 |  | 
|  | 1827 | /* figure out how many metablocks to release */ | 
|  | 1828 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); | 
|  | 1829 | mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; | 
|  | 1830 |  | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1831 | release = to_free + mdb_free; | 
|  | 1832 |  | 
| Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 1833 | /* update fs dirty blocks counter for truncate case */ | 
|  | 1834 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, release); | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1835 |  | 
|  | 1836 | /* update per-inode reservations */ | 
| Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1837 | BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks); | 
|  | 1838 | EXT4_I(inode)->i_reserved_data_blocks -= to_free; | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1839 |  | 
|  | 1840 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); | 
|  | 1841 | EXT4_I(inode)->i_reserved_meta_blocks = mdb; | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1842 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 
| Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 1843 |  | 
|  | 1844 | vfs_dq_release_reservation_block(inode, release); | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1845 | } | 
|  | 1846 |  | 
|  | 1847 | static void ext4_da_page_release_reservation(struct page *page, | 
|  | 1848 | unsigned long offset) | 
|  | 1849 | { | 
|  | 1850 | int to_release = 0; | 
|  | 1851 | struct buffer_head *head, *bh; | 
|  | 1852 | unsigned int curr_off = 0; | 
|  | 1853 |  | 
|  | 1854 | head = page_buffers(page); | 
|  | 1855 | bh = head; | 
|  | 1856 | do { | 
|  | 1857 | unsigned int next_off = curr_off + bh->b_size; | 
|  | 1858 |  | 
|  | 1859 | if ((offset <= curr_off) && (buffer_delay(bh))) { | 
|  | 1860 | to_release++; | 
|  | 1861 | clear_buffer_delay(bh); | 
|  | 1862 | } | 
|  | 1863 | curr_off = next_off; | 
|  | 1864 | } while ((bh = bh->b_this_page) != head); | 
| Aneesh Kumar K.V | 12219ae | 2008-07-17 16:12:08 -0400 | [diff] [blame] | 1865 | ext4_da_release_space(page->mapping->host, to_release); | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 1866 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1867 |  | 
|  | 1868 | /* | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1869 | * Delayed allocation stuff | 
|  | 1870 | */ | 
|  | 1871 |  | 
|  | 1872 | struct mpage_da_data { | 
|  | 1873 | struct inode *inode; | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 1874 | sector_t b_blocknr;		/* start block number of extent */ | 
|  | 1875 | size_t b_size;			/* size of extent */ | 
|  | 1876 | unsigned long b_state;		/* state of the extent */ | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1877 | unsigned long first_page, next_page;	/* extent of pages */ | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1878 | struct writeback_control *wbc; | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1879 | int io_done; | 
| Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1880 | int pages_written; | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 1881 | int retval; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1882 | }; | 
|  | 1883 |  | 
|  | 1884 | /* | 
|  | 1885 | * 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] | 1886 | * them with writepage() call back | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1887 | * | 
|  | 1888 | * @mpd->inode: inode | 
|  | 1889 | * @mpd->first_page: first page of the extent | 
|  | 1890 | * @mpd->next_page: page after the last page of the extent | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1891 | * | 
|  | 1892 | * By the time mpage_da_submit_io() is called we expect all blocks | 
|  | 1893 | * to be allocated. this may be wrong if allocation failed. | 
|  | 1894 | * | 
|  | 1895 | * As pages are already locked by write_cache_pages(), we can't use it | 
|  | 1896 | */ | 
|  | 1897 | static int mpage_da_submit_io(struct mpage_da_data *mpd) | 
|  | 1898 | { | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 1899 | long pages_skipped; | 
| Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1900 | struct pagevec pvec; | 
|  | 1901 | unsigned long index, end; | 
|  | 1902 | int ret = 0, err, nr_pages, i; | 
|  | 1903 | struct inode *inode = mpd->inode; | 
|  | 1904 | struct address_space *mapping = inode->i_mapping; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1905 |  | 
|  | 1906 | BUG_ON(mpd->next_page <= mpd->first_page); | 
| Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1907 | /* | 
|  | 1908 | * We need to start from the first_page to the next_page - 1 | 
|  | 1909 | * to make sure we also write the mapped dirty buffer_heads. | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 1910 | * 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] | 1911 | * at the currently mapped buffer_heads. | 
|  | 1912 | */ | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1913 | index = mpd->first_page; | 
|  | 1914 | end = mpd->next_page - 1; | 
|  | 1915 |  | 
| Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1916 | pagevec_init(&pvec, 0); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1917 | while (index <= end) { | 
| Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1918 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1919 | if (nr_pages == 0) | 
|  | 1920 | break; | 
|  | 1921 | for (i = 0; i < nr_pages; i++) { | 
|  | 1922 | struct page *page = pvec.pages[i]; | 
|  | 1923 |  | 
| Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 1924 | index = page->index; | 
|  | 1925 | if (index > end) | 
|  | 1926 | break; | 
|  | 1927 | index++; | 
|  | 1928 |  | 
|  | 1929 | BUG_ON(!PageLocked(page)); | 
|  | 1930 | BUG_ON(PageWriteback(page)); | 
|  | 1931 |  | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 1932 | pages_skipped = mpd->wbc->pages_skipped; | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1933 | err = mapping->a_ops->writepage(page, mpd->wbc); | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 1934 | if (!err && (pages_skipped == mpd->wbc->pages_skipped)) | 
|  | 1935 | /* | 
|  | 1936 | * have successfully written the page | 
|  | 1937 | * without skipping the same | 
|  | 1938 | */ | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1939 | mpd->pages_written++; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1940 | /* | 
|  | 1941 | * In error case, we have to continue because | 
|  | 1942 | * remaining pages are still locked | 
|  | 1943 | * XXX: unlock and re-dirty them? | 
|  | 1944 | */ | 
|  | 1945 | if (ret == 0) | 
|  | 1946 | ret = err; | 
|  | 1947 | } | 
|  | 1948 | pagevec_release(&pvec); | 
|  | 1949 | } | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1950 | return ret; | 
|  | 1951 | } | 
|  | 1952 |  | 
|  | 1953 | /* | 
|  | 1954 | * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers | 
|  | 1955 | * | 
|  | 1956 | * @mpd->inode - inode to walk through | 
|  | 1957 | * @exbh->b_blocknr - first block on a disk | 
|  | 1958 | * @exbh->b_size - amount of space in bytes | 
|  | 1959 | * @logical - first logical block to start assignment with | 
|  | 1960 | * | 
|  | 1961 | * 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] | 1962 | * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1963 | */ | 
|  | 1964 | static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical, | 
|  | 1965 | struct buffer_head *exbh) | 
|  | 1966 | { | 
|  | 1967 | struct inode *inode = mpd->inode; | 
|  | 1968 | struct address_space *mapping = inode->i_mapping; | 
|  | 1969 | int blocks = exbh->b_size >> inode->i_blkbits; | 
|  | 1970 | sector_t pblock = exbh->b_blocknr, cur_logical; | 
|  | 1971 | struct buffer_head *head, *bh; | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 1972 | pgoff_t index, end; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1973 | struct pagevec pvec; | 
|  | 1974 | int nr_pages, i; | 
|  | 1975 |  | 
|  | 1976 | index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); | 
|  | 1977 | end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits); | 
|  | 1978 | cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); | 
|  | 1979 |  | 
|  | 1980 | pagevec_init(&pvec, 0); | 
|  | 1981 |  | 
|  | 1982 | while (index <= end) { | 
|  | 1983 | /* XXX: optimize tail */ | 
|  | 1984 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); | 
|  | 1985 | if (nr_pages == 0) | 
|  | 1986 | break; | 
|  | 1987 | for (i = 0; i < nr_pages; i++) { | 
|  | 1988 | struct page *page = pvec.pages[i]; | 
|  | 1989 |  | 
|  | 1990 | index = page->index; | 
|  | 1991 | if (index > end) | 
|  | 1992 | break; | 
|  | 1993 | index++; | 
|  | 1994 |  | 
|  | 1995 | BUG_ON(!PageLocked(page)); | 
|  | 1996 | BUG_ON(PageWriteback(page)); | 
|  | 1997 | BUG_ON(!page_has_buffers(page)); | 
|  | 1998 |  | 
|  | 1999 | bh = page_buffers(page); | 
|  | 2000 | head = bh; | 
|  | 2001 |  | 
|  | 2002 | /* skip blocks out of the range */ | 
|  | 2003 | do { | 
|  | 2004 | if (cur_logical >= logical) | 
|  | 2005 | break; | 
|  | 2006 | cur_logical++; | 
|  | 2007 | } while ((bh = bh->b_this_page) != head); | 
|  | 2008 |  | 
|  | 2009 | do { | 
|  | 2010 | if (cur_logical >= logical + blocks) | 
|  | 2011 | break; | 
| Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2012 |  | 
|  | 2013 | if (buffer_delay(bh) || | 
|  | 2014 | buffer_unwritten(bh)) { | 
|  | 2015 |  | 
|  | 2016 | BUG_ON(bh->b_bdev != inode->i_sb->s_bdev); | 
|  | 2017 |  | 
|  | 2018 | if (buffer_delay(bh)) { | 
|  | 2019 | clear_buffer_delay(bh); | 
|  | 2020 | bh->b_blocknr = pblock; | 
|  | 2021 | } else { | 
|  | 2022 | /* | 
|  | 2023 | * unwritten already should have | 
|  | 2024 | * blocknr assigned. Verify that | 
|  | 2025 | */ | 
|  | 2026 | clear_buffer_unwritten(bh); | 
|  | 2027 | BUG_ON(bh->b_blocknr != pblock); | 
|  | 2028 | } | 
|  | 2029 |  | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2030 | } else if (buffer_mapped(bh)) | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2031 | BUG_ON(bh->b_blocknr != pblock); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2032 |  | 
|  | 2033 | cur_logical++; | 
|  | 2034 | pblock++; | 
|  | 2035 | } while ((bh = bh->b_this_page) != head); | 
|  | 2036 | } | 
|  | 2037 | pagevec_release(&pvec); | 
|  | 2038 | } | 
|  | 2039 | } | 
|  | 2040 |  | 
|  | 2041 |  | 
|  | 2042 | /* | 
|  | 2043 | * __unmap_underlying_blocks - just a helper function to unmap | 
|  | 2044 | * set of blocks described by @bh | 
|  | 2045 | */ | 
|  | 2046 | static inline void __unmap_underlying_blocks(struct inode *inode, | 
|  | 2047 | struct buffer_head *bh) | 
|  | 2048 | { | 
|  | 2049 | struct block_device *bdev = inode->i_sb->s_bdev; | 
|  | 2050 | int blocks, i; | 
|  | 2051 |  | 
|  | 2052 | blocks = bh->b_size >> inode->i_blkbits; | 
|  | 2053 | for (i = 0; i < blocks; i++) | 
|  | 2054 | unmap_underlying_metadata(bdev, bh->b_blocknr + i); | 
|  | 2055 | } | 
|  | 2056 |  | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2057 | static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd, | 
|  | 2058 | sector_t logical, long blk_cnt) | 
|  | 2059 | { | 
|  | 2060 | int nr_pages, i; | 
|  | 2061 | pgoff_t index, end; | 
|  | 2062 | struct pagevec pvec; | 
|  | 2063 | struct inode *inode = mpd->inode; | 
|  | 2064 | struct address_space *mapping = inode->i_mapping; | 
|  | 2065 |  | 
|  | 2066 | index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); | 
|  | 2067 | end   = (logical + blk_cnt - 1) >> | 
|  | 2068 | (PAGE_CACHE_SHIFT - inode->i_blkbits); | 
|  | 2069 | while (index <= end) { | 
|  | 2070 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); | 
|  | 2071 | if (nr_pages == 0) | 
|  | 2072 | break; | 
|  | 2073 | for (i = 0; i < nr_pages; i++) { | 
|  | 2074 | struct page *page = pvec.pages[i]; | 
|  | 2075 | index = page->index; | 
|  | 2076 | if (index > end) | 
|  | 2077 | break; | 
|  | 2078 | index++; | 
|  | 2079 |  | 
|  | 2080 | BUG_ON(!PageLocked(page)); | 
|  | 2081 | BUG_ON(PageWriteback(page)); | 
|  | 2082 | block_invalidatepage(page, 0); | 
|  | 2083 | ClearPageUptodate(page); | 
|  | 2084 | unlock_page(page); | 
|  | 2085 | } | 
|  | 2086 | } | 
|  | 2087 | return; | 
|  | 2088 | } | 
|  | 2089 |  | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2090 | static void ext4_print_free_blocks(struct inode *inode) | 
|  | 2091 | { | 
|  | 2092 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
|  | 2093 | printk(KERN_EMERG "Total free blocks count %lld\n", | 
|  | 2094 | ext4_count_free_blocks(inode->i_sb)); | 
|  | 2095 | printk(KERN_EMERG "Free/Dirty block details\n"); | 
|  | 2096 | printk(KERN_EMERG "free_blocks=%lld\n", | 
| Alexander Beregalov | 8f72fbd | 2008-10-29 17:13:08 -0400 | [diff] [blame] | 2097 | (long long)percpu_counter_sum(&sbi->s_freeblocks_counter)); | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2098 | printk(KERN_EMERG "dirty_blocks=%lld\n", | 
| Alexander Beregalov | 8f72fbd | 2008-10-29 17:13:08 -0400 | [diff] [blame] | 2099 | (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter)); | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2100 | printk(KERN_EMERG "Block reservation details\n"); | 
| Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2101 | printk(KERN_EMERG "i_reserved_data_blocks=%u\n", | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2102 | EXT4_I(inode)->i_reserved_data_blocks); | 
| Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2103 | printk(KERN_EMERG "i_reserved_meta_blocks=%u\n", | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2104 | EXT4_I(inode)->i_reserved_meta_blocks); | 
|  | 2105 | return; | 
|  | 2106 | } | 
|  | 2107 |  | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2108 | /* | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2109 | * mpage_da_map_blocks - go through given space | 
|  | 2110 | * | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2111 | * @mpd - bh describing space | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2112 | * | 
|  | 2113 | * The function skips space we know is already mapped to disk blocks. | 
|  | 2114 | * | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2115 | */ | 
| Theodore Ts'o | ed5bde0 | 2009-02-23 10:48:07 -0500 | [diff] [blame] | 2116 | static int mpage_da_map_blocks(struct mpage_da_data *mpd) | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2117 | { | 
| Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 2118 | int err, blks, get_blocks_flags; | 
| Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 2119 | struct buffer_head new; | 
| Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2120 | sector_t next = mpd->b_blocknr; | 
|  | 2121 | unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; | 
|  | 2122 | loff_t disksize = EXT4_I(mpd->inode)->i_disksize; | 
|  | 2123 | handle_t *handle = NULL; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2124 |  | 
|  | 2125 | /* | 
|  | 2126 | * We consider only non-mapped and non-allocated blocks | 
|  | 2127 | */ | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2128 | if ((mpd->b_state  & (1 << BH_Mapped)) && | 
| Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2129 | !(mpd->b_state & (1 << BH_Delay)) && | 
|  | 2130 | !(mpd->b_state & (1 << BH_Unwritten))) | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2131 | return 0; | 
| Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2132 |  | 
|  | 2133 | /* | 
|  | 2134 | * If we didn't accumulate anything to write simply return | 
|  | 2135 | */ | 
|  | 2136 | if (!mpd->b_size) | 
|  | 2137 | return 0; | 
|  | 2138 |  | 
|  | 2139 | handle = ext4_journal_current_handle(); | 
|  | 2140 | BUG_ON(!handle); | 
|  | 2141 |  | 
| Aneesh Kumar K.V | 79ffab3 | 2009-05-13 15:13:42 -0400 | [diff] [blame] | 2142 | /* | 
| Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 2143 | * Call ext4_get_blocks() to allocate any delayed allocation | 
|  | 2144 | * blocks, or to convert an uninitialized extent to be | 
|  | 2145 | * initialized (in the case where we have written into | 
|  | 2146 | * one or more preallocated blocks). | 
|  | 2147 | * | 
|  | 2148 | * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to | 
|  | 2149 | * indicate that we are on the delayed allocation path.  This | 
|  | 2150 | * affects functions in many different parts of the allocation | 
|  | 2151 | * call path.  This flag exists primarily because we don't | 
|  | 2152 | * want to change *many* call functions, so ext4_get_blocks() | 
|  | 2153 | * will set the magic i_delalloc_reserved_flag once the | 
|  | 2154 | * inode's allocation semaphore is taken. | 
|  | 2155 | * | 
|  | 2156 | * If the blocks in questions were delalloc blocks, set | 
|  | 2157 | * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting | 
|  | 2158 | * variables are updated after the blocks have been allocated. | 
| Aneesh Kumar K.V | 79ffab3 | 2009-05-13 15:13:42 -0400 | [diff] [blame] | 2159 | */ | 
| Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 2160 | new.b_state = 0; | 
|  | 2161 | get_blocks_flags = (EXT4_GET_BLOCKS_CREATE | | 
|  | 2162 | EXT4_GET_BLOCKS_DELALLOC_RESERVE); | 
|  | 2163 | if (mpd->b_state & (1 << BH_Delay)) | 
|  | 2164 | get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE; | 
| Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2165 | blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks, | 
| Theodore Ts'o | 2ac3b6e | 2009-05-14 13:57:08 -0400 | [diff] [blame] | 2166 | &new, get_blocks_flags); | 
| Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2167 | if (blks < 0) { | 
|  | 2168 | err = blks; | 
| Theodore Ts'o | ed5bde0 | 2009-02-23 10:48:07 -0500 | [diff] [blame] | 2169 | /* | 
|  | 2170 | * If get block returns with error we simply | 
|  | 2171 | * return. Later writepage will redirty the page and | 
|  | 2172 | * writepages will find the dirty page again | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2173 | */ | 
|  | 2174 | if (err == -EAGAIN) | 
|  | 2175 | return 0; | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2176 |  | 
|  | 2177 | if (err == -ENOSPC && | 
| Theodore Ts'o | ed5bde0 | 2009-02-23 10:48:07 -0500 | [diff] [blame] | 2178 | ext4_count_free_blocks(mpd->inode->i_sb)) { | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2179 | mpd->retval = err; | 
|  | 2180 | return 0; | 
|  | 2181 | } | 
|  | 2182 |  | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2183 | /* | 
| Theodore Ts'o | ed5bde0 | 2009-02-23 10:48:07 -0500 | [diff] [blame] | 2184 | * get block failure will cause us to loop in | 
|  | 2185 | * writepages, because a_ops->writepage won't be able | 
|  | 2186 | * to make progress. The page will be redirtied by | 
|  | 2187 | * writepage and writepages will again try to write | 
|  | 2188 | * the same. | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2189 | */ | 
|  | 2190 | printk(KERN_EMERG "%s block allocation failed for inode %lu " | 
|  | 2191 | "at logical offset %llu with max blocks " | 
|  | 2192 | "%zd with error %d\n", | 
|  | 2193 | __func__, mpd->inode->i_ino, | 
|  | 2194 | (unsigned long long)next, | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2195 | mpd->b_size >> mpd->inode->i_blkbits, err); | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2196 | printk(KERN_EMERG "This should not happen.!! " | 
|  | 2197 | "Data will be lost\n"); | 
| Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 2198 | if (err == -ENOSPC) { | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2199 | ext4_print_free_blocks(mpd->inode); | 
| Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 2200 | } | 
| Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2201 | /* invalidate all the pages */ | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2202 | ext4_da_block_invalidatepages(mpd, next, | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2203 | mpd->b_size >> mpd->inode->i_blkbits); | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2204 | return err; | 
|  | 2205 | } | 
| Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2206 | BUG_ON(blks == 0); | 
|  | 2207 |  | 
|  | 2208 | new.b_size = (blks << mpd->inode->i_blkbits); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2209 |  | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2210 | if (buffer_new(&new)) | 
|  | 2211 | __unmap_underlying_blocks(mpd->inode, &new); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2212 |  | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2213 | /* | 
|  | 2214 | * If blocks are delayed marked, we need to | 
|  | 2215 | * put actual blocknr and drop delayed bit | 
|  | 2216 | */ | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2217 | if ((mpd->b_state & (1 << BH_Delay)) || | 
|  | 2218 | (mpd->b_state & (1 << BH_Unwritten))) | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2219 | mpage_put_bnr_to_bhs(mpd, next, &new); | 
|  | 2220 |  | 
| Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2221 | if (ext4_should_order_data(mpd->inode)) { | 
|  | 2222 | err = ext4_jbd2_file_inode(handle, mpd->inode); | 
|  | 2223 | if (err) | 
|  | 2224 | return err; | 
|  | 2225 | } | 
|  | 2226 |  | 
|  | 2227 | /* | 
| Jan Kara | 03f5d8b | 2009-06-09 00:17:05 -0400 | [diff] [blame] | 2228 | * Update on-disk size along with block allocation. | 
| Theodore Ts'o | 2fa3cdf | 2009-05-14 09:29:45 -0400 | [diff] [blame] | 2229 | */ | 
|  | 2230 | disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; | 
|  | 2231 | if (disksize > i_size_read(mpd->inode)) | 
|  | 2232 | disksize = i_size_read(mpd->inode); | 
|  | 2233 | if (disksize > EXT4_I(mpd->inode)->i_disksize) { | 
|  | 2234 | ext4_update_i_disksize(mpd->inode, disksize); | 
|  | 2235 | return ext4_mark_inode_dirty(handle, mpd->inode); | 
|  | 2236 | } | 
|  | 2237 |  | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2238 | return 0; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2239 | } | 
|  | 2240 |  | 
| Aneesh Kumar K.V | bf068ee | 2008-08-19 22:16:43 -0400 | [diff] [blame] | 2241 | #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ | 
|  | 2242 | (1 << BH_Delay) | (1 << BH_Unwritten)) | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2243 |  | 
|  | 2244 | /* | 
|  | 2245 | * mpage_add_bh_to_extent - try to add one more block to extent of blocks | 
|  | 2246 | * | 
|  | 2247 | * @mpd->lbh - extent of blocks | 
|  | 2248 | * @logical - logical number of the block in the file | 
|  | 2249 | * @bh - bh of the block (used to access block's state) | 
|  | 2250 | * | 
|  | 2251 | * the function is used to collect contig. blocks in same state | 
|  | 2252 | */ | 
|  | 2253 | 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] | 2254 | sector_t logical, size_t b_size, | 
|  | 2255 | unsigned long b_state) | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2256 | { | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2257 | sector_t next; | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2258 | int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2259 |  | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2260 | /* check if thereserved journal credits might overflow */ | 
|  | 2261 | if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) { | 
|  | 2262 | if (nrblocks >= EXT4_MAX_TRANS_DATA) { | 
|  | 2263 | /* | 
|  | 2264 | * With non-extent format we are limited by the journal | 
|  | 2265 | * credit available.  Total credit needed to insert | 
|  | 2266 | * nrblocks contiguous blocks is dependent on the | 
|  | 2267 | * nrblocks.  So limit nrblocks. | 
|  | 2268 | */ | 
|  | 2269 | goto flush_it; | 
|  | 2270 | } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > | 
|  | 2271 | EXT4_MAX_TRANS_DATA) { | 
|  | 2272 | /* | 
|  | 2273 | * Adding the new buffer_head would make it cross the | 
|  | 2274 | * allowed limit for which we have journal credit | 
|  | 2275 | * reserved. So limit the new bh->b_size | 
|  | 2276 | */ | 
|  | 2277 | b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << | 
|  | 2278 | mpd->inode->i_blkbits; | 
|  | 2279 | /* we will do mpage_da_submit_io in the next loop */ | 
|  | 2280 | } | 
|  | 2281 | } | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2282 | /* | 
|  | 2283 | * First block in the extent | 
|  | 2284 | */ | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2285 | if (mpd->b_size == 0) { | 
|  | 2286 | mpd->b_blocknr = logical; | 
|  | 2287 | mpd->b_size = b_size; | 
|  | 2288 | mpd->b_state = b_state & BH_FLAGS; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2289 | return; | 
|  | 2290 | } | 
|  | 2291 |  | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2292 | next = mpd->b_blocknr + nrblocks; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2293 | /* | 
|  | 2294 | * Can we merge the block to our big extent? | 
|  | 2295 | */ | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2296 | if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { | 
|  | 2297 | mpd->b_size += b_size; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2298 | return; | 
|  | 2299 | } | 
|  | 2300 |  | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2301 | flush_it: | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2302 | /* | 
|  | 2303 | * We couldn't merge the block to our extent, so we | 
|  | 2304 | * need to flush current  extent and start new one | 
|  | 2305 | */ | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2306 | if (mpage_da_map_blocks(mpd) == 0) | 
|  | 2307 | mpage_da_submit_io(mpd); | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2308 | mpd->io_done = 1; | 
|  | 2309 | return; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2310 | } | 
|  | 2311 |  | 
| Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2312 | static int ext4_bh_unmapped_or_delay(handle_t *handle, struct buffer_head *bh) | 
|  | 2313 | { | 
|  | 2314 | /* | 
|  | 2315 | * unmapped buffer is possible for holes. | 
|  | 2316 | * delay buffer is possible with delayed allocation. | 
|  | 2317 | * We also need to consider unwritten buffer as unmapped. | 
|  | 2318 | */ | 
|  | 2319 | return (!buffer_mapped(bh) || buffer_delay(bh) || | 
|  | 2320 | buffer_unwritten(bh)) && buffer_dirty(bh); | 
|  | 2321 | } | 
|  | 2322 |  | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2323 | /* | 
|  | 2324 | * __mpage_da_writepage - finds extent of pages and blocks | 
|  | 2325 | * | 
|  | 2326 | * @page: page to consider | 
|  | 2327 | * @wbc: not used, we just follow rules | 
|  | 2328 | * @data: context | 
|  | 2329 | * | 
|  | 2330 | * The function finds extents of pages and scan them for all blocks. | 
|  | 2331 | */ | 
|  | 2332 | static int __mpage_da_writepage(struct page *page, | 
|  | 2333 | struct writeback_control *wbc, void *data) | 
|  | 2334 | { | 
|  | 2335 | struct mpage_da_data *mpd = data; | 
|  | 2336 | struct inode *inode = mpd->inode; | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2337 | struct buffer_head *bh, *head; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2338 | sector_t logical; | 
|  | 2339 |  | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2340 | if (mpd->io_done) { | 
|  | 2341 | /* | 
|  | 2342 | * Rest of the page in the page_vec | 
|  | 2343 | * redirty then and skip then. We will | 
|  | 2344 | * try to to write them again after | 
|  | 2345 | * starting a new transaction | 
|  | 2346 | */ | 
|  | 2347 | redirty_page_for_writepage(wbc, page); | 
|  | 2348 | unlock_page(page); | 
|  | 2349 | return MPAGE_DA_EXTENT_TAIL; | 
|  | 2350 | } | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2351 | /* | 
|  | 2352 | * Can we merge this page to current extent? | 
|  | 2353 | */ | 
|  | 2354 | if (mpd->next_page != page->index) { | 
|  | 2355 | /* | 
|  | 2356 | * Nope, we can't. So, we map non-allocated blocks | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2357 | * and start IO on them using writepage() | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2358 | */ | 
|  | 2359 | if (mpd->next_page != mpd->first_page) { | 
| Aneesh Kumar K.V | c4a0c46 | 2008-08-19 21:08:18 -0400 | [diff] [blame] | 2360 | if (mpage_da_map_blocks(mpd) == 0) | 
|  | 2361 | mpage_da_submit_io(mpd); | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2362 | /* | 
|  | 2363 | * skip rest of the page in the page_vec | 
|  | 2364 | */ | 
|  | 2365 | mpd->io_done = 1; | 
|  | 2366 | redirty_page_for_writepage(wbc, page); | 
|  | 2367 | unlock_page(page); | 
|  | 2368 | return MPAGE_DA_EXTENT_TAIL; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2369 | } | 
|  | 2370 |  | 
|  | 2371 | /* | 
|  | 2372 | * Start next extent of pages ... | 
|  | 2373 | */ | 
|  | 2374 | mpd->first_page = page->index; | 
|  | 2375 |  | 
|  | 2376 | /* | 
|  | 2377 | * ... and blocks | 
|  | 2378 | */ | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2379 | mpd->b_size = 0; | 
|  | 2380 | mpd->b_state = 0; | 
|  | 2381 | mpd->b_blocknr = 0; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2382 | } | 
|  | 2383 |  | 
|  | 2384 | mpd->next_page = page->index + 1; | 
|  | 2385 | logical = (sector_t) page->index << | 
|  | 2386 | (PAGE_CACHE_SHIFT - inode->i_blkbits); | 
|  | 2387 |  | 
|  | 2388 | if (!page_has_buffers(page)) { | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2389 | mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE, | 
|  | 2390 | (1 << BH_Dirty) | (1 << BH_Uptodate)); | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2391 | if (mpd->io_done) | 
|  | 2392 | return MPAGE_DA_EXTENT_TAIL; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2393 | } else { | 
|  | 2394 | /* | 
|  | 2395 | * Page with regular buffer heads, just add all dirty ones | 
|  | 2396 | */ | 
|  | 2397 | head = page_buffers(page); | 
|  | 2398 | bh = head; | 
|  | 2399 | do { | 
|  | 2400 | BUG_ON(buffer_locked(bh)); | 
| Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 2401 | /* | 
|  | 2402 | * We need to try to allocate | 
|  | 2403 | * unmapped blocks in the same page. | 
|  | 2404 | * Otherwise we won't make progress | 
|  | 2405 | * with the page in ext4_da_writepage | 
|  | 2406 | */ | 
| Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2407 | if (ext4_bh_unmapped_or_delay(NULL, bh)) { | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2408 | mpage_add_bh_to_extent(mpd, logical, | 
|  | 2409 | bh->b_size, | 
|  | 2410 | bh->b_state); | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2411 | if (mpd->io_done) | 
|  | 2412 | return MPAGE_DA_EXTENT_TAIL; | 
| Aneesh Kumar K.V | 791b7f0 | 2009-01-05 21:50:43 -0500 | [diff] [blame] | 2413 | } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { | 
|  | 2414 | /* | 
|  | 2415 | * mapped dirty buffer. We need to update | 
|  | 2416 | * the b_state because we look at | 
|  | 2417 | * b_state in mpage_da_map_blocks. We don't | 
|  | 2418 | * update b_size because if we find an | 
|  | 2419 | * unmapped buffer_head later we need to | 
|  | 2420 | * use the b_state flag of that buffer_head. | 
|  | 2421 | */ | 
| Theodore Ts'o | 8dc207c | 2009-02-23 06:46:01 -0500 | [diff] [blame] | 2422 | if (mpd->b_size == 0) | 
|  | 2423 | mpd->b_state = bh->b_state & BH_FLAGS; | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2424 | } | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2425 | logical++; | 
|  | 2426 | } while ((bh = bh->b_this_page) != head); | 
|  | 2427 | } | 
|  | 2428 |  | 
|  | 2429 | return 0; | 
|  | 2430 | } | 
|  | 2431 |  | 
|  | 2432 | /* | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2433 | * This is a special get_blocks_t callback which is used by | 
|  | 2434 | * ext4_da_write_begin().  It will either return mapped block or | 
|  | 2435 | * reserve space for a single block. | 
| Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2436 | * | 
|  | 2437 | * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. | 
|  | 2438 | * We also have b_blocknr = -1 and b_bdev initialized properly | 
|  | 2439 | * | 
|  | 2440 | * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. | 
|  | 2441 | * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev | 
|  | 2442 | * initialized properly. | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2443 | */ | 
|  | 2444 | static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, | 
|  | 2445 | struct buffer_head *bh_result, int create) | 
|  | 2446 | { | 
|  | 2447 | int ret = 0; | 
| Aneesh Kumar K.V | 33b9817 | 2009-05-12 14:40:37 -0400 | [diff] [blame] | 2448 | sector_t invalid_block = ~((sector_t) 0xffff); | 
|  | 2449 |  | 
|  | 2450 | if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) | 
|  | 2451 | invalid_block = ~0; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2452 |  | 
|  | 2453 | BUG_ON(create == 0); | 
|  | 2454 | BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); | 
|  | 2455 |  | 
|  | 2456 | /* | 
|  | 2457 | * first, we need to know whether the block is allocated already | 
|  | 2458 | * preallocated blocks are unmapped but should treated | 
|  | 2459 | * the same as allocated blocks. | 
|  | 2460 | */ | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2461 | ret = ext4_get_blocks(NULL, inode, iblock, 1,  bh_result, 0); | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2462 | if ((ret == 0) && !buffer_delay(bh_result)) { | 
|  | 2463 | /* the block isn't (pre)allocated yet, let's reserve space */ | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2464 | /* | 
|  | 2465 | * XXX: __block_prepare_write() unmaps passed block, | 
|  | 2466 | * is it OK? | 
|  | 2467 | */ | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2468 | ret = ext4_da_reserve_space(inode, 1); | 
|  | 2469 | if (ret) | 
|  | 2470 | /* not enough space to reserve */ | 
|  | 2471 | return ret; | 
|  | 2472 |  | 
| Aneesh Kumar K.V | 33b9817 | 2009-05-12 14:40:37 -0400 | [diff] [blame] | 2473 | map_bh(bh_result, inode->i_sb, invalid_block); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2474 | set_buffer_new(bh_result); | 
|  | 2475 | set_buffer_delay(bh_result); | 
|  | 2476 | } else if (ret > 0) { | 
|  | 2477 | bh_result->b_size = (ret << inode->i_blkbits); | 
| Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2478 | if (buffer_unwritten(bh_result)) { | 
|  | 2479 | /* A delayed write to unwritten bh should | 
|  | 2480 | * be marked new and mapped.  Mapped ensures | 
|  | 2481 | * that we don't do get_block multiple times | 
|  | 2482 | * when we write to the same offset and new | 
|  | 2483 | * ensures that we do proper zero out for | 
|  | 2484 | * partial write. | 
|  | 2485 | */ | 
| Aneesh Kumar K.V | 9c1ee18 | 2009-05-13 18:36:58 -0400 | [diff] [blame] | 2486 | set_buffer_new(bh_result); | 
| Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2487 | set_buffer_mapped(bh_result); | 
|  | 2488 | } | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2489 | ret = 0; | 
|  | 2490 | } | 
|  | 2491 |  | 
|  | 2492 | return ret; | 
|  | 2493 | } | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2494 |  | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2495 | /* | 
|  | 2496 | * This function is used as a standard get_block_t calback function | 
|  | 2497 | * when there is no desire to allocate any blocks.  It is used as a | 
|  | 2498 | * callback function for block_prepare_write(), nobh_writepage(), and | 
|  | 2499 | * block_write_full_page().  These functions should only try to map a | 
|  | 2500 | * single block at a time. | 
|  | 2501 | * | 
|  | 2502 | * Since this function doesn't do block allocations even if the caller | 
|  | 2503 | * requests it by passing in create=1, it is critically important that | 
|  | 2504 | * any caller checks to make sure that any buffer heads are returned | 
|  | 2505 | * by this function are either all already mapped or marked for | 
|  | 2506 | * delayed allocation before calling nobh_writepage() or | 
|  | 2507 | * block_write_full_page().  Otherwise, b_blocknr could be left | 
|  | 2508 | * unitialized, and the page write functions will be taken by | 
|  | 2509 | * surprise. | 
|  | 2510 | */ | 
|  | 2511 | 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] | 2512 | struct buffer_head *bh_result, int create) | 
|  | 2513 | { | 
|  | 2514 | int ret = 0; | 
|  | 2515 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; | 
|  | 2516 |  | 
| Theodore Ts'o | a2dc52b | 2009-05-12 13:51:29 -0400 | [diff] [blame] | 2517 | BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); | 
|  | 2518 |  | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2519 | /* | 
|  | 2520 | * we don't want to do block allocation in writepage | 
|  | 2521 | * so call get_block_wrap with create = 0 | 
|  | 2522 | */ | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2523 | ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0); | 
| Theodore Ts'o | a2dc52b | 2009-05-12 13:51:29 -0400 | [diff] [blame] | 2524 | BUG_ON(create && ret == 0); | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2525 | if (ret > 0) { | 
|  | 2526 | bh_result->b_size = (ret << inode->i_blkbits); | 
|  | 2527 | ret = 0; | 
|  | 2528 | } | 
|  | 2529 | return ret; | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2530 | } | 
|  | 2531 |  | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2532 | /* | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2533 | * This function can get called via... | 
|  | 2534 | *   - ext4_da_writepages after taking page lock (have journal handle) | 
|  | 2535 | *   - journal_submit_inode_data_buffers (no journal handle) | 
|  | 2536 | *   - shrink_page_list via pdflush (no journal handle) | 
|  | 2537 | *   - grab_page_cache when doing write_begin (have journal handle) | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2538 | */ | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2539 | static int ext4_da_writepage(struct page *page, | 
|  | 2540 | struct writeback_control *wbc) | 
|  | 2541 | { | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2542 | int ret = 0; | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2543 | loff_t size; | 
| Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2544 | unsigned int len; | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2545 | struct buffer_head *page_bufs; | 
|  | 2546 | struct inode *inode = page->mapping->host; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2547 |  | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 2548 | trace_ext4_da_writepage(inode, page); | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2549 | size = i_size_read(inode); | 
|  | 2550 | if (page->index == size >> PAGE_CACHE_SHIFT) | 
|  | 2551 | len = size & ~PAGE_CACHE_MASK; | 
|  | 2552 | else | 
|  | 2553 | len = PAGE_CACHE_SIZE; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2554 |  | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2555 | if (page_has_buffers(page)) { | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2556 | page_bufs = page_buffers(page); | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2557 | if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, | 
|  | 2558 | ext4_bh_unmapped_or_delay)) { | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2559 | /* | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2560 | * We don't want to do  block allocation | 
|  | 2561 | * So redirty the page and return | 
| Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2562 | * We may reach here when we do a journal commit | 
|  | 2563 | * via journal_submit_inode_data_buffers. | 
|  | 2564 | * If we don't have mapping block we just ignore | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2565 | * them. We can also reach here via shrink_page_list | 
|  | 2566 | */ | 
|  | 2567 | redirty_page_for_writepage(wbc, page); | 
|  | 2568 | unlock_page(page); | 
|  | 2569 | return 0; | 
|  | 2570 | } | 
|  | 2571 | } else { | 
|  | 2572 | /* | 
|  | 2573 | * The test for page_has_buffers() is subtle: | 
|  | 2574 | * We know the page is dirty but it lost buffers. That means | 
|  | 2575 | * that at some moment in time after write_begin()/write_end() | 
|  | 2576 | * has been called all buffers have been clean and thus they | 
|  | 2577 | * must have been written at least once. So they are all | 
|  | 2578 | * mapped and we can happily proceed with mapping them | 
|  | 2579 | * and writing the page. | 
|  | 2580 | * | 
|  | 2581 | * Try to initialize the buffer_heads and check whether | 
|  | 2582 | * all are mapped and non delay. We don't want to | 
|  | 2583 | * do block allocation here. | 
|  | 2584 | */ | 
|  | 2585 | ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE, | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2586 | noalloc_get_block_write); | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2587 | if (!ret) { | 
|  | 2588 | page_bufs = page_buffers(page); | 
|  | 2589 | /* check whether all are mapped and non delay */ | 
|  | 2590 | if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, | 
|  | 2591 | ext4_bh_unmapped_or_delay)) { | 
|  | 2592 | redirty_page_for_writepage(wbc, page); | 
|  | 2593 | unlock_page(page); | 
|  | 2594 | return 0; | 
|  | 2595 | } | 
|  | 2596 | } else { | 
|  | 2597 | /* | 
|  | 2598 | * We can't do block allocation here | 
|  | 2599 | * so just redity the page and unlock | 
|  | 2600 | * and return | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2601 | */ | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2602 | redirty_page_for_writepage(wbc, page); | 
|  | 2603 | unlock_page(page); | 
|  | 2604 | return 0; | 
|  | 2605 | } | 
| Aneesh Kumar K.V | ed9b3e3 | 2008-11-07 09:06:45 -0500 | [diff] [blame] | 2606 | /* now mark the buffer_heads as dirty and uptodate */ | 
|  | 2607 | block_commit_write(page, 0, PAGE_CACHE_SIZE); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2608 | } | 
|  | 2609 |  | 
|  | 2610 | 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] | 2611 | ret = nobh_writepage(page, noalloc_get_block_write, wbc); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2612 | else | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2613 | ret = block_write_full_page(page, noalloc_get_block_write, | 
|  | 2614 | wbc); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2615 |  | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2616 | return ret; | 
|  | 2617 | } | 
|  | 2618 |  | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2619 | /* | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2620 | * This is called via ext4_da_writepages() to | 
|  | 2621 | * calulate the total number of credits to reserve to fit | 
|  | 2622 | * a single extent allocation into a single transaction, | 
|  | 2623 | * ext4_da_writpeages() will loop calling this before | 
|  | 2624 | * the block allocation. | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2625 | */ | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2626 |  | 
|  | 2627 | static int ext4_da_writepages_trans_blocks(struct inode *inode) | 
|  | 2628 | { | 
|  | 2629 | int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; | 
|  | 2630 |  | 
|  | 2631 | /* | 
|  | 2632 | * With non-extent format the journal credit needed to | 
|  | 2633 | * insert nrblocks contiguous block is dependent on | 
|  | 2634 | * number of contiguous block. So we will limit | 
|  | 2635 | * number of contiguous block to a sane value | 
|  | 2636 | */ | 
|  | 2637 | if (!(inode->i_flags & EXT4_EXTENTS_FL) && | 
|  | 2638 | (max_blocks > EXT4_MAX_TRANS_DATA)) | 
|  | 2639 | max_blocks = EXT4_MAX_TRANS_DATA; | 
|  | 2640 |  | 
|  | 2641 | return ext4_chunk_trans_blocks(inode, max_blocks); | 
|  | 2642 | } | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2643 |  | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2644 | static int ext4_da_writepages(struct address_space *mapping, | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2645 | struct writeback_control *wbc) | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2646 | { | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2647 | pgoff_t	index; | 
|  | 2648 | int range_whole = 0; | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2649 | handle_t *handle = NULL; | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2650 | struct mpage_da_data mpd; | 
| Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2651 | struct inode *inode = mapping->host; | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2652 | int no_nrwrite_index_update; | 
| Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2653 | int pages_written = 0; | 
|  | 2654 | long pages_skipped; | 
| Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2655 | int range_cyclic, cycled = 1, io_done = 0; | 
| Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2656 | int needed_blocks, ret = 0, nr_to_writebump = 0; | 
| Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2657 | struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2658 |  | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 2659 | trace_ext4_da_writepages(inode, wbc); | 
| Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 2660 |  | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2661 | /* | 
|  | 2662 | * No pages to write? This is mainly a kludge to avoid starting | 
|  | 2663 | * a transaction for special inodes like journal inode on last iput() | 
|  | 2664 | * because that could violate lock ordering on umount | 
|  | 2665 | */ | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2666 | if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2667 | return 0; | 
| Theodore Ts'o | 2a21e37 | 2008-11-05 09:22:24 -0500 | [diff] [blame] | 2668 |  | 
|  | 2669 | /* | 
|  | 2670 | * If the filesystem has aborted, it is read-only, so return | 
|  | 2671 | * right away instead of dumping stack traces later on that | 
|  | 2672 | * will obscure the real source of the problem.  We test | 
|  | 2673 | * EXT4_MOUNT_ABORT instead of sb->s_flag's MS_RDONLY because | 
|  | 2674 | * the latter could be true if the filesystem is mounted | 
|  | 2675 | * read-only, and in that case, ext4_da_writepages should | 
|  | 2676 | * *never* be called, so if that ever happens, we would want | 
|  | 2677 | * the stack trace. | 
|  | 2678 | */ | 
|  | 2679 | if (unlikely(sbi->s_mount_opt & EXT4_MOUNT_ABORT)) | 
|  | 2680 | return -EROFS; | 
|  | 2681 |  | 
| Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 2682 | /* | 
|  | 2683 | * Make sure nr_to_write is >= sbi->s_mb_stream_request | 
|  | 2684 | * This make sure small files blocks are allocated in | 
|  | 2685 | * single attempt. This ensure that small files | 
|  | 2686 | * get less fragmented. | 
|  | 2687 | */ | 
|  | 2688 | if (wbc->nr_to_write < sbi->s_mb_stream_request) { | 
|  | 2689 | nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write; | 
|  | 2690 | wbc->nr_to_write = sbi->s_mb_stream_request; | 
|  | 2691 | } | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2692 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) | 
|  | 2693 | range_whole = 1; | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2694 |  | 
| Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2695 | range_cyclic = wbc->range_cyclic; | 
|  | 2696 | if (wbc->range_cyclic) { | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2697 | index = mapping->writeback_index; | 
| Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2698 | if (index) | 
|  | 2699 | cycled = 0; | 
|  | 2700 | wbc->range_start = index << PAGE_CACHE_SHIFT; | 
|  | 2701 | wbc->range_end  = LLONG_MAX; | 
|  | 2702 | wbc->range_cyclic = 0; | 
|  | 2703 | } else | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2704 | index = wbc->range_start >> PAGE_CACHE_SHIFT; | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2705 |  | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2706 | mpd.wbc = wbc; | 
|  | 2707 | mpd.inode = mapping->host; | 
|  | 2708 |  | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2709 | /* | 
|  | 2710 | * we don't want write_cache_pages to update | 
|  | 2711 | * nr_to_write and writeback_index | 
|  | 2712 | */ | 
|  | 2713 | no_nrwrite_index_update = wbc->no_nrwrite_index_update; | 
|  | 2714 | wbc->no_nrwrite_index_update = 1; | 
|  | 2715 | pages_skipped = wbc->pages_skipped; | 
|  | 2716 |  | 
| Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2717 | retry: | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2718 | while (!ret && wbc->nr_to_write > 0) { | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2719 |  | 
|  | 2720 | /* | 
|  | 2721 | * we  insert one extent at a time. So we need | 
|  | 2722 | * credit needed for single extent allocation. | 
|  | 2723 | * journalled mode is currently not supported | 
|  | 2724 | * by delalloc | 
|  | 2725 | */ | 
|  | 2726 | BUG_ON(ext4_should_journal_data(inode)); | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2727 | needed_blocks = ext4_da_writepages_trans_blocks(inode); | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2728 |  | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2729 | /* start a new transaction*/ | 
|  | 2730 | handle = ext4_journal_start(inode, needed_blocks); | 
|  | 2731 | if (IS_ERR(handle)) { | 
|  | 2732 | ret = PTR_ERR(handle); | 
| Theodore Ts'o | 2a21e37 | 2008-11-05 09:22:24 -0500 | [diff] [blame] | 2733 | printk(KERN_CRIT "%s: jbd2_start: " | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2734 | "%ld pages, ino %lu; err %d\n", __func__, | 
|  | 2735 | wbc->nr_to_write, inode->i_ino, ret); | 
|  | 2736 | dump_stack(); | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2737 | goto out_writepages; | 
|  | 2738 | } | 
| Theodore Ts'o | f63e600 | 2009-02-23 16:42:39 -0500 | [diff] [blame] | 2739 |  | 
|  | 2740 | /* | 
|  | 2741 | * Now call __mpage_da_writepage to find the next | 
|  | 2742 | * contiguous region of logical blocks that need | 
|  | 2743 | * blocks to be allocated by ext4.  We don't actually | 
|  | 2744 | * submit the blocks for I/O here, even though | 
|  | 2745 | * write_cache_pages thinks it will, and will set the | 
|  | 2746 | * pages as clean for write before calling | 
|  | 2747 | * __mpage_da_writepage(). | 
|  | 2748 | */ | 
|  | 2749 | mpd.b_size = 0; | 
|  | 2750 | mpd.b_state = 0; | 
|  | 2751 | mpd.b_blocknr = 0; | 
|  | 2752 | mpd.first_page = 0; | 
|  | 2753 | mpd.next_page = 0; | 
|  | 2754 | mpd.io_done = 0; | 
|  | 2755 | mpd.pages_written = 0; | 
|  | 2756 | mpd.retval = 0; | 
|  | 2757 | ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, | 
|  | 2758 | &mpd); | 
|  | 2759 | /* | 
|  | 2760 | * If we have a contigous extent of pages and we | 
|  | 2761 | * haven't done the I/O yet, map the blocks and submit | 
|  | 2762 | * them for I/O. | 
|  | 2763 | */ | 
|  | 2764 | if (!mpd.io_done && mpd.next_page != mpd.first_page) { | 
|  | 2765 | if (mpage_da_map_blocks(&mpd) == 0) | 
|  | 2766 | mpage_da_submit_io(&mpd); | 
|  | 2767 | mpd.io_done = 1; | 
|  | 2768 | ret = MPAGE_DA_EXTENT_TAIL; | 
|  | 2769 | } | 
|  | 2770 | wbc->nr_to_write -= mpd.pages_written; | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2771 |  | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2772 | ext4_journal_stop(handle); | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2773 |  | 
| Eric Sandeen | 8f64b32 | 2009-02-26 00:57:35 -0500 | [diff] [blame] | 2774 | if ((mpd.retval == -ENOSPC) && sbi->s_journal) { | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2775 | /* commit the transaction which would | 
|  | 2776 | * free blocks released in the transaction | 
|  | 2777 | * and try again | 
|  | 2778 | */ | 
| Aneesh Kumar K.V | df22291 | 2008-09-08 23:05:34 -0400 | [diff] [blame] | 2779 | jbd2_journal_force_commit_nested(sbi->s_journal); | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2780 | wbc->pages_skipped = pages_skipped; | 
|  | 2781 | ret = 0; | 
|  | 2782 | } else if (ret == MPAGE_DA_EXTENT_TAIL) { | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2783 | /* | 
|  | 2784 | * got one extent now try with | 
|  | 2785 | * rest of the pages | 
|  | 2786 | */ | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2787 | pages_written += mpd.pages_written; | 
|  | 2788 | wbc->pages_skipped = pages_skipped; | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2789 | ret = 0; | 
| Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2790 | io_done = 1; | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2791 | } else if (wbc->nr_to_write) | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2792 | /* | 
|  | 2793 | * There is no more writeout needed | 
|  | 2794 | * or we requested for a noblocking writeout | 
|  | 2795 | * and we found the device congested | 
|  | 2796 | */ | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2797 | break; | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2798 | } | 
| Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2799 | if (!io_done && !cycled) { | 
|  | 2800 | cycled = 1; | 
|  | 2801 | index = 0; | 
|  | 2802 | wbc->range_start = index << PAGE_CACHE_SHIFT; | 
|  | 2803 | wbc->range_end  = mapping->writeback_index - 1; | 
|  | 2804 | goto retry; | 
|  | 2805 | } | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2806 | if (pages_skipped != wbc->pages_skipped) | 
|  | 2807 | printk(KERN_EMERG "This should not happen leaving %s " | 
|  | 2808 | "with nr_to_write = %ld ret = %d\n", | 
|  | 2809 | __func__, wbc->nr_to_write, ret); | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2810 |  | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2811 | /* Update index */ | 
|  | 2812 | index += pages_written; | 
| Aneesh Kumar K.V | 2acf2c2 | 2009-02-14 10:42:58 -0500 | [diff] [blame] | 2813 | wbc->range_cyclic = range_cyclic; | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2814 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | 
|  | 2815 | /* | 
|  | 2816 | * set the writeback_index so that range_cyclic | 
|  | 2817 | * mode will write it back later | 
|  | 2818 | */ | 
|  | 2819 | mapping->writeback_index = index; | 
| Aneesh Kumar K.V | a1d6cc5 | 2008-08-19 21:55:02 -0400 | [diff] [blame] | 2820 |  | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2821 | out_writepages: | 
| Aneesh Kumar K.V | 22208de | 2008-10-16 10:10:36 -0400 | [diff] [blame] | 2822 | if (!no_nrwrite_index_update) | 
|  | 2823 | wbc->no_nrwrite_index_update = 0; | 
|  | 2824 | wbc->nr_to_write -= nr_to_writebump; | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 2825 | trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); | 
| Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2826 | return ret; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2827 | } | 
|  | 2828 |  | 
| Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2829 | #define FALL_BACK_TO_NONDELALLOC 1 | 
|  | 2830 | static int ext4_nonda_switch(struct super_block *sb) | 
|  | 2831 | { | 
|  | 2832 | s64 free_blocks, dirty_blocks; | 
|  | 2833 | struct ext4_sb_info *sbi = EXT4_SB(sb); | 
|  | 2834 |  | 
|  | 2835 | /* | 
|  | 2836 | * switch to non delalloc mode if we are running low | 
|  | 2837 | * on free block. The free block accounting via percpu | 
| Eric Dumazet | 179f7eb | 2009-01-06 14:41:04 -0800 | [diff] [blame] | 2838 | * counters can get slightly wrong with percpu_counter_batch getting | 
| Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2839 | * accumulated on each CPU without updating global counters | 
|  | 2840 | * Delalloc need an accurate free block accounting. So switch | 
|  | 2841 | * to non delalloc when we are near to error range. | 
|  | 2842 | */ | 
|  | 2843 | free_blocks  = percpu_counter_read_positive(&sbi->s_freeblocks_counter); | 
|  | 2844 | dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); | 
|  | 2845 | if (2 * free_blocks < 3 * dirty_blocks || | 
|  | 2846 | free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { | 
|  | 2847 | /* | 
|  | 2848 | * free block count is less that 150% of dirty blocks | 
|  | 2849 | * or free blocks is less that watermark | 
|  | 2850 | */ | 
|  | 2851 | return 1; | 
|  | 2852 | } | 
|  | 2853 | return 0; | 
|  | 2854 | } | 
|  | 2855 |  | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2856 | static int ext4_da_write_begin(struct file *file, struct address_space *mapping, | 
|  | 2857 | loff_t pos, unsigned len, unsigned flags, | 
|  | 2858 | struct page **pagep, void **fsdata) | 
|  | 2859 | { | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2860 | int ret, retries = 0; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2861 | struct page *page; | 
|  | 2862 | pgoff_t index; | 
|  | 2863 | unsigned from, to; | 
|  | 2864 | struct inode *inode = mapping->host; | 
|  | 2865 | handle_t *handle; | 
|  | 2866 |  | 
|  | 2867 | index = pos >> PAGE_CACHE_SHIFT; | 
|  | 2868 | from = pos & (PAGE_CACHE_SIZE - 1); | 
|  | 2869 | to = from + len; | 
| Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2870 |  | 
|  | 2871 | if (ext4_nonda_switch(inode->i_sb)) { | 
|  | 2872 | *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; | 
|  | 2873 | return ext4_write_begin(file, mapping, pos, | 
|  | 2874 | len, flags, pagep, fsdata); | 
|  | 2875 | } | 
|  | 2876 | *fsdata = (void *)0; | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 2877 | trace_ext4_da_write_begin(inode, pos, len, flags); | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2878 | retry: | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2879 | /* | 
|  | 2880 | * With delayed allocation, we don't log the i_disksize update | 
|  | 2881 | * if there is delayed block allocation. But we still need | 
|  | 2882 | * to journalling the i_disksize update if writes to the end | 
|  | 2883 | * of file which has an already mapped buffer. | 
|  | 2884 | */ | 
|  | 2885 | handle = ext4_journal_start(inode, 1); | 
|  | 2886 | if (IS_ERR(handle)) { | 
|  | 2887 | ret = PTR_ERR(handle); | 
|  | 2888 | goto out; | 
|  | 2889 | } | 
| Jan Kara | ebd3610 | 2009-02-22 21:09:59 -0500 | [diff] [blame] | 2890 | /* We cannot recurse into the filesystem as the transaction is already | 
|  | 2891 | * started */ | 
|  | 2892 | flags |= AOP_FLAG_NOFS; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2893 |  | 
| Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 2894 | page = grab_cache_page_write_begin(mapping, index, flags); | 
| Eric Sandeen | d5a0d4f | 2008-08-02 18:51:06 -0400 | [diff] [blame] | 2895 | if (!page) { | 
|  | 2896 | ext4_journal_stop(handle); | 
|  | 2897 | ret = -ENOMEM; | 
|  | 2898 | goto out; | 
|  | 2899 | } | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2900 | *pagep = page; | 
|  | 2901 |  | 
|  | 2902 | ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 2903 | ext4_da_get_block_prep); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2904 | if (ret < 0) { | 
|  | 2905 | unlock_page(page); | 
|  | 2906 | ext4_journal_stop(handle); | 
|  | 2907 | page_cache_release(page); | 
| Aneesh Kumar K.V | ae4d537 | 2008-09-13 13:10:25 -0400 | [diff] [blame] | 2908 | /* | 
|  | 2909 | * block_write_begin may have instantiated a few blocks | 
|  | 2910 | * outside i_size.  Trim these off again. Don't need | 
|  | 2911 | * i_size_read because we hold i_mutex. | 
|  | 2912 | */ | 
|  | 2913 | if (pos + len > inode->i_size) | 
|  | 2914 | vmtruncate(inode, inode->i_size); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2915 | } | 
|  | 2916 |  | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2917 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) | 
|  | 2918 | goto retry; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2919 | out: | 
|  | 2920 | return ret; | 
|  | 2921 | } | 
|  | 2922 |  | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2923 | /* | 
|  | 2924 | * Check if we should update i_disksize | 
|  | 2925 | * when write to the end of file but not require block allocation | 
|  | 2926 | */ | 
|  | 2927 | static int ext4_da_should_update_i_disksize(struct page *page, | 
|  | 2928 | unsigned long offset) | 
|  | 2929 | { | 
|  | 2930 | struct buffer_head *bh; | 
|  | 2931 | struct inode *inode = page->mapping->host; | 
|  | 2932 | unsigned int idx; | 
|  | 2933 | int i; | 
|  | 2934 |  | 
|  | 2935 | bh = page_buffers(page); | 
|  | 2936 | idx = offset >> inode->i_blkbits; | 
|  | 2937 |  | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2938 | for (i = 0; i < idx; i++) | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2939 | bh = bh->b_this_page; | 
|  | 2940 |  | 
| Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2941 | if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2942 | return 0; | 
|  | 2943 | return 1; | 
|  | 2944 | } | 
|  | 2945 |  | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2946 | static int ext4_da_write_end(struct file *file, | 
|  | 2947 | struct address_space *mapping, | 
|  | 2948 | loff_t pos, unsigned len, unsigned copied, | 
|  | 2949 | struct page *page, void *fsdata) | 
|  | 2950 | { | 
|  | 2951 | struct inode *inode = mapping->host; | 
|  | 2952 | int ret = 0, ret2; | 
|  | 2953 | handle_t *handle = ext4_journal_current_handle(); | 
|  | 2954 | loff_t new_i_size; | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2955 | unsigned long start, end; | 
| Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 2956 | int write_mode = (int)(unsigned long)fsdata; | 
|  | 2957 |  | 
|  | 2958 | if (write_mode == FALL_BACK_TO_NONDELALLOC) { | 
|  | 2959 | if (ext4_should_order_data(inode)) { | 
|  | 2960 | return ext4_ordered_write_end(file, mapping, pos, | 
|  | 2961 | len, copied, page, fsdata); | 
|  | 2962 | } else if (ext4_should_writeback_data(inode)) { | 
|  | 2963 | return ext4_writeback_write_end(file, mapping, pos, | 
|  | 2964 | len, copied, page, fsdata); | 
|  | 2965 | } else { | 
|  | 2966 | BUG(); | 
|  | 2967 | } | 
|  | 2968 | } | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2969 |  | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 2970 | trace_ext4_da_write_end(inode, pos, len, copied); | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2971 | start = pos & (PAGE_CACHE_SIZE - 1); | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2972 | end = start + copied - 1; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2973 |  | 
|  | 2974 | /* | 
|  | 2975 | * generic_write_end() will run mark_inode_dirty() if i_size | 
|  | 2976 | * changes.  So let's piggyback the i_disksize mark_inode_dirty | 
|  | 2977 | * into that. | 
|  | 2978 | */ | 
|  | 2979 |  | 
|  | 2980 | new_i_size = pos + copied; | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2981 | if (new_i_size > EXT4_I(inode)->i_disksize) { | 
|  | 2982 | if (ext4_da_should_update_i_disksize(page, end)) { | 
|  | 2983 | down_write(&EXT4_I(inode)->i_data_sem); | 
|  | 2984 | if (new_i_size > EXT4_I(inode)->i_disksize) { | 
|  | 2985 | /* | 
|  | 2986 | * Updating i_disksize when extending file | 
|  | 2987 | * without needing block allocation | 
|  | 2988 | */ | 
|  | 2989 | if (ext4_should_order_data(inode)) | 
|  | 2990 | ret = ext4_jbd2_file_inode(handle, | 
|  | 2991 | inode); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2992 |  | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2993 | EXT4_I(inode)->i_disksize = new_i_size; | 
|  | 2994 | } | 
|  | 2995 | up_write(&EXT4_I(inode)->i_data_sem); | 
| Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 2996 | /* We need to mark inode dirty even if | 
|  | 2997 | * new_i_size is less that inode->i_size | 
|  | 2998 | * bu greater than i_disksize.(hint delalloc) | 
|  | 2999 | */ | 
|  | 3000 | ext4_mark_inode_dirty(handle, inode); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3001 | } | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3002 | } | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3003 | ret2 = generic_write_end(file, mapping, pos, len, copied, | 
|  | 3004 | page, fsdata); | 
|  | 3005 | copied = ret2; | 
|  | 3006 | if (ret2 < 0) | 
|  | 3007 | ret = ret2; | 
|  | 3008 | ret2 = ext4_journal_stop(handle); | 
|  | 3009 | if (!ret) | 
|  | 3010 | ret = ret2; | 
|  | 3011 |  | 
|  | 3012 | return ret ? ret : copied; | 
|  | 3013 | } | 
|  | 3014 |  | 
|  | 3015 | static void ext4_da_invalidatepage(struct page *page, unsigned long offset) | 
|  | 3016 | { | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3017 | /* | 
|  | 3018 | * Drop reserved blocks | 
|  | 3019 | */ | 
|  | 3020 | BUG_ON(!PageLocked(page)); | 
|  | 3021 | if (!page_has_buffers(page)) | 
|  | 3022 | goto out; | 
|  | 3023 |  | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 3024 | ext4_da_page_release_reservation(page, offset); | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3025 |  | 
|  | 3026 | out: | 
|  | 3027 | ext4_invalidatepage(page, offset); | 
|  | 3028 |  | 
|  | 3029 | return; | 
|  | 3030 | } | 
|  | 3031 |  | 
| Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 3032 | /* | 
|  | 3033 | * Force all delayed allocation blocks to be allocated for a given inode. | 
|  | 3034 | */ | 
|  | 3035 | int ext4_alloc_da_blocks(struct inode *inode) | 
|  | 3036 | { | 
|  | 3037 | if (!EXT4_I(inode)->i_reserved_data_blocks && | 
|  | 3038 | !EXT4_I(inode)->i_reserved_meta_blocks) | 
|  | 3039 | return 0; | 
|  | 3040 |  | 
|  | 3041 | /* | 
|  | 3042 | * We do something simple for now.  The filemap_flush() will | 
|  | 3043 | * also start triggering a write of the data blocks, which is | 
|  | 3044 | * not strictly speaking necessary (and for users of | 
|  | 3045 | * laptop_mode, not even desirable).  However, to do otherwise | 
|  | 3046 | * would require replicating code paths in: | 
|  | 3047 | * | 
|  | 3048 | * ext4_da_writepages() -> | 
|  | 3049 | *    write_cache_pages() ---> (via passed in callback function) | 
|  | 3050 | *        __mpage_da_writepage() --> | 
|  | 3051 | *           mpage_add_bh_to_extent() | 
|  | 3052 | *           mpage_da_map_blocks() | 
|  | 3053 | * | 
|  | 3054 | * The problem is that write_cache_pages(), located in | 
|  | 3055 | * mm/page-writeback.c, marks pages clean in preparation for | 
|  | 3056 | * doing I/O, which is not desirable if we're not planning on | 
|  | 3057 | * doing I/O at all. | 
|  | 3058 | * | 
|  | 3059 | * We could call write_cache_pages(), and then redirty all of | 
|  | 3060 | * the pages by calling redirty_page_for_writeback() but that | 
|  | 3061 | * would be ugly in the extreme.  So instead we would need to | 
|  | 3062 | * replicate parts of the code in the above functions, | 
|  | 3063 | * simplifying them becuase we wouldn't actually intend to | 
|  | 3064 | * write out the pages, but rather only collect contiguous | 
|  | 3065 | * logical block extents, call the multi-block allocator, and | 
|  | 3066 | * then update the buffer heads with the block allocations. | 
|  | 3067 | * | 
|  | 3068 | * For now, though, we'll cheat by calling filemap_flush(), | 
|  | 3069 | * which will map the blocks, and start the I/O, but not | 
|  | 3070 | * actually wait for the I/O to complete. | 
|  | 3071 | */ | 
|  | 3072 | return filemap_flush(inode->i_mapping); | 
|  | 3073 | } | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3074 |  | 
|  | 3075 | /* | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3076 | * bmap() is special.  It gets used by applications such as lilo and by | 
|  | 3077 | * the swapper to find the on-disk block of a specific piece of data. | 
|  | 3078 | * | 
|  | 3079 | * Naturally, this is dangerous if the block concerned is still in the | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3080 | * journal.  If somebody makes a swapfile on an ext4 data-journaling | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3081 | * filesystem and enables swap, then they may get a nasty shock when the | 
|  | 3082 | * data getting swapped to that swapfile suddenly gets overwritten by | 
|  | 3083 | * the original zero's written out previously to the journal and | 
|  | 3084 | * awaiting writeback in the kernel's buffer cache. | 
|  | 3085 | * | 
|  | 3086 | * So, if we see any bmap calls here on a modified, data-journaled file, | 
|  | 3087 | * take extra steps to flush any blocks which might be in the cache. | 
|  | 3088 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3089 | static sector_t ext4_bmap(struct address_space *mapping, sector_t block) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3090 | { | 
|  | 3091 | struct inode *inode = mapping->host; | 
|  | 3092 | journal_t *journal; | 
|  | 3093 | int err; | 
|  | 3094 |  | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3095 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && | 
|  | 3096 | test_opt(inode->i_sb, DELALLOC)) { | 
|  | 3097 | /* | 
|  | 3098 | * With delalloc we want to sync the file | 
|  | 3099 | * so that we can make sure we allocate | 
|  | 3100 | * blocks for file | 
|  | 3101 | */ | 
|  | 3102 | filemap_write_and_wait(mapping); | 
|  | 3103 | } | 
|  | 3104 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3105 | if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3106 | /* | 
|  | 3107 | * This is a REALLY heavyweight approach, but the use of | 
|  | 3108 | * bmap on dirty files is expected to be extremely rare: | 
|  | 3109 | * only if we run lilo or swapon on a freshly made file | 
|  | 3110 | * do we expect this to happen. | 
|  | 3111 | * | 
|  | 3112 | * (bmap requires CAP_SYS_RAWIO so this does not | 
|  | 3113 | * represent an unprivileged user DOS attack --- we'd be | 
|  | 3114 | * in trouble if mortal users could trigger this path at | 
|  | 3115 | * will.) | 
|  | 3116 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3117 | * NB. EXT4_STATE_JDATA is not set on files other than | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3118 | * regular files.  If somebody wants to bmap a directory | 
|  | 3119 | * or symlink and gets confused because the buffer | 
|  | 3120 | * hasn't yet been flushed to disk, they deserve | 
|  | 3121 | * everything they get. | 
|  | 3122 | */ | 
|  | 3123 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3124 | EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA; | 
|  | 3125 | journal = EXT4_JOURNAL(inode); | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3126 | jbd2_journal_lock_updates(journal); | 
|  | 3127 | err = jbd2_journal_flush(journal); | 
|  | 3128 | jbd2_journal_unlock_updates(journal); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3129 |  | 
|  | 3130 | if (err) | 
|  | 3131 | return 0; | 
|  | 3132 | } | 
|  | 3133 |  | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3134 | return generic_block_bmap(mapping, block, ext4_get_block); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3135 | } | 
|  | 3136 |  | 
|  | 3137 | static int bget_one(handle_t *handle, struct buffer_head *bh) | 
|  | 3138 | { | 
|  | 3139 | get_bh(bh); | 
|  | 3140 | return 0; | 
|  | 3141 | } | 
|  | 3142 |  | 
|  | 3143 | static int bput_one(handle_t *handle, struct buffer_head *bh) | 
|  | 3144 | { | 
|  | 3145 | put_bh(bh); | 
|  | 3146 | return 0; | 
|  | 3147 | } | 
|  | 3148 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3149 | /* | 
| Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3150 | * Note that we don't need to start a transaction unless we're journaling data | 
|  | 3151 | * because we should have holes filled from ext4_page_mkwrite(). We even don't | 
|  | 3152 | * need to file the inode to the transaction's list in ordered mode because if | 
|  | 3153 | * we are writing back data added by write(), the inode is already there and if | 
|  | 3154 | * we are writing back data modified via mmap(), noone guarantees in which | 
|  | 3155 | * transaction the data will hit the disk. In case we are journaling data, we | 
|  | 3156 | * cannot start transaction directly because transaction start ranks above page | 
|  | 3157 | * lock so we have to do some magic. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3158 | * | 
| Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3159 | * In all journaling modes block_write_full_page() will start the I/O. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3160 | * | 
|  | 3161 | * Problem: | 
|  | 3162 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3163 | *	ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> | 
|  | 3164 | *		ext4_writepage() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3165 | * | 
|  | 3166 | * Similar for: | 
|  | 3167 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3168 | *	ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ... | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3169 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3170 | * Same applies to ext4_get_block().  We will deadlock on various things like | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3171 | * lock_journal and i_data_sem | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3172 | * | 
|  | 3173 | * Setting PF_MEMALLOC here doesn't work - too many internal memory | 
|  | 3174 | * allocations fail. | 
|  | 3175 | * | 
|  | 3176 | * 16May01: If we're reentered then journal_current_handle() will be | 
|  | 3177 | *	    non-zero. We simply *return*. | 
|  | 3178 | * | 
|  | 3179 | * 1 July 2001: @@@ FIXME: | 
|  | 3180 | *   In journalled data mode, a data buffer may be metadata against the | 
|  | 3181 | *   current transaction.  But the same file is part of a shared mapping | 
|  | 3182 | *   and someone does a writepage() on it. | 
|  | 3183 | * | 
|  | 3184 | *   We will move the buffer onto the async_data list, but *after* it has | 
|  | 3185 | *   been dirtied. So there's a small window where we have dirty data on | 
|  | 3186 | *   BJ_Metadata. | 
|  | 3187 | * | 
|  | 3188 | *   Note that this only applies to the last partial page in the file.  The | 
|  | 3189 | *   bit which block_write_full_page() uses prepare/commit for.  (That's | 
|  | 3190 | *   broken code anyway: it's wrong for msync()). | 
|  | 3191 | * | 
|  | 3192 | *   It's a rare case: affects the final partial page, for journalled data | 
|  | 3193 | *   where the file is subject to bith write() and writepage() in the same | 
|  | 3194 | *   transction.  To fix it we'll need a custom block_write_full_page(). | 
|  | 3195 | *   We'll probably need that anyway for journalling writepage() output. | 
|  | 3196 | * | 
|  | 3197 | * We don't honour synchronous mounts for writepage().  That would be | 
|  | 3198 | * disastrous.  Any write() or metadata operation will sync the fs for | 
|  | 3199 | * us. | 
|  | 3200 | * | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3201 | */ | 
| Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3202 | static int __ext4_normal_writepage(struct page *page, | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3203 | struct writeback_control *wbc) | 
|  | 3204 | { | 
|  | 3205 | struct inode *inode = page->mapping->host; | 
|  | 3206 |  | 
|  | 3207 | if (test_opt(inode->i_sb, NOBH)) | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 3208 | return nobh_writepage(page, noalloc_get_block_write, wbc); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3209 | else | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 3210 | return block_write_full_page(page, noalloc_get_block_write, | 
|  | 3211 | wbc); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3212 | } | 
|  | 3213 |  | 
| Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3214 | static int ext4_normal_writepage(struct page *page, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3215 | struct writeback_control *wbc) | 
|  | 3216 | { | 
|  | 3217 | struct inode *inode = page->mapping->host; | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3218 | loff_t size = i_size_read(inode); | 
|  | 3219 | loff_t len; | 
|  | 3220 |  | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 3221 | trace_ext4_normal_writepage(inode, page); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3222 | J_ASSERT(PageLocked(page)); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3223 | if (page->index == size >> PAGE_CACHE_SHIFT) | 
|  | 3224 | len = size & ~PAGE_CACHE_MASK; | 
|  | 3225 | else | 
|  | 3226 | len = PAGE_CACHE_SIZE; | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3227 |  | 
|  | 3228 | if (page_has_buffers(page)) { | 
|  | 3229 | /* if page has buffers it should all be mapped | 
|  | 3230 | * and allocated. If there are not buffers attached | 
|  | 3231 | * to the page we know the page is dirty but it lost | 
|  | 3232 | * buffers. That means that at some moment in time | 
|  | 3233 | * after write_begin() / write_end() has been called | 
|  | 3234 | * all buffers have been clean and thus they must have been | 
|  | 3235 | * written at least once. So they are all mapped and we can | 
|  | 3236 | * happily proceed with mapping them and writing the page. | 
|  | 3237 | */ | 
|  | 3238 | BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, | 
|  | 3239 | ext4_bh_unmapped_or_delay)); | 
|  | 3240 | } | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3241 |  | 
|  | 3242 | if (!ext4_journal_current_handle()) | 
| Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3243 | return __ext4_normal_writepage(page, wbc); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3244 |  | 
|  | 3245 | redirty_page_for_writepage(wbc, page); | 
|  | 3246 | unlock_page(page); | 
|  | 3247 | return 0; | 
|  | 3248 | } | 
|  | 3249 |  | 
|  | 3250 | static int __ext4_journalled_writepage(struct page *page, | 
|  | 3251 | struct writeback_control *wbc) | 
|  | 3252 | { | 
|  | 3253 | struct address_space *mapping = page->mapping; | 
|  | 3254 | struct inode *inode = mapping->host; | 
|  | 3255 | struct buffer_head *page_bufs; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3256 | handle_t *handle = NULL; | 
|  | 3257 | int ret = 0; | 
|  | 3258 | int err; | 
|  | 3259 |  | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3260 | ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE, | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 3261 | noalloc_get_block_write); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3262 | if (ret != 0) | 
|  | 3263 | goto out_unlock; | 
|  | 3264 |  | 
|  | 3265 | page_bufs = page_buffers(page); | 
|  | 3266 | walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE, NULL, | 
|  | 3267 | bget_one); | 
|  | 3268 | /* As soon as we unlock the page, it can go away, but we have | 
|  | 3269 | * references to buffers so we are safe */ | 
|  | 3270 | unlock_page(page); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3271 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3272 | handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3273 | if (IS_ERR(handle)) { | 
|  | 3274 | ret = PTR_ERR(handle); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3275 | goto out; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3276 | } | 
|  | 3277 |  | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3278 | ret = walk_page_buffers(handle, page_bufs, 0, | 
|  | 3279 | PAGE_CACHE_SIZE, NULL, do_journal_get_write_access); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3280 |  | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3281 | err = walk_page_buffers(handle, page_bufs, 0, | 
|  | 3282 | PAGE_CACHE_SIZE, NULL, write_end_fn); | 
|  | 3283 | if (ret == 0) | 
|  | 3284 | ret = err; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3285 | err = ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3286 | if (!ret) | 
|  | 3287 | ret = err; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3288 |  | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3289 | walk_page_buffers(handle, page_bufs, 0, | 
|  | 3290 | PAGE_CACHE_SIZE, NULL, bput_one); | 
|  | 3291 | EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; | 
|  | 3292 | goto out; | 
|  | 3293 |  | 
|  | 3294 | out_unlock: | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3295 | unlock_page(page); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3296 | out: | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3297 | return ret; | 
|  | 3298 | } | 
|  | 3299 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3300 | static int ext4_journalled_writepage(struct page *page, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3301 | struct writeback_control *wbc) | 
|  | 3302 | { | 
|  | 3303 | struct inode *inode = page->mapping->host; | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3304 | loff_t size = i_size_read(inode); | 
|  | 3305 | loff_t len; | 
|  | 3306 |  | 
| Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame^] | 3307 | trace_ext4_journalled_writepage(inode, page); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3308 | J_ASSERT(PageLocked(page)); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3309 | if (page->index == size >> PAGE_CACHE_SHIFT) | 
|  | 3310 | len = size & ~PAGE_CACHE_MASK; | 
|  | 3311 | else | 
|  | 3312 | len = PAGE_CACHE_SIZE; | 
| Aneesh Kumar K.V | f0e6c98 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3313 |  | 
|  | 3314 | if (page_has_buffers(page)) { | 
|  | 3315 | /* if page has buffers it should all be mapped | 
|  | 3316 | * and allocated. If there are not buffers attached | 
|  | 3317 | * to the page we know the page is dirty but it lost | 
|  | 3318 | * buffers. That means that at some moment in time | 
|  | 3319 | * after write_begin() / write_end() has been called | 
|  | 3320 | * all buffers have been clean and thus they must have been | 
|  | 3321 | * written at least once. So they are all mapped and we can | 
|  | 3322 | * happily proceed with mapping them and writing the page. | 
|  | 3323 | */ | 
|  | 3324 | BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, | 
|  | 3325 | ext4_bh_unmapped_or_delay)); | 
|  | 3326 | } | 
| 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 | if (ext4_journal_current_handle()) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3329 | goto no_write; | 
|  | 3330 |  | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3331 | if (PageChecked(page)) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3332 | /* | 
|  | 3333 | * It's mmapped pagecache.  Add buffers and journal it.  There | 
|  | 3334 | * doesn't seem much point in redirtying the page here. | 
|  | 3335 | */ | 
|  | 3336 | ClearPageChecked(page); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3337 | return __ext4_journalled_writepage(page, wbc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3338 | } else { | 
|  | 3339 | /* | 
|  | 3340 | * It may be a page full of checkpoint-mode buffers.  We don't | 
|  | 3341 | * really know unless we go poke around in the buffer_heads. | 
|  | 3342 | * But block_write_full_page will do the right thing. | 
|  | 3343 | */ | 
| Theodore Ts'o | b920c75 | 2009-05-14 00:54:29 -0400 | [diff] [blame] | 3344 | return block_write_full_page(page, noalloc_get_block_write, | 
|  | 3345 | wbc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3346 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3347 | no_write: | 
|  | 3348 | redirty_page_for_writepage(wbc, page); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3349 | unlock_page(page); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3350 | return 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3351 | } | 
|  | 3352 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3353 | static int ext4_readpage(struct file *file, struct page *page) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3354 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3355 | return mpage_readpage(page, ext4_get_block); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3356 | } | 
|  | 3357 |  | 
|  | 3358 | static int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3359 | ext4_readpages(struct file *file, struct address_space *mapping, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3360 | struct list_head *pages, unsigned nr_pages) | 
|  | 3361 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3362 | return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3363 | } | 
|  | 3364 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3365 | static void ext4_invalidatepage(struct page *page, unsigned long offset) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3366 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3367 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3368 |  | 
|  | 3369 | /* | 
|  | 3370 | * If it's a full truncate we just forget about the pending dirtying | 
|  | 3371 | */ | 
|  | 3372 | if (offset == 0) | 
|  | 3373 | ClearPageChecked(page); | 
|  | 3374 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3375 | if (journal) | 
|  | 3376 | jbd2_journal_invalidatepage(journal, page, offset); | 
|  | 3377 | else | 
|  | 3378 | block_invalidatepage(page, offset); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3379 | } | 
|  | 3380 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3381 | static int ext4_releasepage(struct page *page, gfp_t wait) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3382 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3383 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3384 |  | 
|  | 3385 | WARN_ON(PageChecked(page)); | 
|  | 3386 | if (!page_has_buffers(page)) | 
|  | 3387 | return 0; | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3388 | if (journal) | 
|  | 3389 | return jbd2_journal_try_to_free_buffers(journal, page, wait); | 
|  | 3390 | else | 
|  | 3391 | return try_to_free_buffers(page); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3392 | } | 
|  | 3393 |  | 
|  | 3394 | /* | 
|  | 3395 | * If the O_DIRECT write will extend the file then add this inode to the | 
|  | 3396 | * orphan list.  So recovery will truncate it back to the original size | 
|  | 3397 | * if the machine crashes during the write. | 
|  | 3398 | * | 
|  | 3399 | * 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] | 3400 | * crashes then stale disk data _may_ be exposed inside the file. But current | 
|  | 3401 | * 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] | 3402 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3403 | static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3404 | const struct iovec *iov, loff_t offset, | 
|  | 3405 | unsigned long nr_segs) | 
|  | 3406 | { | 
|  | 3407 | struct file *file = iocb->ki_filp; | 
|  | 3408 | struct inode *inode = file->f_mapping->host; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3409 | struct ext4_inode_info *ei = EXT4_I(inode); | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3410 | handle_t *handle; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3411 | ssize_t ret; | 
|  | 3412 | int orphan = 0; | 
|  | 3413 | size_t count = iov_length(iov, nr_segs); | 
|  | 3414 |  | 
|  | 3415 | if (rw == WRITE) { | 
|  | 3416 | loff_t final_size = offset + count; | 
|  | 3417 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3418 | if (final_size > inode->i_size) { | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3419 | /* Credits for sb + inode write */ | 
|  | 3420 | handle = ext4_journal_start(inode, 2); | 
|  | 3421 | if (IS_ERR(handle)) { | 
|  | 3422 | ret = PTR_ERR(handle); | 
|  | 3423 | goto out; | 
|  | 3424 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3425 | ret = ext4_orphan_add(handle, inode); | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3426 | if (ret) { | 
|  | 3427 | ext4_journal_stop(handle); | 
|  | 3428 | goto out; | 
|  | 3429 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3430 | orphan = 1; | 
|  | 3431 | ei->i_disksize = inode->i_size; | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3432 | ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3433 | } | 
|  | 3434 | } | 
|  | 3435 |  | 
|  | 3436 | ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, | 
|  | 3437 | offset, nr_segs, | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3438 | ext4_get_block, NULL); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3439 |  | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3440 | if (orphan) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3441 | int err; | 
|  | 3442 |  | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3443 | /* Credits for sb + inode write */ | 
|  | 3444 | handle = ext4_journal_start(inode, 2); | 
|  | 3445 | if (IS_ERR(handle)) { | 
|  | 3446 | /* This is really bad luck. We've written the data | 
|  | 3447 | * but cannot extend i_size. Bail out and pretend | 
|  | 3448 | * the write failed... */ | 
|  | 3449 | ret = PTR_ERR(handle); | 
|  | 3450 | goto out; | 
|  | 3451 | } | 
|  | 3452 | if (inode->i_nlink) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3453 | ext4_orphan_del(handle, inode); | 
| Jan Kara | 7fb5409 | 2008-02-10 01:08:38 -0500 | [diff] [blame] | 3454 | if (ret > 0) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3455 | loff_t end = offset + ret; | 
|  | 3456 | if (end > inode->i_size) { | 
|  | 3457 | ei->i_disksize = end; | 
|  | 3458 | i_size_write(inode, end); | 
|  | 3459 | /* | 
|  | 3460 | * We're going to return a positive `ret' | 
|  | 3461 | * here due to non-zero-length I/O, so there's | 
|  | 3462 | * no way of reporting error returns from | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3463 | * ext4_mark_inode_dirty() to userspace.  So | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3464 | * ignore it. | 
|  | 3465 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3466 | ext4_mark_inode_dirty(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3467 | } | 
|  | 3468 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3469 | err = ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3470 | if (ret == 0) | 
|  | 3471 | ret = err; | 
|  | 3472 | } | 
|  | 3473 | out: | 
|  | 3474 | return ret; | 
|  | 3475 | } | 
|  | 3476 |  | 
|  | 3477 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3478 | * Pages can be marked dirty completely asynchronously from ext4's journalling | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3479 | * activity.  By filemap_sync_pte(), try_to_unmap_one(), etc.  We cannot do | 
|  | 3480 | * much here because ->set_page_dirty is called under VFS locks.  The page is | 
|  | 3481 | * not necessarily locked. | 
|  | 3482 | * | 
|  | 3483 | * We cannot just dirty the page and leave attached buffers clean, because the | 
|  | 3484 | * buffers' dirty state is "definitive".  We cannot just set the buffers dirty | 
|  | 3485 | * or jbddirty because all the journalling code will explode. | 
|  | 3486 | * | 
|  | 3487 | * So what we do is to mark the page "pending dirty" and next time writepage | 
|  | 3488 | * is called, propagate that into the buffers appropriately. | 
|  | 3489 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3490 | static int ext4_journalled_set_page_dirty(struct page *page) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3491 | { | 
|  | 3492 | SetPageChecked(page); | 
|  | 3493 | return __set_page_dirty_nobuffers(page); | 
|  | 3494 | } | 
|  | 3495 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3496 | static const struct address_space_operations ext4_ordered_aops = { | 
| Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3497 | .readpage		= ext4_readpage, | 
|  | 3498 | .readpages		= ext4_readpages, | 
|  | 3499 | .writepage		= ext4_normal_writepage, | 
|  | 3500 | .sync_page		= block_sync_page, | 
|  | 3501 | .write_begin		= ext4_write_begin, | 
|  | 3502 | .write_end		= ext4_ordered_write_end, | 
|  | 3503 | .bmap			= ext4_bmap, | 
|  | 3504 | .invalidatepage		= ext4_invalidatepage, | 
|  | 3505 | .releasepage		= ext4_releasepage, | 
|  | 3506 | .direct_IO		= ext4_direct_IO, | 
|  | 3507 | .migratepage		= buffer_migrate_page, | 
|  | 3508 | .is_partially_uptodate  = block_is_partially_uptodate, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3509 | }; | 
|  | 3510 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3511 | static const struct address_space_operations ext4_writeback_aops = { | 
| Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3512 | .readpage		= ext4_readpage, | 
|  | 3513 | .readpages		= ext4_readpages, | 
|  | 3514 | .writepage		= ext4_normal_writepage, | 
|  | 3515 | .sync_page		= block_sync_page, | 
|  | 3516 | .write_begin		= ext4_write_begin, | 
|  | 3517 | .write_end		= ext4_writeback_write_end, | 
|  | 3518 | .bmap			= ext4_bmap, | 
|  | 3519 | .invalidatepage		= ext4_invalidatepage, | 
|  | 3520 | .releasepage		= ext4_releasepage, | 
|  | 3521 | .direct_IO		= ext4_direct_IO, | 
|  | 3522 | .migratepage		= buffer_migrate_page, | 
|  | 3523 | .is_partially_uptodate  = block_is_partially_uptodate, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3524 | }; | 
|  | 3525 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3526 | static const struct address_space_operations ext4_journalled_aops = { | 
| Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3527 | .readpage		= ext4_readpage, | 
|  | 3528 | .readpages		= ext4_readpages, | 
|  | 3529 | .writepage		= ext4_journalled_writepage, | 
|  | 3530 | .sync_page		= block_sync_page, | 
|  | 3531 | .write_begin		= ext4_write_begin, | 
|  | 3532 | .write_end		= ext4_journalled_write_end, | 
|  | 3533 | .set_page_dirty		= ext4_journalled_set_page_dirty, | 
|  | 3534 | .bmap			= ext4_bmap, | 
|  | 3535 | .invalidatepage		= ext4_invalidatepage, | 
|  | 3536 | .releasepage		= ext4_releasepage, | 
|  | 3537 | .is_partially_uptodate  = block_is_partially_uptodate, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3538 | }; | 
|  | 3539 |  | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3540 | static const struct address_space_operations ext4_da_aops = { | 
| Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 3541 | .readpage		= ext4_readpage, | 
|  | 3542 | .readpages		= ext4_readpages, | 
|  | 3543 | .writepage		= ext4_da_writepage, | 
|  | 3544 | .writepages		= ext4_da_writepages, | 
|  | 3545 | .sync_page		= block_sync_page, | 
|  | 3546 | .write_begin		= ext4_da_write_begin, | 
|  | 3547 | .write_end		= ext4_da_write_end, | 
|  | 3548 | .bmap			= ext4_bmap, | 
|  | 3549 | .invalidatepage		= ext4_da_invalidatepage, | 
|  | 3550 | .releasepage		= ext4_releasepage, | 
|  | 3551 | .direct_IO		= ext4_direct_IO, | 
|  | 3552 | .migratepage		= buffer_migrate_page, | 
|  | 3553 | .is_partially_uptodate  = block_is_partially_uptodate, | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3554 | }; | 
|  | 3555 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3556 | void ext4_set_aops(struct inode *inode) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3557 | { | 
| Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3558 | if (ext4_should_order_data(inode) && | 
|  | 3559 | test_opt(inode->i_sb, DELALLOC)) | 
|  | 3560 | inode->i_mapping->a_ops = &ext4_da_aops; | 
|  | 3561 | else if (ext4_should_order_data(inode)) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3562 | inode->i_mapping->a_ops = &ext4_ordered_aops; | 
| Alex Tomas | 6476924 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3563 | else if (ext4_should_writeback_data(inode) && | 
|  | 3564 | test_opt(inode->i_sb, DELALLOC)) | 
|  | 3565 | inode->i_mapping->a_ops = &ext4_da_aops; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3566 | else if (ext4_should_writeback_data(inode)) | 
|  | 3567 | inode->i_mapping->a_ops = &ext4_writeback_aops; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3568 | else | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3569 | inode->i_mapping->a_ops = &ext4_journalled_aops; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3570 | } | 
|  | 3571 |  | 
|  | 3572 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3573 | * ext4_block_truncate_page() zeroes out a mapping from file offset `from' | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3574 | * up to the end of the block which corresponds to `from'. | 
|  | 3575 | * This required during truncate. We need to physically zero the tail end | 
|  | 3576 | * of that block so it doesn't yield old data if the file is later grown. | 
|  | 3577 | */ | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3578 | int ext4_block_truncate_page(handle_t *handle, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3579 | struct address_space *mapping, loff_t from) | 
|  | 3580 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3581 | ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3582 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3583 | unsigned blocksize, length, pos; | 
|  | 3584 | ext4_lblk_t iblock; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3585 | struct inode *inode = mapping->host; | 
|  | 3586 | struct buffer_head *bh; | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3587 | struct page *page; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3588 | int err = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3589 |  | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3590 | page = grab_cache_page(mapping, from >> PAGE_CACHE_SHIFT); | 
|  | 3591 | if (!page) | 
|  | 3592 | return -EINVAL; | 
|  | 3593 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3594 | blocksize = inode->i_sb->s_blocksize; | 
|  | 3595 | length = blocksize - (offset & (blocksize - 1)); | 
|  | 3596 | iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); | 
|  | 3597 |  | 
|  | 3598 | /* | 
|  | 3599 | * For "nobh" option,  we can only work if we don't need to | 
|  | 3600 | * read-in the page - otherwise we create buffers to do the IO. | 
|  | 3601 | */ | 
|  | 3602 | if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) && | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3603 | ext4_should_writeback_data(inode) && PageUptodate(page)) { | 
| Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 3604 | zero_user(page, offset, length); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3605 | set_page_dirty(page); | 
|  | 3606 | goto unlock; | 
|  | 3607 | } | 
|  | 3608 |  | 
|  | 3609 | if (!page_has_buffers(page)) | 
|  | 3610 | create_empty_buffers(page, blocksize, 0); | 
|  | 3611 |  | 
|  | 3612 | /* Find the buffer that contains "offset" */ | 
|  | 3613 | bh = page_buffers(page); | 
|  | 3614 | pos = blocksize; | 
|  | 3615 | while (offset >= pos) { | 
|  | 3616 | bh = bh->b_this_page; | 
|  | 3617 | iblock++; | 
|  | 3618 | pos += blocksize; | 
|  | 3619 | } | 
|  | 3620 |  | 
|  | 3621 | err = 0; | 
|  | 3622 | if (buffer_freed(bh)) { | 
|  | 3623 | BUFFER_TRACE(bh, "freed: skip"); | 
|  | 3624 | goto unlock; | 
|  | 3625 | } | 
|  | 3626 |  | 
|  | 3627 | if (!buffer_mapped(bh)) { | 
|  | 3628 | BUFFER_TRACE(bh, "unmapped"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3629 | ext4_get_block(inode, iblock, bh, 0); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3630 | /* unmapped? It's a hole - nothing to do */ | 
|  | 3631 | if (!buffer_mapped(bh)) { | 
|  | 3632 | BUFFER_TRACE(bh, "still unmapped"); | 
|  | 3633 | goto unlock; | 
|  | 3634 | } | 
|  | 3635 | } | 
|  | 3636 |  | 
|  | 3637 | /* Ok, it's mapped. Make sure it's up-to-date */ | 
|  | 3638 | if (PageUptodate(page)) | 
|  | 3639 | set_buffer_uptodate(bh); | 
|  | 3640 |  | 
|  | 3641 | if (!buffer_uptodate(bh)) { | 
|  | 3642 | err = -EIO; | 
|  | 3643 | ll_rw_block(READ, 1, &bh); | 
|  | 3644 | wait_on_buffer(bh); | 
|  | 3645 | /* Uhhuh. Read error. Complain and punt. */ | 
|  | 3646 | if (!buffer_uptodate(bh)) | 
|  | 3647 | goto unlock; | 
|  | 3648 | } | 
|  | 3649 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3650 | if (ext4_should_journal_data(inode)) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3651 | BUFFER_TRACE(bh, "get write access"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3652 | err = ext4_journal_get_write_access(handle, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3653 | if (err) | 
|  | 3654 | goto unlock; | 
|  | 3655 | } | 
|  | 3656 |  | 
| Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 3657 | zero_user(page, offset, length); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3658 |  | 
|  | 3659 | BUFFER_TRACE(bh, "zeroed end of block"); | 
|  | 3660 |  | 
|  | 3661 | err = 0; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3662 | if (ext4_should_journal_data(inode)) { | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3663 | err = ext4_handle_dirty_metadata(handle, inode, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3664 | } else { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3665 | if (ext4_should_order_data(inode)) | 
| Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3666 | err = ext4_jbd2_file_inode(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3667 | mark_buffer_dirty(bh); | 
|  | 3668 | } | 
|  | 3669 |  | 
|  | 3670 | unlock: | 
|  | 3671 | unlock_page(page); | 
|  | 3672 | page_cache_release(page); | 
|  | 3673 | return err; | 
|  | 3674 | } | 
|  | 3675 |  | 
|  | 3676 | /* | 
|  | 3677 | * Probably it should be a library function... search for first non-zero word | 
|  | 3678 | * or memcmp with zero_page, whatever is better for particular architecture. | 
|  | 3679 | * Linus? | 
|  | 3680 | */ | 
|  | 3681 | static inline int all_zeroes(__le32 *p, __le32 *q) | 
|  | 3682 | { | 
|  | 3683 | while (p < q) | 
|  | 3684 | if (*p++) | 
|  | 3685 | return 0; | 
|  | 3686 | return 1; | 
|  | 3687 | } | 
|  | 3688 |  | 
|  | 3689 | /** | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3690 | *	ext4_find_shared - find the indirect blocks for partial truncation. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3691 | *	@inode:	  inode in question | 
|  | 3692 | *	@depth:	  depth of the affected branch | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3693 | *	@offsets: offsets of pointers in that branch (see ext4_block_to_path) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3694 | *	@chain:	  place to store the pointers to partial indirect blocks | 
|  | 3695 | *	@top:	  place to the (detached) top of branch | 
|  | 3696 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3697 | *	This is a helper function used by ext4_truncate(). | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3698 | * | 
|  | 3699 | *	When we do truncate() we may have to clean the ends of several | 
|  | 3700 | *	indirect blocks but leave the blocks themselves alive. Block is | 
|  | 3701 | *	partially truncated if some data below the new i_size is refered | 
|  | 3702 | *	from it (and it is on the path to the first completely truncated | 
|  | 3703 | *	data block, indeed).  We have to free the top of that path along | 
|  | 3704 | *	with everything to the right of the path. Since no allocation | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3705 | *	past the truncation point is possible until ext4_truncate() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3706 | *	finishes, we may safely do the latter, but top of branch may | 
|  | 3707 | *	require special attention - pageout below the truncation point | 
|  | 3708 | *	might try to populate it. | 
|  | 3709 | * | 
|  | 3710 | *	We atomically detach the top of branch from the tree, store the | 
|  | 3711 | *	block number of its root in *@top, pointers to buffer_heads of | 
|  | 3712 | *	partially truncated blocks - in @chain[].bh and pointers to | 
|  | 3713 | *	their last elements that should not be removed - in | 
|  | 3714 | *	@chain[].p. Return value is the pointer to last filled element | 
|  | 3715 | *	of @chain. | 
|  | 3716 | * | 
|  | 3717 | *	The work left to caller to do the actual freeing of subtrees: | 
|  | 3718 | *		a) free the subtree starting from *@top | 
|  | 3719 | *		b) free the subtrees whose roots are stored in | 
|  | 3720 | *			(@chain[i].p+1 .. end of @chain[i].bh->b_data) | 
|  | 3721 | *		c) free the subtrees growing from the inode past the @chain[0]. | 
|  | 3722 | *			(no partially truncated stuff there).  */ | 
|  | 3723 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3724 | static Indirect *ext4_find_shared(struct inode *inode, int depth, | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3725 | ext4_lblk_t offsets[4], Indirect chain[4], __le32 *top) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3726 | { | 
|  | 3727 | Indirect *partial, *p; | 
|  | 3728 | int k, err; | 
|  | 3729 |  | 
|  | 3730 | *top = 0; | 
|  | 3731 | /* Make k index the deepest non-null offest + 1 */ | 
|  | 3732 | for (k = depth; k > 1 && !offsets[k-1]; k--) | 
|  | 3733 | ; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3734 | partial = ext4_get_branch(inode, k, offsets, chain, &err); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3735 | /* Writer: pointers */ | 
|  | 3736 | if (!partial) | 
|  | 3737 | partial = chain + k-1; | 
|  | 3738 | /* | 
|  | 3739 | * If the branch acquired continuation since we've looked at it - | 
|  | 3740 | * fine, it should all survive and (new) top doesn't belong to us. | 
|  | 3741 | */ | 
|  | 3742 | if (!partial->key && *partial->p) | 
|  | 3743 | /* Writer: end */ | 
|  | 3744 | goto no_top; | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3745 | 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] | 3746 | ; | 
|  | 3747 | /* | 
|  | 3748 | * OK, we've found the last block that must survive. The rest of our | 
|  | 3749 | * branch should be detached before unlocking. However, if that rest | 
|  | 3750 | * of branch is all ours and does not grow immediately from the inode | 
|  | 3751 | * it's easier to cheat and just decrement partial->p. | 
|  | 3752 | */ | 
|  | 3753 | if (p == chain + k - 1 && p > chain) { | 
|  | 3754 | p->p--; | 
|  | 3755 | } else { | 
|  | 3756 | *top = *p->p; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3757 | /* Nope, don't do this in ext4.  Must leave the tree intact */ | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3758 | #if 0 | 
|  | 3759 | *p->p = 0; | 
|  | 3760 | #endif | 
|  | 3761 | } | 
|  | 3762 | /* Writer: end */ | 
|  | 3763 |  | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3764 | while (partial > p) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3765 | brelse(partial->bh); | 
|  | 3766 | partial--; | 
|  | 3767 | } | 
|  | 3768 | no_top: | 
|  | 3769 | return partial; | 
|  | 3770 | } | 
|  | 3771 |  | 
|  | 3772 | /* | 
|  | 3773 | * Zero a number of block pointers in either an inode or an indirect block. | 
|  | 3774 | * If we restart the transaction we must again get write access to the | 
|  | 3775 | * indirect block for further modification. | 
|  | 3776 | * | 
|  | 3777 | * We release `count' blocks on disk, but (last - first) may be greater | 
|  | 3778 | * than `count' because there can be holes in there. | 
|  | 3779 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3780 | static void ext4_clear_blocks(handle_t *handle, struct inode *inode, | 
|  | 3781 | struct buffer_head *bh, ext4_fsblk_t block_to_free, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3782 | unsigned long count, __le32 *first, __le32 *last) | 
|  | 3783 | { | 
|  | 3784 | __le32 *p; | 
|  | 3785 | if (try_to_extend_transaction(handle, inode)) { | 
|  | 3786 | if (bh) { | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3787 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); | 
|  | 3788 | ext4_handle_dirty_metadata(handle, inode, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3789 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3790 | ext4_mark_inode_dirty(handle, inode); | 
|  | 3791 | ext4_journal_test_restart(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3792 | if (bh) { | 
|  | 3793 | BUFFER_TRACE(bh, "retaking write access"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3794 | ext4_journal_get_write_access(handle, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3795 | } | 
|  | 3796 | } | 
|  | 3797 |  | 
|  | 3798 | /* | 
|  | 3799 | * Any buffers which are on the journal will be in memory. We find | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3800 | * them on the hash table so jbd2_journal_revoke() will run jbd2_journal_forget() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3801 | * on them.  We've already detached each block from the file, so | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3802 | * bforget() in jbd2_journal_forget() should be safe. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3803 | * | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3804 | * AKPM: turn on bforget in jbd2_journal_forget()!!! | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3805 | */ | 
|  | 3806 | for (p = first; p < last; p++) { | 
|  | 3807 | u32 nr = le32_to_cpu(*p); | 
|  | 3808 | if (nr) { | 
| Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3809 | struct buffer_head *tbh; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3810 |  | 
|  | 3811 | *p = 0; | 
| Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3812 | tbh = sb_find_get_block(inode->i_sb, nr); | 
|  | 3813 | ext4_forget(handle, 0, inode, tbh, nr); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3814 | } | 
|  | 3815 | } | 
|  | 3816 |  | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3817 | ext4_free_blocks(handle, inode, block_to_free, count, 0); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3818 | } | 
|  | 3819 |  | 
|  | 3820 | /** | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3821 | * ext4_free_data - free a list of data blocks | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3822 | * @handle:	handle for this transaction | 
|  | 3823 | * @inode:	inode we are dealing with | 
|  | 3824 | * @this_bh:	indirect buffer_head which contains *@first and *@last | 
|  | 3825 | * @first:	array of block numbers | 
|  | 3826 | * @last:	points immediately past the end of array | 
|  | 3827 | * | 
|  | 3828 | * We are freeing all blocks refered from that array (numbers are stored as | 
|  | 3829 | * little-endian 32-bit) and updating @inode->i_blocks appropriately. | 
|  | 3830 | * | 
|  | 3831 | * We accumulate contiguous runs of blocks to free.  Conveniently, if these | 
|  | 3832 | * blocks are contiguous then releasing them at one time will only affect one | 
|  | 3833 | * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't | 
|  | 3834 | * actually use a lot of journal space. | 
|  | 3835 | * | 
|  | 3836 | * @this_bh will be %NULL if @first and @last point into the inode's direct | 
|  | 3837 | * block pointers. | 
|  | 3838 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3839 | static void ext4_free_data(handle_t *handle, struct inode *inode, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3840 | struct buffer_head *this_bh, | 
|  | 3841 | __le32 *first, __le32 *last) | 
|  | 3842 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3843 | ext4_fsblk_t block_to_free = 0;    /* Starting block # of a run */ | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3844 | unsigned long count = 0;	    /* Number of blocks in the run */ | 
|  | 3845 | __le32 *block_to_free_p = NULL;	    /* Pointer into inode/ind | 
|  | 3846 | corresponding to | 
|  | 3847 | block_to_free */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3848 | ext4_fsblk_t nr;		    /* Current block # */ | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3849 | __le32 *p;			    /* Pointer into inode/ind | 
|  | 3850 | for current block */ | 
|  | 3851 | int err; | 
|  | 3852 |  | 
|  | 3853 | if (this_bh) {				/* For indirect block */ | 
|  | 3854 | BUFFER_TRACE(this_bh, "get_write_access"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3855 | err = ext4_journal_get_write_access(handle, this_bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3856 | /* Important: if we can't update the indirect pointers | 
|  | 3857 | * to the blocks, we can't free them. */ | 
|  | 3858 | if (err) | 
|  | 3859 | return; | 
|  | 3860 | } | 
|  | 3861 |  | 
|  | 3862 | for (p = first; p < last; p++) { | 
|  | 3863 | nr = le32_to_cpu(*p); | 
|  | 3864 | if (nr) { | 
|  | 3865 | /* accumulate blocks to free if they're contiguous */ | 
|  | 3866 | if (count == 0) { | 
|  | 3867 | block_to_free = nr; | 
|  | 3868 | block_to_free_p = p; | 
|  | 3869 | count = 1; | 
|  | 3870 | } else if (nr == block_to_free + count) { | 
|  | 3871 | count++; | 
|  | 3872 | } else { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3873 | ext4_clear_blocks(handle, inode, this_bh, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3874 | block_to_free, | 
|  | 3875 | count, block_to_free_p, p); | 
|  | 3876 | block_to_free = nr; | 
|  | 3877 | block_to_free_p = p; | 
|  | 3878 | count = 1; | 
|  | 3879 | } | 
|  | 3880 | } | 
|  | 3881 | } | 
|  | 3882 |  | 
|  | 3883 | if (count > 0) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3884 | ext4_clear_blocks(handle, inode, this_bh, block_to_free, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3885 | count, block_to_free_p, p); | 
|  | 3886 |  | 
|  | 3887 | if (this_bh) { | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3888 | BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata"); | 
| Duane Griffin | 71dc8fb | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3889 |  | 
|  | 3890 | /* | 
|  | 3891 | * The buffer head should have an attached journal head at this | 
|  | 3892 | * point. However, if the data is corrupted and an indirect | 
|  | 3893 | * block pointed to itself, it would have been detached when | 
|  | 3894 | * the block was cleared. Check for this instead of OOPSing. | 
|  | 3895 | */ | 
| Theodore Ts'o | e7f0796 | 2009-01-20 09:50:19 -0500 | [diff] [blame] | 3896 | if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh)) | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3897 | ext4_handle_dirty_metadata(handle, inode, this_bh); | 
| Duane Griffin | 71dc8fb | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3898 | else | 
|  | 3899 | ext4_error(inode->i_sb, __func__, | 
|  | 3900 | "circular indirect block detected, " | 
|  | 3901 | "inode=%lu, block=%llu", | 
|  | 3902 | inode->i_ino, | 
|  | 3903 | (unsigned long long) this_bh->b_blocknr); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3904 | } | 
|  | 3905 | } | 
|  | 3906 |  | 
|  | 3907 | /** | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3908 | *	ext4_free_branches - free an array of branches | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3909 | *	@handle: JBD handle for this transaction | 
|  | 3910 | *	@inode:	inode we are dealing with | 
|  | 3911 | *	@parent_bh: the buffer_head which contains *@first and *@last | 
|  | 3912 | *	@first:	array of block numbers | 
|  | 3913 | *	@last:	pointer immediately past the end of array | 
|  | 3914 | *	@depth:	depth of the branches to free | 
|  | 3915 | * | 
|  | 3916 | *	We are freeing all blocks refered from these branches (numbers are | 
|  | 3917 | *	stored as little-endian 32-bit) and updating @inode->i_blocks | 
|  | 3918 | *	appropriately. | 
|  | 3919 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3920 | static void ext4_free_branches(handle_t *handle, struct inode *inode, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3921 | struct buffer_head *parent_bh, | 
|  | 3922 | __le32 *first, __le32 *last, int depth) | 
|  | 3923 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3924 | ext4_fsblk_t nr; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3925 | __le32 *p; | 
|  | 3926 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3927 | if (ext4_handle_is_aborted(handle)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3928 | return; | 
|  | 3929 |  | 
|  | 3930 | if (depth--) { | 
|  | 3931 | struct buffer_head *bh; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3932 | int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3933 | p = last; | 
|  | 3934 | while (--p >= first) { | 
|  | 3935 | nr = le32_to_cpu(*p); | 
|  | 3936 | if (!nr) | 
|  | 3937 | continue;		/* A hole */ | 
|  | 3938 |  | 
|  | 3939 | /* Go read the buffer for the next level down */ | 
|  | 3940 | bh = sb_bread(inode->i_sb, nr); | 
|  | 3941 |  | 
|  | 3942 | /* | 
|  | 3943 | * A read failure? Report error and clear slot | 
|  | 3944 | * (should be rare). | 
|  | 3945 | */ | 
|  | 3946 | if (!bh) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3947 | ext4_error(inode->i_sb, "ext4_free_branches", | 
| Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 3948 | "Read failure, inode=%lu, block=%llu", | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3949 | inode->i_ino, nr); | 
|  | 3950 | continue; | 
|  | 3951 | } | 
|  | 3952 |  | 
|  | 3953 | /* This zaps the entire block.  Bottom up. */ | 
|  | 3954 | BUFFER_TRACE(bh, "free child branches"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3955 | ext4_free_branches(handle, inode, bh, | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3956 | (__le32 *) bh->b_data, | 
|  | 3957 | (__le32 *) bh->b_data + addr_per_block, | 
|  | 3958 | depth); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3959 |  | 
|  | 3960 | /* | 
|  | 3961 | * We've probably journalled the indirect block several | 
|  | 3962 | * times during the truncate.  But it's no longer | 
|  | 3963 | * needed and we now drop it from the transaction via | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3964 | * jbd2_journal_revoke(). | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3965 | * | 
|  | 3966 | * That's easy if it's exclusively part of this | 
|  | 3967 | * transaction.  But if it's part of the committing | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 3968 | * transaction then jbd2_journal_forget() will simply | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3969 | * brelse() it.  That means that if the underlying | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3970 | * block is reallocated in ext4_get_block(), | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3971 | * unmap_underlying_metadata() will find this block | 
|  | 3972 | * and will try to get rid of it.  damn, damn. | 
|  | 3973 | * | 
|  | 3974 | * If this block has already been committed to the | 
|  | 3975 | * journal, a revoke record will be written.  And | 
|  | 3976 | * revoke records must be emitted *before* clearing | 
|  | 3977 | * this block's bit in the bitmaps. | 
|  | 3978 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3979 | ext4_forget(handle, 1, inode, bh, bh->b_blocknr); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3980 |  | 
|  | 3981 | /* | 
|  | 3982 | * Everything below this this pointer has been | 
|  | 3983 | * released.  Now let this top-of-subtree go. | 
|  | 3984 | * | 
|  | 3985 | * We want the freeing of this indirect block to be | 
|  | 3986 | * atomic in the journal with the updating of the | 
|  | 3987 | * bitmap block which owns it.  So make some room in | 
|  | 3988 | * the journal. | 
|  | 3989 | * | 
|  | 3990 | * We zero the parent pointer *after* freeing its | 
|  | 3991 | * pointee in the bitmaps, so if extend_transaction() | 
|  | 3992 | * for some reason fails to put the bitmap changes and | 
|  | 3993 | * the release into the same transaction, recovery | 
|  | 3994 | * will merely complain about releasing a free block, | 
|  | 3995 | * rather than leaking blocks. | 
|  | 3996 | */ | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3997 | if (ext4_handle_is_aborted(handle)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3998 | return; | 
|  | 3999 | if (try_to_extend_transaction(handle, inode)) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4000 | ext4_mark_inode_dirty(handle, inode); | 
|  | 4001 | ext4_journal_test_restart(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4002 | } | 
|  | 4003 |  | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4004 | ext4_free_blocks(handle, inode, nr, 1, 1); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4005 |  | 
|  | 4006 | if (parent_bh) { | 
|  | 4007 | /* | 
|  | 4008 | * The block which we have just freed is | 
|  | 4009 | * pointed to by an indirect block: journal it | 
|  | 4010 | */ | 
|  | 4011 | BUFFER_TRACE(parent_bh, "get_write_access"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4012 | if (!ext4_journal_get_write_access(handle, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4013 | parent_bh)){ | 
|  | 4014 | *p = 0; | 
|  | 4015 | BUFFER_TRACE(parent_bh, | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4016 | "call ext4_handle_dirty_metadata"); | 
|  | 4017 | ext4_handle_dirty_metadata(handle, | 
|  | 4018 | inode, | 
|  | 4019 | parent_bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4020 | } | 
|  | 4021 | } | 
|  | 4022 | } | 
|  | 4023 | } else { | 
|  | 4024 | /* We have reached the bottom of the tree. */ | 
|  | 4025 | BUFFER_TRACE(parent_bh, "free data blocks"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4026 | ext4_free_data(handle, inode, parent_bh, first, last); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4027 | } | 
|  | 4028 | } | 
|  | 4029 |  | 
| Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4030 | int ext4_can_truncate(struct inode *inode) | 
|  | 4031 | { | 
|  | 4032 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | 
|  | 4033 | return 0; | 
|  | 4034 | if (S_ISREG(inode->i_mode)) | 
|  | 4035 | return 1; | 
|  | 4036 | if (S_ISDIR(inode->i_mode)) | 
|  | 4037 | return 1; | 
|  | 4038 | if (S_ISLNK(inode->i_mode)) | 
|  | 4039 | return !ext4_inode_is_fast_symlink(inode); | 
|  | 4040 | return 0; | 
|  | 4041 | } | 
|  | 4042 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4043 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4044 | * ext4_truncate() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4045 | * | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4046 | * We block out ext4_get_block() block instantiations across the entire | 
|  | 4047 | * transaction, and VFS/VM ensures that ext4_truncate() cannot run | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4048 | * simultaneously on behalf of the same inode. | 
|  | 4049 | * | 
|  | 4050 | * As we work through the truncate and commmit bits of it to the journal there | 
|  | 4051 | * is one core, guiding principle: the file's tree must always be consistent on | 
|  | 4052 | * disk.  We must be able to restart the truncate after a crash. | 
|  | 4053 | * | 
|  | 4054 | * The file's tree may be transiently inconsistent in memory (although it | 
|  | 4055 | * probably isn't), but whenever we close off and commit a journal transaction, | 
|  | 4056 | * the contents of (the filesystem + the journal) must be consistent and | 
|  | 4057 | * restartable.  It's pretty simple, really: bottom up, right to left (although | 
|  | 4058 | * left-to-right works OK too). | 
|  | 4059 | * | 
|  | 4060 | * Note that at recovery time, journal replay occurs *before* the restart of | 
|  | 4061 | * truncate against the orphan inode list. | 
|  | 4062 | * | 
|  | 4063 | * 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] | 4064 | * 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] | 4065 | * 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] | 4066 | * ext4_truncate() to have another go.  So there will be instantiated blocks | 
|  | 4067 | * 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] | 4068 | * 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] | 4069 | * ext4_truncate() run will find them and release them. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4070 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4071 | void ext4_truncate(struct inode *inode) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4072 | { | 
|  | 4073 | handle_t *handle; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4074 | struct ext4_inode_info *ei = EXT4_I(inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4075 | __le32 *i_data = ei->i_data; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4076 | int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4077 | struct address_space *mapping = inode->i_mapping; | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4078 | ext4_lblk_t offsets[4]; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4079 | Indirect chain[4]; | 
|  | 4080 | Indirect *partial; | 
|  | 4081 | __le32 nr = 0; | 
|  | 4082 | int n; | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4083 | ext4_lblk_t last_block; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4084 | unsigned blocksize = inode->i_sb->s_blocksize; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4085 |  | 
| Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4086 | if (!ext4_can_truncate(inode)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4087 | return; | 
|  | 4088 |  | 
| Theodore Ts'o | 0eab928 | 2009-06-09 09:54:40 -0400 | [diff] [blame] | 4089 | if (ei->i_disksize && inode->i_size == 0 && | 
|  | 4090 | !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) | 
| Theodore Ts'o | 7d8f9f7 | 2009-02-24 08:21:14 -0500 | [diff] [blame] | 4091 | ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE; | 
|  | 4092 |  | 
| Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4093 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4094 | ext4_ext_truncate(inode); | 
| Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4095 | return; | 
|  | 4096 | } | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4097 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4098 | handle = start_transaction(inode); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4099 | if (IS_ERR(handle)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4100 | return;		/* AKPM: return what? */ | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4101 |  | 
|  | 4102 | last_block = (inode->i_size + blocksize-1) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4103 | >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4104 |  | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4105 | if (inode->i_size & (blocksize - 1)) | 
|  | 4106 | if (ext4_block_truncate_page(handle, mapping, inode->i_size)) | 
|  | 4107 | goto out_stop; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4108 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4109 | n = ext4_block_to_path(inode, last_block, offsets, NULL); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4110 | if (n == 0) | 
|  | 4111 | goto out_stop;	/* error */ | 
|  | 4112 |  | 
|  | 4113 | /* | 
|  | 4114 | * OK.  This truncate is going to happen.  We add the inode to the | 
|  | 4115 | * orphan list, so that if this truncate spans multiple transactions, | 
|  | 4116 | * and we crash, we will resume the truncate when the filesystem | 
|  | 4117 | * recovers.  It also marks the inode dirty, to catch the new size. | 
|  | 4118 | * | 
|  | 4119 | * Implication: the file must always be in a sane, consistent | 
|  | 4120 | * truncatable state while each transaction commits. | 
|  | 4121 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4122 | if (ext4_orphan_add(handle, inode)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4123 | goto out_stop; | 
|  | 4124 |  | 
|  | 4125 | /* | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4126 | * From here we block out all ext4_get_block() callers who want to | 
|  | 4127 | * modify the block allocation tree. | 
|  | 4128 | */ | 
|  | 4129 | down_write(&ei->i_data_sem); | 
| Theodore Ts'o | b4df203 | 2008-08-13 21:44:34 -0400 | [diff] [blame] | 4130 |  | 
| Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4131 | ext4_discard_preallocations(inode); | 
| Theodore Ts'o | b4df203 | 2008-08-13 21:44:34 -0400 | [diff] [blame] | 4132 |  | 
| Mingming Cao | 632eaea | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4133 | /* | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4134 | * The orphan list entry will now protect us from any crash which | 
|  | 4135 | * occurs before the truncate completes, so it is now safe to propagate | 
|  | 4136 | * the new, shorter inode size (held for now in i_size) into the | 
|  | 4137 | * 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] | 4138 | * ext4 *really* writes onto the disk inode. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4139 | */ | 
|  | 4140 | ei->i_disksize = inode->i_size; | 
|  | 4141 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4142 | if (n == 1) {		/* direct blocks */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4143 | ext4_free_data(handle, inode, NULL, i_data+offsets[0], | 
|  | 4144 | i_data + EXT4_NDIR_BLOCKS); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4145 | goto do_indirects; | 
|  | 4146 | } | 
|  | 4147 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4148 | partial = ext4_find_shared(inode, n, offsets, chain, &nr); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4149 | /* Kill the top of shared branch (not detached) */ | 
|  | 4150 | if (nr) { | 
|  | 4151 | if (partial == chain) { | 
|  | 4152 | /* Shared branch grows from the inode */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4153 | ext4_free_branches(handle, inode, NULL, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4154 | &nr, &nr+1, (chain+n-1) - partial); | 
|  | 4155 | *partial->p = 0; | 
|  | 4156 | /* | 
|  | 4157 | * We mark the inode dirty prior to restart, | 
|  | 4158 | * and prior to stop.  No need for it here. | 
|  | 4159 | */ | 
|  | 4160 | } else { | 
|  | 4161 | /* Shared branch grows from an indirect block */ | 
|  | 4162 | BUFFER_TRACE(partial->bh, "get_write_access"); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4163 | ext4_free_branches(handle, inode, partial->bh, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4164 | partial->p, | 
|  | 4165 | partial->p+1, (chain+n-1) - partial); | 
|  | 4166 | } | 
|  | 4167 | } | 
|  | 4168 | /* Clear the ends of indirect blocks on the shared branch */ | 
|  | 4169 | while (partial > chain) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4170 | ext4_free_branches(handle, inode, partial->bh, partial->p + 1, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4171 | (__le32*)partial->bh->b_data+addr_per_block, | 
|  | 4172 | (chain+n-1) - partial); | 
|  | 4173 | BUFFER_TRACE(partial->bh, "call brelse"); | 
|  | 4174 | brelse (partial->bh); | 
|  | 4175 | partial--; | 
|  | 4176 | } | 
|  | 4177 | do_indirects: | 
|  | 4178 | /* Kill the remaining (whole) subtrees */ | 
|  | 4179 | switch (offsets[0]) { | 
|  | 4180 | default: | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4181 | nr = i_data[EXT4_IND_BLOCK]; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4182 | if (nr) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4183 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); | 
|  | 4184 | i_data[EXT4_IND_BLOCK] = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4185 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4186 | case EXT4_IND_BLOCK: | 
|  | 4187 | nr = i_data[EXT4_DIND_BLOCK]; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4188 | if (nr) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4189 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); | 
|  | 4190 | i_data[EXT4_DIND_BLOCK] = 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4191 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4192 | case EXT4_DIND_BLOCK: | 
|  | 4193 | nr = i_data[EXT4_TIND_BLOCK]; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4194 | if (nr) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4195 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); | 
|  | 4196 | i_data[EXT4_TIND_BLOCK] = 0; | 
| 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 | case EXT4_TIND_BLOCK: | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4199 | ; | 
|  | 4200 | } | 
|  | 4201 |  | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4202 | up_write(&ei->i_data_sem); | 
| Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 4203 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4204 | ext4_mark_inode_dirty(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4205 |  | 
|  | 4206 | /* | 
|  | 4207 | * In a multi-transaction truncate, we only make the final transaction | 
|  | 4208 | * synchronous | 
|  | 4209 | */ | 
|  | 4210 | if (IS_SYNC(inode)) | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4211 | ext4_handle_sync(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4212 | out_stop: | 
|  | 4213 | /* | 
|  | 4214 | * If this was a simple ftruncate(), and the file will remain alive | 
|  | 4215 | * then we need to clear up the orphan record which we created above. | 
|  | 4216 | * However, if this was a real unlink then we were called by | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4217 | * ext4_delete_inode(), and we allow that function to clean up the | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4218 | * orphan info for us. | 
|  | 4219 | */ | 
|  | 4220 | if (inode->i_nlink) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4221 | ext4_orphan_del(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4222 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4223 | ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4224 | } | 
|  | 4225 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4226 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4227 | * 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] | 4228 | * underlying buffer_head on success. If 'in_mem' is true, we have all | 
|  | 4229 | * data in memory that is needed to recreate the on-disk version of this | 
|  | 4230 | * inode. | 
|  | 4231 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4232 | static int __ext4_get_inode_loc(struct inode *inode, | 
|  | 4233 | struct ext4_iloc *iloc, int in_mem) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4234 | { | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4235 | struct ext4_group_desc	*gdp; | 
|  | 4236 | struct buffer_head	*bh; | 
|  | 4237 | struct super_block	*sb = inode->i_sb; | 
|  | 4238 | ext4_fsblk_t		block; | 
|  | 4239 | int			inodes_per_block, inode_offset; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4240 |  | 
| Aneesh Kumar K.V | 3a06d77 | 2008-11-22 15:04:59 -0500 | [diff] [blame] | 4241 | iloc->bh = NULL; | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4242 | if (!ext4_valid_inum(sb, inode->i_ino)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4243 | return -EIO; | 
|  | 4244 |  | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4245 | iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); | 
|  | 4246 | gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); | 
|  | 4247 | if (!gdp) | 
|  | 4248 | return -EIO; | 
|  | 4249 |  | 
|  | 4250 | /* | 
|  | 4251 | * Figure out the offset within the block group inode table | 
|  | 4252 | */ | 
|  | 4253 | inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb)); | 
|  | 4254 | inode_offset = ((inode->i_ino - 1) % | 
|  | 4255 | EXT4_INODES_PER_GROUP(sb)); | 
|  | 4256 | block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); | 
|  | 4257 | iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); | 
|  | 4258 |  | 
|  | 4259 | bh = sb_getblk(sb, block); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4260 | if (!bh) { | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4261 | ext4_error(sb, "ext4_get_inode_loc", "unable to read " | 
|  | 4262 | "inode block - inode=%lu, block=%llu", | 
|  | 4263 | inode->i_ino, block); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4264 | return -EIO; | 
|  | 4265 | } | 
|  | 4266 | if (!buffer_uptodate(bh)) { | 
|  | 4267 | lock_buffer(bh); | 
| Hidehiro Kawai | 9c83a92 | 2008-07-26 16:39:26 -0400 | [diff] [blame] | 4268 |  | 
|  | 4269 | /* | 
|  | 4270 | * If the buffer has the write error flag, we have failed | 
|  | 4271 | * to write out another inode in the same block.  In this | 
|  | 4272 | * case, we don't have to read the block because we may | 
|  | 4273 | * read the old inode data successfully. | 
|  | 4274 | */ | 
|  | 4275 | if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) | 
|  | 4276 | set_buffer_uptodate(bh); | 
|  | 4277 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4278 | if (buffer_uptodate(bh)) { | 
|  | 4279 | /* someone brought it uptodate while we waited */ | 
|  | 4280 | unlock_buffer(bh); | 
|  | 4281 | goto has_buffer; | 
|  | 4282 | } | 
|  | 4283 |  | 
|  | 4284 | /* | 
|  | 4285 | * If we have all information of the inode in memory and this | 
|  | 4286 | * is the only valid inode in the block, we need not read the | 
|  | 4287 | * block. | 
|  | 4288 | */ | 
|  | 4289 | if (in_mem) { | 
|  | 4290 | struct buffer_head *bitmap_bh; | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4291 | int i, start; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4292 |  | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4293 | start = inode_offset & ~(inodes_per_block - 1); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4294 |  | 
|  | 4295 | /* Is the inode bitmap in cache? */ | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4296 | bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4297 | if (!bitmap_bh) | 
|  | 4298 | goto make_io; | 
|  | 4299 |  | 
|  | 4300 | /* | 
|  | 4301 | * If the inode bitmap isn't in cache then the | 
|  | 4302 | * optimisation may end up performing two reads instead | 
|  | 4303 | * of one, so skip it. | 
|  | 4304 | */ | 
|  | 4305 | if (!buffer_uptodate(bitmap_bh)) { | 
|  | 4306 | brelse(bitmap_bh); | 
|  | 4307 | goto make_io; | 
|  | 4308 | } | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4309 | for (i = start; i < start + inodes_per_block; i++) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4310 | if (i == inode_offset) | 
|  | 4311 | continue; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4312 | if (ext4_test_bit(i, bitmap_bh->b_data)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4313 | break; | 
|  | 4314 | } | 
|  | 4315 | brelse(bitmap_bh); | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4316 | if (i == start + inodes_per_block) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4317 | /* all other inodes are free, so skip I/O */ | 
|  | 4318 | memset(bh->b_data, 0, bh->b_size); | 
|  | 4319 | set_buffer_uptodate(bh); | 
|  | 4320 | unlock_buffer(bh); | 
|  | 4321 | goto has_buffer; | 
|  | 4322 | } | 
|  | 4323 | } | 
|  | 4324 |  | 
|  | 4325 | make_io: | 
|  | 4326 | /* | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4327 | * If we need to do any I/O, try to pre-readahead extra | 
|  | 4328 | * blocks from the inode table. | 
|  | 4329 | */ | 
|  | 4330 | if (EXT4_SB(sb)->s_inode_readahead_blks) { | 
|  | 4331 | ext4_fsblk_t b, end, table; | 
|  | 4332 | unsigned num; | 
|  | 4333 |  | 
|  | 4334 | table = ext4_inode_table(sb, gdp); | 
| Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 4335 | /* s_inode_readahead_blks is always a power of 2 */ | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4336 | b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); | 
|  | 4337 | if (table > b) | 
|  | 4338 | b = table; | 
|  | 4339 | end = b + EXT4_SB(sb)->s_inode_readahead_blks; | 
|  | 4340 | num = EXT4_INODES_PER_GROUP(sb); | 
|  | 4341 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | 
|  | 4342 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) | 
| Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 4343 | num -= ext4_itable_unused_count(sb, gdp); | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4344 | table += num / inodes_per_block; | 
|  | 4345 | if (end > table) | 
|  | 4346 | end = table; | 
|  | 4347 | while (b <= end) | 
|  | 4348 | sb_breadahead(sb, b++); | 
|  | 4349 | } | 
|  | 4350 |  | 
|  | 4351 | /* | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4352 | * There are other valid inodes in the buffer, this inode | 
|  | 4353 | * has in-inode xattrs, or we don't have this inode in memory. | 
|  | 4354 | * Read the block from disk. | 
|  | 4355 | */ | 
|  | 4356 | get_bh(bh); | 
|  | 4357 | bh->b_end_io = end_buffer_read_sync; | 
|  | 4358 | submit_bh(READ_META, bh); | 
|  | 4359 | wait_on_buffer(bh); | 
|  | 4360 | if (!buffer_uptodate(bh)) { | 
| Theodore Ts'o | 240799c | 2008-10-09 23:53:47 -0400 | [diff] [blame] | 4361 | ext4_error(sb, __func__, | 
|  | 4362 | "unable to read inode block - inode=%lu, " | 
|  | 4363 | "block=%llu", inode->i_ino, block); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4364 | brelse(bh); | 
|  | 4365 | return -EIO; | 
|  | 4366 | } | 
|  | 4367 | } | 
|  | 4368 | has_buffer: | 
|  | 4369 | iloc->bh = bh; | 
|  | 4370 | return 0; | 
|  | 4371 | } | 
|  | 4372 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4373 | int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4374 | { | 
|  | 4375 | /* We have all inode data except xattrs in memory here. */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4376 | return __ext4_get_inode_loc(inode, iloc, | 
|  | 4377 | !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR)); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4378 | } | 
|  | 4379 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4380 | void ext4_set_inode_flags(struct inode *inode) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4381 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4382 | unsigned int flags = EXT4_I(inode)->i_flags; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4383 |  | 
|  | 4384 | 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] | 4385 | if (flags & EXT4_SYNC_FL) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4386 | inode->i_flags |= S_SYNC; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4387 | if (flags & EXT4_APPEND_FL) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4388 | inode->i_flags |= S_APPEND; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4389 | if (flags & EXT4_IMMUTABLE_FL) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4390 | inode->i_flags |= S_IMMUTABLE; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4391 | if (flags & EXT4_NOATIME_FL) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4392 | inode->i_flags |= S_NOATIME; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4393 | if (flags & EXT4_DIRSYNC_FL) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4394 | inode->i_flags |= S_DIRSYNC; | 
|  | 4395 | } | 
|  | 4396 |  | 
| Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 4397 | /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ | 
|  | 4398 | void ext4_get_inode_flags(struct ext4_inode_info *ei) | 
|  | 4399 | { | 
|  | 4400 | unsigned int flags = ei->vfs_inode.i_flags; | 
|  | 4401 |  | 
|  | 4402 | ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL| | 
|  | 4403 | EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL); | 
|  | 4404 | if (flags & S_SYNC) | 
|  | 4405 | ei->i_flags |= EXT4_SYNC_FL; | 
|  | 4406 | if (flags & S_APPEND) | 
|  | 4407 | ei->i_flags |= EXT4_APPEND_FL; | 
|  | 4408 | if (flags & S_IMMUTABLE) | 
|  | 4409 | ei->i_flags |= EXT4_IMMUTABLE_FL; | 
|  | 4410 | if (flags & S_NOATIME) | 
|  | 4411 | ei->i_flags |= EXT4_NOATIME_FL; | 
|  | 4412 | if (flags & S_DIRSYNC) | 
|  | 4413 | ei->i_flags |= EXT4_DIRSYNC_FL; | 
|  | 4414 | } | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4415 | static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, | 
|  | 4416 | struct ext4_inode_info *ei) | 
|  | 4417 | { | 
|  | 4418 | blkcnt_t i_blocks ; | 
| Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4419 | struct inode *inode = &(ei->vfs_inode); | 
|  | 4420 | struct super_block *sb = inode->i_sb; | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4421 |  | 
|  | 4422 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | 
|  | 4423 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { | 
|  | 4424 | /* we are using combined 48 bit field */ | 
|  | 4425 | i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | | 
|  | 4426 | le32_to_cpu(raw_inode->i_blocks_lo); | 
| Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4427 | if (ei->i_flags & EXT4_HUGE_FILE_FL) { | 
|  | 4428 | /* i_blocks represent file system block size */ | 
|  | 4429 | return i_blocks  << (inode->i_blkbits - 9); | 
|  | 4430 | } else { | 
|  | 4431 | return i_blocks; | 
|  | 4432 | } | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4433 | } else { | 
|  | 4434 | return le32_to_cpu(raw_inode->i_blocks_lo); | 
|  | 4435 | } | 
|  | 4436 | } | 
| Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 4437 |  | 
| David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4438 | struct inode *ext4_iget(struct super_block *sb, unsigned long ino) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4439 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4440 | struct ext4_iloc iloc; | 
|  | 4441 | struct ext4_inode *raw_inode; | 
| David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4442 | struct ext4_inode_info *ei; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4443 | struct buffer_head *bh; | 
| David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4444 | struct inode *inode; | 
|  | 4445 | long ret; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4446 | int block; | 
|  | 4447 |  | 
| David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4448 | inode = iget_locked(sb, ino); | 
|  | 4449 | if (!inode) | 
|  | 4450 | return ERR_PTR(-ENOMEM); | 
|  | 4451 | if (!(inode->i_state & I_NEW)) | 
|  | 4452 | return inode; | 
|  | 4453 |  | 
|  | 4454 | ei = EXT4_I(inode); | 
| Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 4455 | #ifdef CONFIG_EXT4_FS_POSIX_ACL | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4456 | ei->i_acl = EXT4_ACL_NOT_CACHED; | 
|  | 4457 | ei->i_default_acl = EXT4_ACL_NOT_CACHED; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4458 | #endif | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4459 |  | 
| David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4460 | ret = __ext4_get_inode_loc(inode, &iloc, 0); | 
|  | 4461 | if (ret < 0) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4462 | goto bad_inode; | 
|  | 4463 | bh = iloc.bh; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4464 | raw_inode = ext4_raw_inode(&iloc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4465 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); | 
|  | 4466 | inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); | 
|  | 4467 | 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] | 4468 | if (!(test_opt(inode->i_sb, NO_UID32))) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4469 | inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; | 
|  | 4470 | inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; | 
|  | 4471 | } | 
|  | 4472 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4473 |  | 
|  | 4474 | ei->i_state = 0; | 
|  | 4475 | ei->i_dir_start_lookup = 0; | 
|  | 4476 | ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); | 
|  | 4477 | /* We now have enough fields to check if the inode was active or not. | 
|  | 4478 | * This is needed because nfsd might try to access dead inodes | 
|  | 4479 | * the test is that same one that e2fsck uses | 
|  | 4480 | * NeilBrown 1999oct15 | 
|  | 4481 | */ | 
|  | 4482 | if (inode->i_nlink == 0) { | 
|  | 4483 | if (inode->i_mode == 0 || | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4484 | !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4485 | /* this inode is deleted */ | 
| 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 = -ESTALE; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4488 | goto bad_inode; | 
|  | 4489 | } | 
|  | 4490 | /* The only unlinked inodes we let through here have | 
|  | 4491 | * valid i_mode and are being read by the orphan | 
|  | 4492 | * recovery code: that's fine, we're about to complete | 
|  | 4493 | * the process of deleting those. */ | 
|  | 4494 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4495 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4496 | inode->i_blocks = ext4_inode_blocks(raw_inode, ei); | 
| Aneesh Kumar K.V | 7973c0c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4497 | 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] | 4498 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) | 
| Badari Pulavarty | a1ddeb7 | 2006-10-11 01:21:09 -0700 | [diff] [blame] | 4499 | ei->i_file_acl |= | 
|  | 4500 | ((__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] | 4501 | inode->i_size = ext4_isize(raw_inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4502 | ei->i_disksize = inode->i_size; | 
|  | 4503 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); | 
|  | 4504 | ei->i_block_group = iloc.block_group; | 
| Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 4505 | ei->i_last_alloc_group = ~0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4506 | /* | 
|  | 4507 | * NOTE! The in-memory inode i_data array is in little-endian order | 
|  | 4508 | * even on big-endian machines: we do NOT byteswap the block numbers! | 
|  | 4509 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4510 | for (block = 0; block < EXT4_N_BLOCKS; block++) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4511 | ei->i_data[block] = raw_inode->i_block[block]; | 
|  | 4512 | INIT_LIST_HEAD(&ei->i_orphan); | 
|  | 4513 |  | 
| Eric Sandeen | 0040d98 | 2008-02-05 22:36:43 -0500 | [diff] [blame] | 4514 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4515 | ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4516 | if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > | 
| Kirill Korotaev | e5d2861 | 2007-06-23 17:16:51 -0700 | [diff] [blame] | 4517 | EXT4_INODE_SIZE(inode->i_sb)) { | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4518 | brelse(bh); | 
| David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4519 | ret = -EIO; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4520 | goto bad_inode; | 
| Kirill Korotaev | e5d2861 | 2007-06-23 17:16:51 -0700 | [diff] [blame] | 4521 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4522 | if (ei->i_extra_isize == 0) { | 
|  | 4523 | /* The extra space is currently unused. Use it. */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4524 | ei->i_extra_isize = sizeof(struct ext4_inode) - | 
|  | 4525 | EXT4_GOOD_OLD_INODE_SIZE; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4526 | } else { | 
|  | 4527 | __le32 *magic = (void *)raw_inode + | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4528 | EXT4_GOOD_OLD_INODE_SIZE + | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4529 | ei->i_extra_isize; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4530 | if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) | 
|  | 4531 | ei->i_state |= EXT4_STATE_XATTR; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4532 | } | 
|  | 4533 | } else | 
|  | 4534 | ei->i_extra_isize = 0; | 
|  | 4535 |  | 
| Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 4536 | EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); | 
|  | 4537 | EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); | 
|  | 4538 | EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); | 
|  | 4539 | EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); | 
|  | 4540 |  | 
| Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4541 | inode->i_version = le32_to_cpu(raw_inode->i_disk_version); | 
|  | 4542 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { | 
|  | 4543 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) | 
|  | 4544 | inode->i_version |= | 
|  | 4545 | (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; | 
|  | 4546 | } | 
|  | 4547 |  | 
| Theodore Ts'o | c4b5a61 | 2009-04-24 18:45:35 -0400 | [diff] [blame] | 4548 | ret = 0; | 
| Theodore Ts'o | 485c26e | 2009-04-24 13:43:20 -0400 | [diff] [blame] | 4549 | if (ei->i_file_acl && | 
|  | 4550 | ((ei->i_file_acl < | 
|  | 4551 | (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) + | 
|  | 4552 | EXT4_SB(sb)->s_gdb_count)) || | 
|  | 4553 | (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) { | 
|  | 4554 | ext4_error(sb, __func__, | 
|  | 4555 | "bad extended attribute block %llu in inode #%lu", | 
|  | 4556 | ei->i_file_acl, inode->i_ino); | 
|  | 4557 | ret = -EIO; | 
|  | 4558 | goto bad_inode; | 
|  | 4559 | } else if (ei->i_flags & EXT4_EXTENTS_FL) { | 
| Theodore Ts'o | c4b5a61 | 2009-04-24 18:45:35 -0400 | [diff] [blame] | 4560 | if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | 
|  | 4561 | (S_ISLNK(inode->i_mode) && | 
|  | 4562 | !ext4_inode_is_fast_symlink(inode))) | 
|  | 4563 | /* Validate extent which is part of inode */ | 
|  | 4564 | ret = ext4_ext_check_inode(inode); | 
| Thiemo Nagel | fe2c819 | 2009-03-31 08:36:10 -0400 | [diff] [blame] | 4565 | } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | 
|  | 4566 | (S_ISLNK(inode->i_mode) && | 
|  | 4567 | !ext4_inode_is_fast_symlink(inode))) { | 
|  | 4568 | /* Validate block references which are part of inode */ | 
|  | 4569 | ret = ext4_check_inode_blockref(inode); | 
|  | 4570 | } | 
|  | 4571 | if (ret) { | 
|  | 4572 | brelse(bh); | 
|  | 4573 | goto bad_inode; | 
| Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 4574 | } | 
|  | 4575 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4576 | if (S_ISREG(inode->i_mode)) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4577 | inode->i_op = &ext4_file_inode_operations; | 
|  | 4578 | inode->i_fop = &ext4_file_operations; | 
|  | 4579 | ext4_set_aops(inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4580 | } else if (S_ISDIR(inode->i_mode)) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4581 | inode->i_op = &ext4_dir_inode_operations; | 
|  | 4582 | inode->i_fop = &ext4_dir_operations; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4583 | } else if (S_ISLNK(inode->i_mode)) { | 
| Duane Griffin | e83c139 | 2008-12-19 20:47:15 +0000 | [diff] [blame] | 4584 | if (ext4_inode_is_fast_symlink(inode)) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4585 | inode->i_op = &ext4_fast_symlink_inode_operations; | 
| Duane Griffin | e83c139 | 2008-12-19 20:47:15 +0000 | [diff] [blame] | 4586 | nd_terminate_link(ei->i_data, inode->i_size, | 
|  | 4587 | sizeof(ei->i_data) - 1); | 
|  | 4588 | } else { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4589 | inode->i_op = &ext4_symlink_inode_operations; | 
|  | 4590 | ext4_set_aops(inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4591 | } | 
| Theodore Ts'o | 563bdd6 | 2009-03-26 00:06:19 -0400 | [diff] [blame] | 4592 | } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || | 
|  | 4593 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4594 | inode->i_op = &ext4_special_inode_operations; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4595 | if (raw_inode->i_block[0]) | 
|  | 4596 | init_special_inode(inode, inode->i_mode, | 
|  | 4597 | old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); | 
|  | 4598 | else | 
|  | 4599 | init_special_inode(inode, inode->i_mode, | 
|  | 4600 | new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); | 
| Theodore Ts'o | 563bdd6 | 2009-03-26 00:06:19 -0400 | [diff] [blame] | 4601 | } else { | 
|  | 4602 | brelse(bh); | 
|  | 4603 | ret = -EIO; | 
|  | 4604 | ext4_error(inode->i_sb, __func__, | 
|  | 4605 | "bogus i_mode (%o) for inode=%lu", | 
|  | 4606 | inode->i_mode, inode->i_ino); | 
|  | 4607 | goto bad_inode; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4608 | } | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4609 | brelse(iloc.bh); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4610 | ext4_set_inode_flags(inode); | 
| David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4611 | unlock_new_inode(inode); | 
|  | 4612 | return inode; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4613 |  | 
|  | 4614 | bad_inode: | 
| David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 4615 | iget_failed(inode); | 
|  | 4616 | return ERR_PTR(ret); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4617 | } | 
|  | 4618 |  | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4619 | static int ext4_inode_blocks_set(handle_t *handle, | 
|  | 4620 | struct ext4_inode *raw_inode, | 
|  | 4621 | struct ext4_inode_info *ei) | 
|  | 4622 | { | 
|  | 4623 | struct inode *inode = &(ei->vfs_inode); | 
|  | 4624 | u64 i_blocks = inode->i_blocks; | 
|  | 4625 | struct super_block *sb = inode->i_sb; | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4626 |  | 
|  | 4627 | if (i_blocks <= ~0U) { | 
|  | 4628 | /* | 
|  | 4629 | * i_blocks can be represnted in a 32 bit variable | 
|  | 4630 | * as multiple of 512 bytes | 
|  | 4631 | */ | 
| Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4632 | raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks); | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4633 | raw_inode->i_blocks_high = 0; | 
| Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4634 | ei->i_flags &= ~EXT4_HUGE_FILE_FL; | 
| Theodore Ts'o | f287a1a | 2008-10-16 22:50:48 -0400 | [diff] [blame] | 4635 | return 0; | 
|  | 4636 | } | 
|  | 4637 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) | 
|  | 4638 | return -EFBIG; | 
|  | 4639 |  | 
|  | 4640 | if (i_blocks <= 0xffffffffffffULL) { | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4641 | /* | 
|  | 4642 | * i_blocks can be represented in a 48 bit variable | 
|  | 4643 | * as multiple of 512 bytes | 
|  | 4644 | */ | 
| Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4645 | raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks); | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4646 | 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] | 4647 | ei->i_flags &= ~EXT4_HUGE_FILE_FL; | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4648 | } else { | 
| Aneesh Kumar K.V | 8180a56 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4649 | ei->i_flags |= EXT4_HUGE_FILE_FL; | 
|  | 4650 | /* i_block is stored in file system block size */ | 
|  | 4651 | i_blocks = i_blocks >> (inode->i_blkbits - 9); | 
|  | 4652 | raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks); | 
|  | 4653 | 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] | 4654 | } | 
| Theodore Ts'o | f287a1a | 2008-10-16 22:50:48 -0400 | [diff] [blame] | 4655 | return 0; | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4656 | } | 
|  | 4657 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4658 | /* | 
|  | 4659 | * Post the struct inode info into an on-disk inode location in the | 
|  | 4660 | * buffer-cache.  This gobbles the caller's reference to the | 
|  | 4661 | * buffer_head in the inode location struct. | 
|  | 4662 | * | 
|  | 4663 | * The caller must have write access to iloc->bh. | 
|  | 4664 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4665 | static int ext4_do_update_inode(handle_t *handle, | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4666 | struct inode *inode, | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4667 | struct ext4_iloc *iloc) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4668 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4669 | struct ext4_inode *raw_inode = ext4_raw_inode(iloc); | 
|  | 4670 | struct ext4_inode_info *ei = EXT4_I(inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4671 | struct buffer_head *bh = iloc->bh; | 
|  | 4672 | int err = 0, rc, block; | 
|  | 4673 |  | 
|  | 4674 | /* For fields not not tracking in the in-memory inode, | 
|  | 4675 | * initialise them to zero for new inodes. */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4676 | if (ei->i_state & EXT4_STATE_NEW) | 
|  | 4677 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4678 |  | 
| Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 4679 | ext4_get_inode_flags(ei); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4680 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4681 | if (!(test_opt(inode->i_sb, NO_UID32))) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4682 | raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); | 
|  | 4683 | raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); | 
|  | 4684 | /* | 
|  | 4685 | * Fix up interoperability with old kernels. Otherwise, old inodes get | 
|  | 4686 | * re-used with the upper 16 bits of the uid/gid intact | 
|  | 4687 | */ | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4688 | if (!ei->i_dtime) { | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4689 | raw_inode->i_uid_high = | 
|  | 4690 | cpu_to_le16(high_16_bits(inode->i_uid)); | 
|  | 4691 | raw_inode->i_gid_high = | 
|  | 4692 | cpu_to_le16(high_16_bits(inode->i_gid)); | 
|  | 4693 | } else { | 
|  | 4694 | raw_inode->i_uid_high = 0; | 
|  | 4695 | raw_inode->i_gid_high = 0; | 
|  | 4696 | } | 
|  | 4697 | } else { | 
|  | 4698 | raw_inode->i_uid_low = | 
|  | 4699 | cpu_to_le16(fs_high2lowuid(inode->i_uid)); | 
|  | 4700 | raw_inode->i_gid_low = | 
|  | 4701 | cpu_to_le16(fs_high2lowgid(inode->i_gid)); | 
|  | 4702 | raw_inode->i_uid_high = 0; | 
|  | 4703 | raw_inode->i_gid_high = 0; | 
|  | 4704 | } | 
|  | 4705 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); | 
| Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 4706 |  | 
|  | 4707 | EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); | 
|  | 4708 | EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); | 
|  | 4709 | EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); | 
|  | 4710 | EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); | 
|  | 4711 |  | 
| Aneesh Kumar K.V | 0fc1b45 | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4712 | if (ext4_inode_blocks_set(handle, raw_inode, ei)) | 
|  | 4713 | goto out_brelse; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4714 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); | 
| Aneesh Kumar K.V | 267e4db | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4715 | /* clear the migrate flag in the raw_inode */ | 
|  | 4716 | raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE); | 
| Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 4717 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != | 
|  | 4718 | cpu_to_le32(EXT4_OS_HURD)) | 
| Badari Pulavarty | a1ddeb7 | 2006-10-11 01:21:09 -0700 | [diff] [blame] | 4719 | raw_inode->i_file_acl_high = | 
|  | 4720 | cpu_to_le16(ei->i_file_acl >> 32); | 
| Aneesh Kumar K.V | 7973c0c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4721 | 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] | 4722 | ext4_isize_set(raw_inode, ei->i_disksize); | 
|  | 4723 | if (ei->i_disksize > 0x7fffffffULL) { | 
|  | 4724 | struct super_block *sb = inode->i_sb; | 
|  | 4725 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, | 
|  | 4726 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || | 
|  | 4727 | EXT4_SB(sb)->s_es->s_rev_level == | 
|  | 4728 | cpu_to_le32(EXT4_GOOD_OLD_REV)) { | 
|  | 4729 | /* If this is the first large file | 
|  | 4730 | * created, add a flag to the superblock. | 
|  | 4731 | */ | 
|  | 4732 | err = ext4_journal_get_write_access(handle, | 
|  | 4733 | EXT4_SB(sb)->s_sbh); | 
|  | 4734 | if (err) | 
|  | 4735 | goto out_brelse; | 
|  | 4736 | ext4_update_dynamic_rev(sb); | 
|  | 4737 | EXT4_SET_RO_COMPAT_FEATURE(sb, | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4738 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE); | 
| Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4739 | sb->s_dirt = 1; | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4740 | ext4_handle_sync(handle); | 
|  | 4741 | err = ext4_handle_dirty_metadata(handle, inode, | 
| Aneesh Kumar K.V | a48380f | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4742 | EXT4_SB(sb)->s_sbh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4743 | } | 
|  | 4744 | } | 
|  | 4745 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); | 
|  | 4746 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | 
|  | 4747 | if (old_valid_dev(inode->i_rdev)) { | 
|  | 4748 | raw_inode->i_block[0] = | 
|  | 4749 | cpu_to_le32(old_encode_dev(inode->i_rdev)); | 
|  | 4750 | raw_inode->i_block[1] = 0; | 
|  | 4751 | } else { | 
|  | 4752 | raw_inode->i_block[0] = 0; | 
|  | 4753 | raw_inode->i_block[1] = | 
|  | 4754 | cpu_to_le32(new_encode_dev(inode->i_rdev)); | 
|  | 4755 | raw_inode->i_block[2] = 0; | 
|  | 4756 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4757 | } else for (block = 0; block < EXT4_N_BLOCKS; block++) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4758 | raw_inode->i_block[block] = ei->i_data[block]; | 
|  | 4759 |  | 
| Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4760 | raw_inode->i_disk_version = cpu_to_le32(inode->i_version); | 
|  | 4761 | if (ei->i_extra_isize) { | 
|  | 4762 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) | 
|  | 4763 | raw_inode->i_version_hi = | 
|  | 4764 | cpu_to_le32(inode->i_version >> 32); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4765 | 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] | 4766 | } | 
|  | 4767 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4768 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); | 
|  | 4769 | rc = ext4_handle_dirty_metadata(handle, inode, bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4770 | if (!err) | 
|  | 4771 | err = rc; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4772 | ei->i_state &= ~EXT4_STATE_NEW; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4773 |  | 
|  | 4774 | out_brelse: | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 4775 | brelse(bh); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4776 | ext4_std_error(inode->i_sb, err); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4777 | return err; | 
|  | 4778 | } | 
|  | 4779 |  | 
|  | 4780 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4781 | * ext4_write_inode() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4782 | * | 
|  | 4783 | * We are called from a few places: | 
|  | 4784 | * | 
|  | 4785 | * - Within generic_file_write() for O_SYNC files. | 
|  | 4786 | *   Here, there will be no transaction running. We wait for any running | 
|  | 4787 | *   trasnaction to commit. | 
|  | 4788 | * | 
|  | 4789 | * - Within sys_sync(), kupdate and such. | 
|  | 4790 | *   We wait on commit, if tol to. | 
|  | 4791 | * | 
|  | 4792 | * - Within prune_icache() (PF_MEMALLOC == true) | 
|  | 4793 | *   Here we simply return.  We can't afford to block kswapd on the | 
|  | 4794 | *   journal commit. | 
|  | 4795 | * | 
|  | 4796 | * In all cases it is actually safe for us to return without doing anything, | 
|  | 4797 | * because the inode has been copied into a raw inode buffer in | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4798 | * 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] | 4799 | * knfsd. | 
|  | 4800 | * | 
|  | 4801 | * Note that we are absolutely dependent upon all inode dirtiers doing the | 
|  | 4802 | * right thing: they *must* call mark_inode_dirty() after dirtying info in | 
|  | 4803 | * which we are interested. | 
|  | 4804 | * | 
|  | 4805 | * It would be a bug for them to not do this.  The code: | 
|  | 4806 | * | 
|  | 4807 | *	mark_inode_dirty(inode) | 
|  | 4808 | *	stuff(); | 
|  | 4809 | *	inode->i_size = expr; | 
|  | 4810 | * | 
|  | 4811 | * is in error because a kswapd-driven write_inode() could occur while | 
|  | 4812 | * `stuff()' is running, and the new i_size will be lost.  Plus the inode | 
|  | 4813 | * will no longer be on the superblock's dirty inode list. | 
|  | 4814 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4815 | int ext4_write_inode(struct inode *inode, int wait) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4816 | { | 
|  | 4817 | if (current->flags & PF_MEMALLOC) | 
|  | 4818 | return 0; | 
|  | 4819 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4820 | if (ext4_journal_current_handle()) { | 
| Mingming Cao | b38bd33 | 2007-07-19 01:48:35 -0700 | [diff] [blame] | 4821 | jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4822 | dump_stack(); | 
|  | 4823 | return -EIO; | 
|  | 4824 | } | 
|  | 4825 |  | 
|  | 4826 | if (!wait) | 
|  | 4827 | return 0; | 
|  | 4828 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4829 | return ext4_force_commit(inode->i_sb); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4830 | } | 
|  | 4831 |  | 
|  | 4832 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4833 | * ext4_setattr() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4834 | * | 
|  | 4835 | * Called from notify_change. | 
|  | 4836 | * | 
|  | 4837 | * We want to trap VFS attempts to truncate the file as soon as | 
|  | 4838 | * possible.  In particular, we want to make sure that when the VFS | 
|  | 4839 | * shrinks i_size, we put the inode on the orphan list and modify | 
|  | 4840 | * i_disksize immediately, so that during the subsequent flushing of | 
|  | 4841 | * dirty pages and freeing of disk blocks, we can guarantee that any | 
|  | 4842 | * commit will leave the blocks being flushed in an unused state on | 
|  | 4843 | * disk.  (On recovery, the inode will get truncated and the blocks will | 
|  | 4844 | * be freed, so we have a strong guarantee that no future commit will | 
|  | 4845 | * leave these blocks visible to the user.) | 
|  | 4846 | * | 
| Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4847 | * Another thing we have to assure is that if we are in ordered mode | 
|  | 4848 | * and inode is still attached to the committing transaction, we must | 
|  | 4849 | * we start writeout of all the dirty pages which are being truncated. | 
|  | 4850 | * This way we are sure that all the data written in the previous | 
|  | 4851 | * transaction are already on disk (truncate waits for pages under | 
|  | 4852 | * writeback). | 
|  | 4853 | * | 
|  | 4854 | * Called with inode->i_mutex down. | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4855 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4856 | int ext4_setattr(struct dentry *dentry, struct iattr *attr) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4857 | { | 
|  | 4858 | struct inode *inode = dentry->d_inode; | 
|  | 4859 | int error, rc = 0; | 
|  | 4860 | const unsigned int ia_valid = attr->ia_valid; | 
|  | 4861 |  | 
|  | 4862 | error = inode_change_ok(inode, attr); | 
|  | 4863 | if (error) | 
|  | 4864 | return error; | 
|  | 4865 |  | 
|  | 4866 | if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || | 
|  | 4867 | (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { | 
|  | 4868 | handle_t *handle; | 
|  | 4869 |  | 
|  | 4870 | /* (user+group)*(old+new) structure, inode write (sb, | 
|  | 4871 | * inode block, ? - but truncate inode update has it) */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4872 | handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+ | 
|  | 4873 | EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4874 | if (IS_ERR(handle)) { | 
|  | 4875 | error = PTR_ERR(handle); | 
|  | 4876 | goto err_out; | 
|  | 4877 | } | 
| Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 4878 | error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4879 | if (error) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4880 | ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4881 | return error; | 
|  | 4882 | } | 
|  | 4883 | /* Update corresponding info in inode so that everything is in | 
|  | 4884 | * one transaction */ | 
|  | 4885 | if (attr->ia_valid & ATTR_UID) | 
|  | 4886 | inode->i_uid = attr->ia_uid; | 
|  | 4887 | if (attr->ia_valid & ATTR_GID) | 
|  | 4888 | inode->i_gid = attr->ia_gid; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4889 | error = ext4_mark_inode_dirty(handle, inode); | 
|  | 4890 | ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4891 | } | 
|  | 4892 |  | 
| Eric Sandeen | e2b4657 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4893 | if (attr->ia_valid & ATTR_SIZE) { | 
|  | 4894 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) { | 
|  | 4895 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
|  | 4896 |  | 
|  | 4897 | if (attr->ia_size > sbi->s_bitmap_maxbytes) { | 
|  | 4898 | error = -EFBIG; | 
|  | 4899 | goto err_out; | 
|  | 4900 | } | 
|  | 4901 | } | 
|  | 4902 | } | 
|  | 4903 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4904 | if (S_ISREG(inode->i_mode) && | 
|  | 4905 | attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) { | 
|  | 4906 | handle_t *handle; | 
|  | 4907 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4908 | handle = ext4_journal_start(inode, 3); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4909 | if (IS_ERR(handle)) { | 
|  | 4910 | error = PTR_ERR(handle); | 
|  | 4911 | goto err_out; | 
|  | 4912 | } | 
|  | 4913 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4914 | error = ext4_orphan_add(handle, inode); | 
|  | 4915 | EXT4_I(inode)->i_disksize = attr->ia_size; | 
|  | 4916 | rc = ext4_mark_inode_dirty(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4917 | if (!error) | 
|  | 4918 | error = rc; | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4919 | ext4_journal_stop(handle); | 
| Jan Kara | 678aaf4 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4920 |  | 
|  | 4921 | if (ext4_should_order_data(inode)) { | 
|  | 4922 | error = ext4_begin_ordered_truncate(inode, | 
|  | 4923 | attr->ia_size); | 
|  | 4924 | if (error) { | 
|  | 4925 | /* Do as much error cleanup as possible */ | 
|  | 4926 | handle = ext4_journal_start(inode, 3); | 
|  | 4927 | if (IS_ERR(handle)) { | 
|  | 4928 | ext4_orphan_del(NULL, inode); | 
|  | 4929 | goto err_out; | 
|  | 4930 | } | 
|  | 4931 | ext4_orphan_del(handle, inode); | 
|  | 4932 | ext4_journal_stop(handle); | 
|  | 4933 | goto err_out; | 
|  | 4934 | } | 
|  | 4935 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4936 | } | 
|  | 4937 |  | 
|  | 4938 | rc = inode_setattr(inode, attr); | 
|  | 4939 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4940 | /* If inode_setattr's call to ext4_truncate failed to get a | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4941 | * transaction handle at all, we need to clean up the in-core | 
|  | 4942 | * orphan list manually. */ | 
|  | 4943 | if (inode->i_nlink) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4944 | ext4_orphan_del(NULL, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4945 |  | 
|  | 4946 | if (!rc && (ia_valid & ATTR_MODE)) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4947 | rc = ext4_acl_chmod(inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4948 |  | 
|  | 4949 | err_out: | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4950 | ext4_std_error(inode->i_sb, error); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4951 | if (!error) | 
|  | 4952 | error = rc; | 
|  | 4953 | return error; | 
|  | 4954 | } | 
|  | 4955 |  | 
| Mingming Cao | 3e3398a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4956 | int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, | 
|  | 4957 | struct kstat *stat) | 
|  | 4958 | { | 
|  | 4959 | struct inode *inode; | 
|  | 4960 | unsigned long delalloc_blocks; | 
|  | 4961 |  | 
|  | 4962 | inode = dentry->d_inode; | 
|  | 4963 | generic_fillattr(inode, stat); | 
|  | 4964 |  | 
|  | 4965 | /* | 
|  | 4966 | * We can't update i_blocks if the block allocation is delayed | 
|  | 4967 | * otherwise in the case of system crash before the real block | 
|  | 4968 | * allocation is done, we will have i_blocks inconsistent with | 
|  | 4969 | * on-disk file blocks. | 
|  | 4970 | * We always keep i_blocks updated together with real | 
|  | 4971 | * allocation. But to not confuse with user, stat | 
|  | 4972 | * will return the blocks that include the delayed allocation | 
|  | 4973 | * blocks for this file. | 
|  | 4974 | */ | 
|  | 4975 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | 
|  | 4976 | delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; | 
|  | 4977 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 
|  | 4978 |  | 
|  | 4979 | stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; | 
|  | 4980 | return 0; | 
|  | 4981 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4982 |  | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4983 | static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks, | 
|  | 4984 | int chunk) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4985 | { | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4986 | int indirects; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4987 |  | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 4988 | /* if nrblocks are contiguous */ | 
|  | 4989 | if (chunk) { | 
|  | 4990 | /* | 
|  | 4991 | * With N contiguous data blocks, it need at most | 
|  | 4992 | * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks | 
|  | 4993 | * 2 dindirect blocks | 
|  | 4994 | * 1 tindirect block | 
|  | 4995 | */ | 
|  | 4996 | indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb); | 
|  | 4997 | return indirects + 3; | 
|  | 4998 | } | 
|  | 4999 | /* | 
|  | 5000 | * if nrblocks are not contiguous, worse case, each block touch | 
|  | 5001 | * a indirect block, and each indirect block touch a double indirect | 
|  | 5002 | * block, plus a triple indirect block | 
|  | 5003 | */ | 
|  | 5004 | indirects = nrblocks * 2 + 1; | 
|  | 5005 | return indirects; | 
|  | 5006 | } | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 5007 |  | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5008 | static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) | 
|  | 5009 | { | 
|  | 5010 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) | 
| Theodore Ts'o | ac51d83 | 2008-11-06 16:49:36 -0500 | [diff] [blame] | 5011 | return ext4_indirect_trans_blocks(inode, nrblocks, chunk); | 
|  | 5012 | return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5013 | } | 
| Theodore Ts'o | ac51d83 | 2008-11-06 16:49:36 -0500 | [diff] [blame] | 5014 |  | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5015 | /* | 
|  | 5016 | * Account for index blocks, block groups bitmaps and block group | 
|  | 5017 | * descriptor blocks if modify datablocks and index blocks | 
|  | 5018 | * worse case, the indexs blocks spread over different block groups | 
|  | 5019 | * | 
|  | 5020 | * If datablocks are discontiguous, they are possible to spread over | 
|  | 5021 | * different block groups too. If they are contiugous, with flexbg, | 
|  | 5022 | * they could still across block group boundary. | 
|  | 5023 | * | 
|  | 5024 | * Also account for superblock, inode, quota and xattr blocks | 
|  | 5025 | */ | 
|  | 5026 | int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) | 
|  | 5027 | { | 
| Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 5028 | ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); | 
|  | 5029 | int gdpblocks; | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5030 | int idxblocks; | 
|  | 5031 | int ret = 0; | 
|  | 5032 |  | 
|  | 5033 | /* | 
|  | 5034 | * How many index blocks need to touch to modify nrblocks? | 
|  | 5035 | * The "Chunk" flag indicating whether the nrblocks is | 
|  | 5036 | * physically contiguous on disk | 
|  | 5037 | * | 
|  | 5038 | * For Direct IO and fallocate, they calls get_block to allocate | 
|  | 5039 | * one single extent at a time, so they could set the "Chunk" flag | 
|  | 5040 | */ | 
|  | 5041 | idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); | 
|  | 5042 |  | 
|  | 5043 | ret = idxblocks; | 
|  | 5044 |  | 
|  | 5045 | /* | 
|  | 5046 | * Now let's see how many group bitmaps and group descriptors need | 
|  | 5047 | * to account | 
|  | 5048 | */ | 
|  | 5049 | groups = idxblocks; | 
|  | 5050 | if (chunk) | 
|  | 5051 | groups += 1; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5052 | else | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5053 | groups += nrblocks; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5054 |  | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5055 | gdpblocks = groups; | 
| Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 5056 | if (groups > ngroups) | 
|  | 5057 | groups = ngroups; | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5058 | if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) | 
|  | 5059 | gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; | 
|  | 5060 |  | 
|  | 5061 | /* bitmaps and block group descriptor blocks */ | 
|  | 5062 | ret += groups + gdpblocks; | 
|  | 5063 |  | 
|  | 5064 | /* Blocks for super block, inode, quota and xattr blocks */ | 
|  | 5065 | ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5066 |  | 
|  | 5067 | return ret; | 
|  | 5068 | } | 
|  | 5069 |  | 
|  | 5070 | /* | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5071 | * Calulate the total number of credits to reserve to fit | 
| Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 5072 | * the modification of a single pages into a single transaction, | 
|  | 5073 | * which may include multiple chunks of block allocations. | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5074 | * | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 5075 | * This could be called via ext4_write_begin() | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5076 | * | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 5077 | * We need to consider the worse case, when | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5078 | * one new block per extent. | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5079 | */ | 
|  | 5080 | int ext4_writepage_trans_blocks(struct inode *inode) | 
|  | 5081 | { | 
|  | 5082 | int bpp = ext4_journal_blocks_per_page(inode); | 
|  | 5083 | int ret; | 
|  | 5084 |  | 
|  | 5085 | ret = ext4_meta_trans_blocks(inode, bpp, 0); | 
|  | 5086 |  | 
|  | 5087 | /* Account for data blocks for journalled mode */ | 
|  | 5088 | if (ext4_should_journal_data(inode)) | 
|  | 5089 | ret += bpp; | 
|  | 5090 | return ret; | 
|  | 5091 | } | 
| Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 5092 |  | 
|  | 5093 | /* | 
|  | 5094 | * Calculate the journal credits for a chunk of data modification. | 
|  | 5095 | * | 
|  | 5096 | * This is called from DIO, fallocate or whoever calling | 
| Theodore Ts'o | 12b7ac1 | 2009-05-14 00:57:44 -0400 | [diff] [blame] | 5097 | * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks. | 
| Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 5098 | * | 
|  | 5099 | * journal buffers for data blocks are not included here, as DIO | 
|  | 5100 | * and fallocate do no need to journal data buffers. | 
|  | 5101 | */ | 
|  | 5102 | int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) | 
|  | 5103 | { | 
|  | 5104 | return ext4_meta_trans_blocks(inode, nrblocks, 1); | 
|  | 5105 | } | 
|  | 5106 |  | 
| Mingming Cao | a02908f | 2008-08-19 22:16:07 -0400 | [diff] [blame] | 5107 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5108 | * The caller must have previously called ext4_reserve_inode_write(). | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5109 | * Give this, we know that the caller already has write access to iloc->bh. | 
|  | 5110 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5111 | int ext4_mark_iloc_dirty(handle_t *handle, | 
|  | 5112 | struct inode *inode, struct ext4_iloc *iloc) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5113 | { | 
|  | 5114 | int err = 0; | 
|  | 5115 |  | 
| Jean Noel Cordenner | 25ec56b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 5116 | if (test_opt(inode->i_sb, I_VERSION)) | 
|  | 5117 | inode_inc_iversion(inode); | 
|  | 5118 |  | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5119 | /* the do_update_inode consumes one bh->b_count */ | 
|  | 5120 | get_bh(iloc->bh); | 
|  | 5121 |  | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 5122 | /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5123 | err = ext4_do_update_inode(handle, inode, iloc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5124 | put_bh(iloc->bh); | 
|  | 5125 | return err; | 
|  | 5126 | } | 
|  | 5127 |  | 
|  | 5128 | /* | 
|  | 5129 | * On success, We end up with an outstanding reference count against | 
|  | 5130 | * iloc->bh.  This _must_ be cleaned up later. | 
|  | 5131 | */ | 
|  | 5132 |  | 
|  | 5133 | int | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5134 | ext4_reserve_inode_write(handle_t *handle, struct inode *inode, | 
|  | 5135 | struct ext4_iloc *iloc) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5136 | { | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5137 | int err; | 
|  | 5138 |  | 
|  | 5139 | err = ext4_get_inode_loc(inode, iloc); | 
|  | 5140 | if (!err) { | 
|  | 5141 | BUFFER_TRACE(iloc->bh, "get_write_access"); | 
|  | 5142 | err = ext4_journal_get_write_access(handle, iloc->bh); | 
|  | 5143 | if (err) { | 
|  | 5144 | brelse(iloc->bh); | 
|  | 5145 | iloc->bh = NULL; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5146 | } | 
|  | 5147 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5148 | ext4_std_error(inode->i_sb, err); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5149 | return err; | 
|  | 5150 | } | 
|  | 5151 |  | 
|  | 5152 | /* | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5153 | * Expand an inode by new_extra_isize bytes. | 
|  | 5154 | * Returns 0 on success or negative error number on failure. | 
|  | 5155 | */ | 
| Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 5156 | static int ext4_expand_extra_isize(struct inode *inode, | 
|  | 5157 | unsigned int new_extra_isize, | 
|  | 5158 | struct ext4_iloc iloc, | 
|  | 5159 | handle_t *handle) | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5160 | { | 
|  | 5161 | struct ext4_inode *raw_inode; | 
|  | 5162 | struct ext4_xattr_ibody_header *header; | 
|  | 5163 | struct ext4_xattr_entry *entry; | 
|  | 5164 |  | 
|  | 5165 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) | 
|  | 5166 | return 0; | 
|  | 5167 |  | 
|  | 5168 | raw_inode = ext4_raw_inode(&iloc); | 
|  | 5169 |  | 
|  | 5170 | header = IHDR(inode, raw_inode); | 
|  | 5171 | entry = IFIRST(header); | 
|  | 5172 |  | 
|  | 5173 | /* No extended attributes present */ | 
|  | 5174 | if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) || | 
|  | 5175 | header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { | 
|  | 5176 | memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, | 
|  | 5177 | new_extra_isize); | 
|  | 5178 | EXT4_I(inode)->i_extra_isize = new_extra_isize; | 
|  | 5179 | return 0; | 
|  | 5180 | } | 
|  | 5181 |  | 
|  | 5182 | /* try to expand with EAs present */ | 
|  | 5183 | return ext4_expand_extra_isize_ea(inode, new_extra_isize, | 
|  | 5184 | raw_inode, handle); | 
|  | 5185 | } | 
|  | 5186 |  | 
|  | 5187 | /* | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5188 | * What we do here is to mark the in-core inode as clean with respect to inode | 
|  | 5189 | * dirtiness (it may still be data-dirty). | 
|  | 5190 | * This means that the in-core inode may be reaped by prune_icache | 
|  | 5191 | * without having to perform any I/O.  This is a very good thing, | 
|  | 5192 | * because *any* task may call prune_icache - even ones which | 
|  | 5193 | * have a transaction open against a different journal. | 
|  | 5194 | * | 
|  | 5195 | * Is this cheating?  Not really.  Sure, we haven't written the | 
|  | 5196 | * inode out, but prune_icache isn't a user-visible syncing function. | 
|  | 5197 | * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) | 
|  | 5198 | * we start and wait on commits. | 
|  | 5199 | * | 
|  | 5200 | * Is this efficient/effective?  Well, we're being nice to the system | 
|  | 5201 | * by cleaning up our inodes proactively so they can be reaped | 
|  | 5202 | * without I/O.  But we are potentially leaving up to five seconds' | 
|  | 5203 | * worth of inodes floating about which prune_icache wants us to | 
|  | 5204 | * write out.  One way to fix that would be to get prune_icache() | 
|  | 5205 | * to do a write_super() to free up some memory.  It has the desired | 
|  | 5206 | * effect. | 
|  | 5207 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5208 | int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5209 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5210 | struct ext4_iloc iloc; | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5211 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
|  | 5212 | static unsigned int mnt_count; | 
|  | 5213 | int err, ret; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5214 |  | 
|  | 5215 | might_sleep(); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5216 | err = ext4_reserve_inode_write(handle, inode, &iloc); | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5217 | if (ext4_handle_valid(handle) && | 
|  | 5218 | EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5219 | !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) { | 
|  | 5220 | /* | 
|  | 5221 | * We need extra buffer credits since we may write into EA block | 
|  | 5222 | * with this same handle. If journal_extend fails, then it will | 
|  | 5223 | * only result in a minor loss of functionality for that inode. | 
|  | 5224 | * If this is felt to be critical, then e2fsck should be run to | 
|  | 5225 | * force a large enough s_min_extra_isize. | 
|  | 5226 | */ | 
|  | 5227 | if ((jbd2_journal_extend(handle, | 
|  | 5228 | EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { | 
|  | 5229 | ret = ext4_expand_extra_isize(inode, | 
|  | 5230 | sbi->s_want_extra_isize, | 
|  | 5231 | iloc, handle); | 
|  | 5232 | if (ret) { | 
|  | 5233 | EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND; | 
| Aneesh Kumar K.V | c1bddad | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 5234 | if (mnt_count != | 
|  | 5235 | le16_to_cpu(sbi->s_es->s_mnt_count)) { | 
| Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 5236 | ext4_warning(inode->i_sb, __func__, | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5237 | "Unable to expand inode %lu. Delete" | 
|  | 5238 | " some EAs or run e2fsck.", | 
|  | 5239 | inode->i_ino); | 
| Aneesh Kumar K.V | c1bddad | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 5240 | mnt_count = | 
|  | 5241 | le16_to_cpu(sbi->s_es->s_mnt_count); | 
| Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 5242 | } | 
|  | 5243 | } | 
|  | 5244 | } | 
|  | 5245 | } | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5246 | if (!err) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5247 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5248 | return err; | 
|  | 5249 | } | 
|  | 5250 |  | 
|  | 5251 | /* | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5252 | * ext4_dirty_inode() is called from __mark_inode_dirty() | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5253 | * | 
|  | 5254 | * We're really interested in the case where a file is being extended. | 
|  | 5255 | * i_size has been changed by generic_commit_write() and we thus need | 
|  | 5256 | * to include the updated inode in the current transaction. | 
|  | 5257 | * | 
| Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 5258 | * Also, vfs_dq_alloc_block() will always dirty the inode when blocks | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5259 | * are allocated to the file. | 
|  | 5260 | * | 
|  | 5261 | * If the inode is marked synchronous, we don't honour that here - doing | 
|  | 5262 | * so would cause a commit on atime updates, which we don't bother doing. | 
|  | 5263 | * We handle synchronous inodes at the highest possible level. | 
|  | 5264 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5265 | void ext4_dirty_inode(struct inode *inode) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5266 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5267 | handle_t *current_handle = ext4_journal_current_handle(); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5268 | handle_t *handle; | 
|  | 5269 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5270 | if (!ext4_handle_valid(current_handle)) { | 
|  | 5271 | ext4_mark_inode_dirty(current_handle, inode); | 
|  | 5272 | return; | 
|  | 5273 | } | 
|  | 5274 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5275 | handle = ext4_journal_start(inode, 2); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5276 | if (IS_ERR(handle)) | 
|  | 5277 | goto out; | 
|  | 5278 | if (current_handle && | 
|  | 5279 | current_handle->h_transaction != handle->h_transaction) { | 
|  | 5280 | /* This task has a transaction open against a different fs */ | 
|  | 5281 | printk(KERN_EMERG "%s: transactions do not match!\n", | 
| Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 5282 | __func__); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5283 | } else { | 
|  | 5284 | jbd_debug(5, "marking dirty.  outer handle=%p\n", | 
|  | 5285 | current_handle); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5286 | ext4_mark_inode_dirty(handle, inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5287 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5288 | ext4_journal_stop(handle); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5289 | out: | 
|  | 5290 | return; | 
|  | 5291 | } | 
|  | 5292 |  | 
|  | 5293 | #if 0 | 
|  | 5294 | /* | 
|  | 5295 | * Bind an inode's backing buffer_head into this transaction, to prevent | 
|  | 5296 | * it from being flushed to disk early.  Unlike | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5297 | * ext4_reserve_inode_write, this leaves behind no bh reference and | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5298 | * returns no iloc structure, so the caller needs to repeat the iloc | 
|  | 5299 | * lookup to mark the inode dirty later. | 
|  | 5300 | */ | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5301 | static int ext4_pin_inode(handle_t *handle, struct inode *inode) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5302 | { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5303 | struct ext4_iloc iloc; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5304 |  | 
|  | 5305 | int err = 0; | 
|  | 5306 | if (handle) { | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5307 | err = ext4_get_inode_loc(inode, &iloc); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5308 | if (!err) { | 
|  | 5309 | BUFFER_TRACE(iloc.bh, "get_write_access"); | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 5310 | err = jbd2_journal_get_write_access(handle, iloc.bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5311 | if (!err) | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5312 | err = ext4_handle_dirty_metadata(handle, | 
|  | 5313 | inode, | 
|  | 5314 | iloc.bh); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5315 | brelse(iloc.bh); | 
|  | 5316 | } | 
|  | 5317 | } | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5318 | ext4_std_error(inode->i_sb, err); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5319 | return err; | 
|  | 5320 | } | 
|  | 5321 | #endif | 
|  | 5322 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5323 | int ext4_change_inode_journal_flag(struct inode *inode, int val) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5324 | { | 
|  | 5325 | journal_t *journal; | 
|  | 5326 | handle_t *handle; | 
|  | 5327 | int err; | 
|  | 5328 |  | 
|  | 5329 | /* | 
|  | 5330 | * We have to be very careful here: changing a data block's | 
|  | 5331 | * journaling status dynamically is dangerous.  If we write a | 
|  | 5332 | * data block to the journal, change the status and then delete | 
|  | 5333 | * that block, we risk forgetting to revoke the old log record | 
|  | 5334 | * from the journal and so a subsequent replay can corrupt data. | 
|  | 5335 | * So, first we make sure that the journal is empty and that | 
|  | 5336 | * nobody is changing anything. | 
|  | 5337 | */ | 
|  | 5338 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5339 | journal = EXT4_JOURNAL(inode); | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5340 | if (!journal) | 
|  | 5341 | return 0; | 
| Dave Hansen | d699594 | 2007-07-18 08:33:51 -0400 | [diff] [blame] | 5342 | if (is_journal_aborted(journal)) | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5343 | return -EROFS; | 
|  | 5344 |  | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 5345 | jbd2_journal_lock_updates(journal); | 
|  | 5346 | jbd2_journal_flush(journal); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5347 |  | 
|  | 5348 | /* | 
|  | 5349 | * OK, there are no updates running now, and all cached data is | 
|  | 5350 | * synced to disk.  We are now in a completely consistent state | 
|  | 5351 | * which doesn't have anything in the journal, and we know that | 
|  | 5352 | * no filesystem updates are running, so it is safe to modify | 
|  | 5353 | * the inode's in-core data-journaling state flag now. | 
|  | 5354 | */ | 
|  | 5355 |  | 
|  | 5356 | if (val) | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5357 | EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL; | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5358 | else | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5359 | EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL; | 
|  | 5360 | ext4_set_aops(inode); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5361 |  | 
| Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 5362 | jbd2_journal_unlock_updates(journal); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5363 |  | 
|  | 5364 | /* Finally we can mark the inode as dirty. */ | 
|  | 5365 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5366 | handle = ext4_journal_start(inode, 1); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5367 | if (IS_ERR(handle)) | 
|  | 5368 | return PTR_ERR(handle); | 
|  | 5369 |  | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5370 | err = ext4_mark_inode_dirty(handle, inode); | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5371 | ext4_handle_sync(handle); | 
| Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 5372 | ext4_journal_stop(handle); | 
|  | 5373 | ext4_std_error(inode->i_sb, err); | 
| Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5374 |  | 
|  | 5375 | return err; | 
|  | 5376 | } | 
| Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5377 |  | 
|  | 5378 | static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) | 
|  | 5379 | { | 
|  | 5380 | return !buffer_mapped(bh); | 
|  | 5381 | } | 
|  | 5382 |  | 
| Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5383 | 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] | 5384 | { | 
| Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5385 | struct page *page = vmf->page; | 
| Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5386 | loff_t size; | 
|  | 5387 | unsigned long len; | 
|  | 5388 | int ret = -EINVAL; | 
| Aneesh Kumar K.V | 79f0be8 | 2008-10-08 23:13:30 -0400 | [diff] [blame] | 5389 | void *fsdata; | 
| Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5390 | struct file *file = vma->vm_file; | 
|  | 5391 | struct inode *inode = file->f_path.dentry->d_inode; | 
|  | 5392 | struct address_space *mapping = inode->i_mapping; | 
|  | 5393 |  | 
|  | 5394 | /* | 
|  | 5395 | * Get i_alloc_sem to stop truncates messing with the inode. We cannot | 
|  | 5396 | * get i_mutex because we are already holding mmap_sem. | 
|  | 5397 | */ | 
|  | 5398 | down_read(&inode->i_alloc_sem); | 
|  | 5399 | size = i_size_read(inode); | 
|  | 5400 | if (page->mapping != mapping || size <= page_offset(page) | 
|  | 5401 | || !PageUptodate(page)) { | 
|  | 5402 | /* page got truncated from under us? */ | 
|  | 5403 | goto out_unlock; | 
|  | 5404 | } | 
|  | 5405 | ret = 0; | 
|  | 5406 | if (PageMappedToDisk(page)) | 
|  | 5407 | goto out_unlock; | 
|  | 5408 |  | 
|  | 5409 | if (page->index == size >> PAGE_CACHE_SHIFT) | 
|  | 5410 | len = size & ~PAGE_CACHE_MASK; | 
|  | 5411 | else | 
|  | 5412 | len = PAGE_CACHE_SIZE; | 
|  | 5413 |  | 
|  | 5414 | if (page_has_buffers(page)) { | 
|  | 5415 | /* return if we have all the buffers mapped */ | 
|  | 5416 | if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, | 
|  | 5417 | ext4_bh_unmapped)) | 
|  | 5418 | goto out_unlock; | 
|  | 5419 | } | 
|  | 5420 | /* | 
|  | 5421 | * OK, we need to fill the hole... Do write_begin write_end | 
|  | 5422 | * to do block allocation/reservation.We are not holding | 
|  | 5423 | * inode.i__mutex here. That allow * parallel write_begin, | 
|  | 5424 | * write_end call. lock_page prevent this from happening | 
|  | 5425 | * on the same page though | 
|  | 5426 | */ | 
|  | 5427 | 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] | 5428 | len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); | 
| Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5429 | if (ret < 0) | 
|  | 5430 | goto out_unlock; | 
|  | 5431 | 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] | 5432 | len, len, page, fsdata); | 
| Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5433 | if (ret < 0) | 
|  | 5434 | goto out_unlock; | 
|  | 5435 | ret = 0; | 
|  | 5436 | out_unlock: | 
| Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5437 | if (ret) | 
|  | 5438 | ret = VM_FAULT_SIGBUS; | 
| Aneesh Kumar K.V | 2e9ee85 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5439 | up_read(&inode->i_alloc_sem); | 
|  | 5440 | return ret; | 
|  | 5441 | } |