Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext2/inode.c |
| 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@dcs.ed.ac.uk), 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 | * |
| 22 | * Assorted race fixes, rewrite of ext2_get_block() by Al Viro, 2000 |
| 23 | */ |
| 24 | |
| 25 | #include <linux/smp_lock.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/highuid.h> |
| 28 | #include <linux/pagemap.h> |
| 29 | #include <linux/quotaops.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/writeback.h> |
| 32 | #include <linux/buffer_head.h> |
| 33 | #include <linux/mpage.h> |
| 34 | #include "ext2.h" |
| 35 | #include "acl.h" |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 36 | #include "xip.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | MODULE_AUTHOR("Remy Card and others"); |
| 39 | MODULE_DESCRIPTION("Second Extended Filesystem"); |
| 40 | MODULE_LICENSE("GPL"); |
| 41 | |
| 42 | static int ext2_update_inode(struct inode * inode, int do_sync); |
| 43 | |
| 44 | /* |
| 45 | * Test whether an inode is a fast symlink. |
| 46 | */ |
| 47 | static inline int ext2_inode_is_fast_symlink(struct inode *inode) |
| 48 | { |
| 49 | int ea_blocks = EXT2_I(inode)->i_file_acl ? |
| 50 | (inode->i_sb->s_blocksize >> 9) : 0; |
| 51 | |
| 52 | return (S_ISLNK(inode->i_mode) && |
| 53 | inode->i_blocks - ea_blocks == 0); |
| 54 | } |
| 55 | |
| 56 | /* |
Bernard Blackham | e072c6f | 2005-04-16 15:25:45 -0700 | [diff] [blame] | 57 | * Called at each iput(). |
| 58 | * |
| 59 | * The inode may be "bad" if ext2_read_inode() saw an error from |
| 60 | * ext2_get_inode(), so we need to check that to avoid freeing random disk |
| 61 | * blocks. |
| 62 | */ |
| 63 | void ext2_put_inode(struct inode *inode) |
| 64 | { |
| 65 | if (!is_bad_inode(inode)) |
| 66 | ext2_discard_prealloc(inode); |
| 67 | } |
| 68 | |
| 69 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * Called at the last iput() if i_nlink is zero. |
| 71 | */ |
| 72 | void ext2_delete_inode (struct inode * inode) |
| 73 | { |
Mark Fasheh | fef2665 | 2005-09-09 13:01:31 -0700 | [diff] [blame] | 74 | truncate_inode_pages(&inode->i_data, 0); |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (is_bad_inode(inode)) |
| 77 | goto no_delete; |
| 78 | EXT2_I(inode)->i_dtime = get_seconds(); |
| 79 | mark_inode_dirty(inode); |
| 80 | ext2_update_inode(inode, inode_needs_sync(inode)); |
| 81 | |
| 82 | inode->i_size = 0; |
| 83 | if (inode->i_blocks) |
| 84 | ext2_truncate (inode); |
| 85 | ext2_free_inode (inode); |
| 86 | |
| 87 | return; |
| 88 | no_delete: |
| 89 | clear_inode(inode); /* We must guarantee clearing of inode... */ |
| 90 | } |
| 91 | |
| 92 | void ext2_discard_prealloc (struct inode * inode) |
| 93 | { |
| 94 | #ifdef EXT2_PREALLOCATE |
| 95 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 96 | write_lock(&ei->i_meta_lock); |
| 97 | if (ei->i_prealloc_count) { |
| 98 | unsigned short total = ei->i_prealloc_count; |
| 99 | unsigned long block = ei->i_prealloc_block; |
| 100 | ei->i_prealloc_count = 0; |
| 101 | ei->i_prealloc_block = 0; |
| 102 | write_unlock(&ei->i_meta_lock); |
| 103 | ext2_free_blocks (inode, block, total); |
| 104 | return; |
| 105 | } else |
| 106 | write_unlock(&ei->i_meta_lock); |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | static int ext2_alloc_block (struct inode * inode, unsigned long goal, int *err) |
| 111 | { |
| 112 | #ifdef EXT2FS_DEBUG |
| 113 | static unsigned long alloc_hits, alloc_attempts; |
| 114 | #endif |
| 115 | unsigned long result; |
| 116 | |
| 117 | |
| 118 | #ifdef EXT2_PREALLOCATE |
| 119 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 120 | write_lock(&ei->i_meta_lock); |
| 121 | if (ei->i_prealloc_count && |
| 122 | (goal == ei->i_prealloc_block || goal + 1 == ei->i_prealloc_block)) |
| 123 | { |
| 124 | result = ei->i_prealloc_block++; |
| 125 | ei->i_prealloc_count--; |
| 126 | write_unlock(&ei->i_meta_lock); |
| 127 | ext2_debug ("preallocation hit (%lu/%lu).\n", |
| 128 | ++alloc_hits, ++alloc_attempts); |
| 129 | } else { |
| 130 | write_unlock(&ei->i_meta_lock); |
| 131 | ext2_discard_prealloc (inode); |
| 132 | ext2_debug ("preallocation miss (%lu/%lu).\n", |
| 133 | alloc_hits, ++alloc_attempts); |
| 134 | if (S_ISREG(inode->i_mode)) |
| 135 | result = ext2_new_block (inode, goal, |
| 136 | &ei->i_prealloc_count, |
| 137 | &ei->i_prealloc_block, err); |
| 138 | else |
| 139 | result = ext2_new_block(inode, goal, NULL, NULL, err); |
| 140 | } |
| 141 | #else |
| 142 | result = ext2_new_block (inode, goal, 0, 0, err); |
| 143 | #endif |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | typedef struct { |
| 148 | __le32 *p; |
| 149 | __le32 key; |
| 150 | struct buffer_head *bh; |
| 151 | } Indirect; |
| 152 | |
| 153 | static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) |
| 154 | { |
| 155 | p->key = *(p->p = v); |
| 156 | p->bh = bh; |
| 157 | } |
| 158 | |
| 159 | static inline int verify_chain(Indirect *from, Indirect *to) |
| 160 | { |
| 161 | while (from <= to && from->key == *from->p) |
| 162 | from++; |
| 163 | return (from > to); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * ext2_block_to_path - parse the block number into array of offsets |
| 168 | * @inode: inode in question (we are only interested in its superblock) |
| 169 | * @i_block: block number to be parsed |
| 170 | * @offsets: array to store the offsets in |
| 171 | * @boundary: set this non-zero if the referred-to block is likely to be |
| 172 | * followed (on disk) by an indirect block. |
| 173 | * To store the locations of file's data ext2 uses a data structure common |
| 174 | * for UNIX filesystems - tree of pointers anchored in the inode, with |
| 175 | * data blocks at leaves and indirect blocks in intermediate nodes. |
| 176 | * This function translates the block number into path in that tree - |
| 177 | * return value is the path length and @offsets[n] is the offset of |
| 178 | * pointer to (n+1)th node in the nth one. If @block is out of range |
| 179 | * (negative or too large) warning is printed and zero returned. |
| 180 | * |
| 181 | * Note: function doesn't find node addresses, so no IO is needed. All |
| 182 | * we need to know is the capacity of indirect blocks (taken from the |
| 183 | * inode->i_sb). |
| 184 | */ |
| 185 | |
| 186 | /* |
| 187 | * Portability note: the last comparison (check that we fit into triple |
| 188 | * indirect block) is spelled differently, because otherwise on an |
| 189 | * architecture with 32-bit longs and 8Kb pages we might get into trouble |
| 190 | * if our filesystem had 8Kb blocks. We might use long long, but that would |
| 191 | * kill us on x86. Oh, well, at least the sign propagation does not matter - |
| 192 | * i_block would have to be negative in the very beginning, so we would not |
| 193 | * get there at all. |
| 194 | */ |
| 195 | |
| 196 | static int ext2_block_to_path(struct inode *inode, |
| 197 | long i_block, int offsets[4], int *boundary) |
| 198 | { |
| 199 | int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb); |
| 200 | int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb); |
| 201 | const long direct_blocks = EXT2_NDIR_BLOCKS, |
| 202 | indirect_blocks = ptrs, |
| 203 | double_blocks = (1 << (ptrs_bits * 2)); |
| 204 | int n = 0; |
| 205 | int final = 0; |
| 206 | |
| 207 | if (i_block < 0) { |
| 208 | ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0"); |
| 209 | } else if (i_block < direct_blocks) { |
| 210 | offsets[n++] = i_block; |
| 211 | final = direct_blocks; |
| 212 | } else if ( (i_block -= direct_blocks) < indirect_blocks) { |
| 213 | offsets[n++] = EXT2_IND_BLOCK; |
| 214 | offsets[n++] = i_block; |
| 215 | final = ptrs; |
| 216 | } else if ((i_block -= indirect_blocks) < double_blocks) { |
| 217 | offsets[n++] = EXT2_DIND_BLOCK; |
| 218 | offsets[n++] = i_block >> ptrs_bits; |
| 219 | offsets[n++] = i_block & (ptrs - 1); |
| 220 | final = ptrs; |
| 221 | } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { |
| 222 | offsets[n++] = EXT2_TIND_BLOCK; |
| 223 | offsets[n++] = i_block >> (ptrs_bits * 2); |
| 224 | offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); |
| 225 | offsets[n++] = i_block & (ptrs - 1); |
| 226 | final = ptrs; |
| 227 | } else { |
| 228 | ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big"); |
| 229 | } |
| 230 | if (boundary) |
| 231 | *boundary = (i_block & (ptrs - 1)) == (final - 1); |
| 232 | return n; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * ext2_get_branch - read the chain of indirect blocks leading to data |
| 237 | * @inode: inode in question |
| 238 | * @depth: depth of the chain (1 - direct pointer, etc.) |
| 239 | * @offsets: offsets of pointers in inode/indirect blocks |
| 240 | * @chain: place to store the result |
| 241 | * @err: here we store the error value |
| 242 | * |
| 243 | * Function fills the array of triples <key, p, bh> and returns %NULL |
| 244 | * if everything went OK or the pointer to the last filled triple |
| 245 | * (incomplete one) otherwise. Upon the return chain[i].key contains |
| 246 | * the number of (i+1)-th block in the chain (as it is stored in memory, |
| 247 | * i.e. little-endian 32-bit), chain[i].p contains the address of that |
| 248 | * number (it points into struct inode for i==0 and into the bh->b_data |
| 249 | * for i>0) and chain[i].bh points to the buffer_head of i-th indirect |
| 250 | * block for i>0 and NULL for i==0. In other words, it holds the block |
| 251 | * numbers of the chain, addresses they were taken from (and where we can |
| 252 | * verify that chain did not change) and buffer_heads hosting these |
| 253 | * numbers. |
| 254 | * |
| 255 | * Function stops when it stumbles upon zero pointer (absent block) |
| 256 | * (pointer to last triple returned, *@err == 0) |
| 257 | * or when it gets an IO error reading an indirect block |
| 258 | * (ditto, *@err == -EIO) |
| 259 | * or when it notices that chain had been changed while it was reading |
| 260 | * (ditto, *@err == -EAGAIN) |
| 261 | * or when it reads all @depth-1 indirect blocks successfully and finds |
| 262 | * the whole chain, all way to the data (returns %NULL, *err == 0). |
| 263 | */ |
| 264 | static Indirect *ext2_get_branch(struct inode *inode, |
| 265 | int depth, |
| 266 | int *offsets, |
| 267 | Indirect chain[4], |
| 268 | int *err) |
| 269 | { |
| 270 | struct super_block *sb = inode->i_sb; |
| 271 | Indirect *p = chain; |
| 272 | struct buffer_head *bh; |
| 273 | |
| 274 | *err = 0; |
| 275 | /* i_data is not going away, no lock needed */ |
| 276 | add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets); |
| 277 | if (!p->key) |
| 278 | goto no_block; |
| 279 | while (--depth) { |
| 280 | bh = sb_bread(sb, le32_to_cpu(p->key)); |
| 281 | if (!bh) |
| 282 | goto failure; |
| 283 | read_lock(&EXT2_I(inode)->i_meta_lock); |
| 284 | if (!verify_chain(chain, p)) |
| 285 | goto changed; |
| 286 | add_chain(++p, bh, (__le32*)bh->b_data + *++offsets); |
| 287 | read_unlock(&EXT2_I(inode)->i_meta_lock); |
| 288 | if (!p->key) |
| 289 | goto no_block; |
| 290 | } |
| 291 | return NULL; |
| 292 | |
| 293 | changed: |
| 294 | read_unlock(&EXT2_I(inode)->i_meta_lock); |
| 295 | brelse(bh); |
| 296 | *err = -EAGAIN; |
| 297 | goto no_block; |
| 298 | failure: |
| 299 | *err = -EIO; |
| 300 | no_block: |
| 301 | return p; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * ext2_find_near - find a place for allocation with sufficient locality |
| 306 | * @inode: owner |
| 307 | * @ind: descriptor of indirect block. |
| 308 | * |
| 309 | * This function returns the prefered place for block allocation. |
| 310 | * It is used when heuristic for sequential allocation fails. |
| 311 | * Rules are: |
| 312 | * + if there is a block to the left of our position - allocate near it. |
| 313 | * + if pointer will live in indirect block - allocate near that block. |
| 314 | * + if pointer will live in inode - allocate in the same cylinder group. |
| 315 | * |
| 316 | * In the latter case we colour the starting block by the callers PID to |
| 317 | * prevent it from clashing with concurrent allocations for a different inode |
| 318 | * in the same block group. The PID is used here so that functionally related |
| 319 | * files will be close-by on-disk. |
| 320 | * |
| 321 | * Caller must make sure that @ind is valid and will stay that way. |
| 322 | */ |
| 323 | |
| 324 | static unsigned long ext2_find_near(struct inode *inode, Indirect *ind) |
| 325 | { |
| 326 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 327 | __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; |
| 328 | __le32 *p; |
| 329 | unsigned long bg_start; |
| 330 | unsigned long colour; |
| 331 | |
| 332 | /* Try to find previous block */ |
| 333 | for (p = ind->p - 1; p >= start; p--) |
| 334 | if (*p) |
| 335 | return le32_to_cpu(*p); |
| 336 | |
| 337 | /* No such thing, so let's try location of indirect block */ |
| 338 | if (ind->bh) |
| 339 | return ind->bh->b_blocknr; |
| 340 | |
| 341 | /* |
| 342 | * It is going to be refered from inode itself? OK, just put it into |
| 343 | * the same cylinder group then. |
| 344 | */ |
| 345 | bg_start = (ei->i_block_group * EXT2_BLOCKS_PER_GROUP(inode->i_sb)) + |
| 346 | le32_to_cpu(EXT2_SB(inode->i_sb)->s_es->s_first_data_block); |
| 347 | colour = (current->pid % 16) * |
| 348 | (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16); |
| 349 | return bg_start + colour; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * ext2_find_goal - find a prefered place for allocation. |
| 354 | * @inode: owner |
| 355 | * @block: block we want |
| 356 | * @chain: chain of indirect blocks |
| 357 | * @partial: pointer to the last triple within a chain |
| 358 | * @goal: place to store the result. |
| 359 | * |
| 360 | * Normally this function find the prefered place for block allocation, |
| 361 | * stores it in *@goal and returns zero. If the branch had been changed |
| 362 | * under us we return -EAGAIN. |
| 363 | */ |
| 364 | |
| 365 | static inline int ext2_find_goal(struct inode *inode, |
| 366 | long block, |
| 367 | Indirect chain[4], |
| 368 | Indirect *partial, |
| 369 | unsigned long *goal) |
| 370 | { |
| 371 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 372 | write_lock(&ei->i_meta_lock); |
| 373 | if ((block == ei->i_next_alloc_block + 1) && ei->i_next_alloc_goal) { |
| 374 | ei->i_next_alloc_block++; |
| 375 | ei->i_next_alloc_goal++; |
| 376 | } |
| 377 | if (verify_chain(chain, partial)) { |
| 378 | /* |
| 379 | * try the heuristic for sequential allocation, |
| 380 | * failing that at least try to get decent locality. |
| 381 | */ |
| 382 | if (block == ei->i_next_alloc_block) |
| 383 | *goal = ei->i_next_alloc_goal; |
| 384 | if (!*goal) |
| 385 | *goal = ext2_find_near(inode, partial); |
| 386 | write_unlock(&ei->i_meta_lock); |
| 387 | return 0; |
| 388 | } |
| 389 | write_unlock(&ei->i_meta_lock); |
| 390 | return -EAGAIN; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * ext2_alloc_branch - allocate and set up a chain of blocks. |
| 395 | * @inode: owner |
| 396 | * @num: depth of the chain (number of blocks to allocate) |
| 397 | * @offsets: offsets (in the blocks) to store the pointers to next. |
| 398 | * @branch: place to store the chain in. |
| 399 | * |
| 400 | * This function allocates @num blocks, zeroes out all but the last one, |
| 401 | * links them into chain and (if we are synchronous) writes them to disk. |
| 402 | * In other words, it prepares a branch that can be spliced onto the |
| 403 | * inode. It stores the information about that chain in the branch[], in |
| 404 | * the same format as ext2_get_branch() would do. We are calling it after |
| 405 | * we had read the existing part of chain and partial points to the last |
| 406 | * triple of that (one with zero ->key). Upon the exit we have the same |
| 407 | * picture as after the successful ext2_get_block(), excpet that in one |
| 408 | * place chain is disconnected - *branch->p is still zero (we did not |
| 409 | * set the last link), but branch->key contains the number that should |
| 410 | * be placed into *branch->p to fill that gap. |
| 411 | * |
| 412 | * If allocation fails we free all blocks we've allocated (and forget |
| 413 | * their buffer_heads) and return the error value the from failed |
| 414 | * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain |
| 415 | * as described above and return 0. |
| 416 | */ |
| 417 | |
| 418 | static int ext2_alloc_branch(struct inode *inode, |
| 419 | int num, |
| 420 | unsigned long goal, |
| 421 | int *offsets, |
| 422 | Indirect *branch) |
| 423 | { |
| 424 | int blocksize = inode->i_sb->s_blocksize; |
| 425 | int n = 0; |
| 426 | int err; |
| 427 | int i; |
| 428 | int parent = ext2_alloc_block(inode, goal, &err); |
| 429 | |
| 430 | branch[0].key = cpu_to_le32(parent); |
| 431 | if (parent) for (n = 1; n < num; n++) { |
| 432 | struct buffer_head *bh; |
| 433 | /* Allocate the next block */ |
| 434 | int nr = ext2_alloc_block(inode, parent, &err); |
| 435 | if (!nr) |
| 436 | break; |
| 437 | branch[n].key = cpu_to_le32(nr); |
| 438 | /* |
| 439 | * Get buffer_head for parent block, zero it out and set |
| 440 | * the pointer to new one, then send parent to disk. |
| 441 | */ |
| 442 | bh = sb_getblk(inode->i_sb, parent); |
Glauber de Oliveira Costa | 2973dfd | 2005-10-30 15:03:05 -0800 | [diff] [blame] | 443 | if (!bh) { |
| 444 | err = -EIO; |
| 445 | break; |
| 446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | lock_buffer(bh); |
| 448 | memset(bh->b_data, 0, blocksize); |
| 449 | branch[n].bh = bh; |
| 450 | branch[n].p = (__le32 *) bh->b_data + offsets[n]; |
| 451 | *branch[n].p = branch[n].key; |
| 452 | set_buffer_uptodate(bh); |
| 453 | unlock_buffer(bh); |
| 454 | mark_buffer_dirty_inode(bh, inode); |
| 455 | /* We used to sync bh here if IS_SYNC(inode). |
| 456 | * But we now rely upon generic_osync_inode() |
| 457 | * and b_inode_buffers. But not for directories. |
| 458 | */ |
| 459 | if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) |
| 460 | sync_dirty_buffer(bh); |
| 461 | parent = nr; |
| 462 | } |
| 463 | if (n == num) |
| 464 | return 0; |
| 465 | |
| 466 | /* Allocation failed, free what we already allocated */ |
| 467 | for (i = 1; i < n; i++) |
| 468 | bforget(branch[i].bh); |
| 469 | for (i = 0; i < n; i++) |
| 470 | ext2_free_blocks(inode, le32_to_cpu(branch[i].key), 1); |
| 471 | return err; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * ext2_splice_branch - splice the allocated branch onto inode. |
| 476 | * @inode: owner |
| 477 | * @block: (logical) number of block we are adding |
| 478 | * @chain: chain of indirect blocks (with a missing link - see |
| 479 | * ext2_alloc_branch) |
| 480 | * @where: location of missing link |
| 481 | * @num: number of blocks we are adding |
| 482 | * |
| 483 | * This function verifies that chain (up to the missing link) had not |
| 484 | * changed, fills the missing link and does all housekeeping needed in |
| 485 | * inode (->i_blocks, etc.). In case of success we end up with the full |
| 486 | * chain to new block and return 0. Otherwise (== chain had been changed) |
| 487 | * we free the new blocks (forgetting their buffer_heads, indeed) and |
| 488 | * return -EAGAIN. |
| 489 | */ |
| 490 | |
| 491 | static inline int ext2_splice_branch(struct inode *inode, |
| 492 | long block, |
| 493 | Indirect chain[4], |
| 494 | Indirect *where, |
| 495 | int num) |
| 496 | { |
| 497 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 498 | int i; |
| 499 | |
| 500 | /* Verify that place we are splicing to is still there and vacant */ |
| 501 | |
| 502 | write_lock(&ei->i_meta_lock); |
| 503 | if (!verify_chain(chain, where-1) || *where->p) |
| 504 | goto changed; |
| 505 | |
| 506 | /* That's it */ |
| 507 | |
| 508 | *where->p = where->key; |
| 509 | ei->i_next_alloc_block = block; |
| 510 | ei->i_next_alloc_goal = le32_to_cpu(where[num-1].key); |
| 511 | |
| 512 | write_unlock(&ei->i_meta_lock); |
| 513 | |
| 514 | /* We are done with atomic stuff, now do the rest of housekeeping */ |
| 515 | |
| 516 | inode->i_ctime = CURRENT_TIME_SEC; |
| 517 | |
| 518 | /* had we spliced it onto indirect block? */ |
| 519 | if (where->bh) |
| 520 | mark_buffer_dirty_inode(where->bh, inode); |
| 521 | |
| 522 | mark_inode_dirty(inode); |
| 523 | return 0; |
| 524 | |
| 525 | changed: |
| 526 | write_unlock(&ei->i_meta_lock); |
| 527 | for (i = 1; i < num; i++) |
| 528 | bforget(where[i].bh); |
| 529 | for (i = 0; i < num; i++) |
| 530 | ext2_free_blocks(inode, le32_to_cpu(where[i].key), 1); |
| 531 | return -EAGAIN; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Allocation strategy is simple: if we have to allocate something, we will |
| 536 | * have to go the whole way to leaf. So let's do it before attaching anything |
| 537 | * to tree, set linkage between the newborn blocks, write them if sync is |
| 538 | * required, recheck the path, free and repeat if check fails, otherwise |
| 539 | * set the last missing link (that will protect us from any truncate-generated |
| 540 | * removals - all blocks on the path are immune now) and possibly force the |
| 541 | * write on the parent block. |
| 542 | * That has a nice additional property: no special recovery from the failed |
| 543 | * allocations is needed - we simply release blocks and do not touch anything |
| 544 | * reachable from inode. |
| 545 | */ |
| 546 | |
| 547 | int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create) |
| 548 | { |
| 549 | int err = -EIO; |
| 550 | int offsets[4]; |
| 551 | Indirect chain[4]; |
| 552 | Indirect *partial; |
| 553 | unsigned long goal; |
| 554 | int left; |
| 555 | int boundary = 0; |
| 556 | int depth = ext2_block_to_path(inode, iblock, offsets, &boundary); |
| 557 | |
| 558 | if (depth == 0) |
| 559 | goto out; |
| 560 | |
| 561 | reread: |
| 562 | partial = ext2_get_branch(inode, depth, offsets, chain, &err); |
| 563 | |
| 564 | /* Simplest case - block found, no allocation needed */ |
| 565 | if (!partial) { |
| 566 | got_it: |
| 567 | map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); |
| 568 | if (boundary) |
| 569 | set_buffer_boundary(bh_result); |
| 570 | /* Clean up and exit */ |
| 571 | partial = chain+depth-1; /* the whole chain */ |
| 572 | goto cleanup; |
| 573 | } |
| 574 | |
| 575 | /* Next simple case - plain lookup or failed read of indirect block */ |
| 576 | if (!create || err == -EIO) { |
| 577 | cleanup: |
| 578 | while (partial > chain) { |
| 579 | brelse(partial->bh); |
| 580 | partial--; |
| 581 | } |
| 582 | out: |
| 583 | return err; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Indirect block might be removed by truncate while we were |
| 588 | * reading it. Handling of that case (forget what we've got and |
| 589 | * reread) is taken out of the main path. |
| 590 | */ |
| 591 | if (err == -EAGAIN) |
| 592 | goto changed; |
| 593 | |
| 594 | goal = 0; |
| 595 | if (ext2_find_goal(inode, iblock, chain, partial, &goal) < 0) |
| 596 | goto changed; |
| 597 | |
| 598 | left = (chain + depth) - partial; |
| 599 | err = ext2_alloc_branch(inode, left, goal, |
| 600 | offsets+(partial-chain), partial); |
| 601 | if (err) |
| 602 | goto cleanup; |
| 603 | |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 604 | if (ext2_use_xip(inode->i_sb)) { |
| 605 | /* |
| 606 | * we need to clear the block |
| 607 | */ |
| 608 | err = ext2_clear_xip_target (inode, |
| 609 | le32_to_cpu(chain[depth-1].key)); |
| 610 | if (err) |
| 611 | goto cleanup; |
| 612 | } |
| 613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | if (ext2_splice_branch(inode, iblock, chain, partial, left) < 0) |
| 615 | goto changed; |
| 616 | |
| 617 | set_buffer_new(bh_result); |
| 618 | goto got_it; |
| 619 | |
| 620 | changed: |
| 621 | while (partial > chain) { |
| 622 | brelse(partial->bh); |
| 623 | partial--; |
| 624 | } |
| 625 | goto reread; |
| 626 | } |
| 627 | |
| 628 | static int ext2_writepage(struct page *page, struct writeback_control *wbc) |
| 629 | { |
| 630 | return block_write_full_page(page, ext2_get_block, wbc); |
| 631 | } |
| 632 | |
| 633 | static int ext2_readpage(struct file *file, struct page *page) |
| 634 | { |
| 635 | return mpage_readpage(page, ext2_get_block); |
| 636 | } |
| 637 | |
| 638 | static int |
| 639 | ext2_readpages(struct file *file, struct address_space *mapping, |
| 640 | struct list_head *pages, unsigned nr_pages) |
| 641 | { |
| 642 | return mpage_readpages(mapping, pages, nr_pages, ext2_get_block); |
| 643 | } |
| 644 | |
| 645 | static int |
| 646 | ext2_prepare_write(struct file *file, struct page *page, |
| 647 | unsigned from, unsigned to) |
| 648 | { |
| 649 | return block_prepare_write(page,from,to,ext2_get_block); |
| 650 | } |
| 651 | |
| 652 | static int |
| 653 | ext2_nobh_prepare_write(struct file *file, struct page *page, |
| 654 | unsigned from, unsigned to) |
| 655 | { |
| 656 | return nobh_prepare_write(page,from,to,ext2_get_block); |
| 657 | } |
| 658 | |
| 659 | static int ext2_nobh_writepage(struct page *page, |
| 660 | struct writeback_control *wbc) |
| 661 | { |
| 662 | return nobh_writepage(page, ext2_get_block, wbc); |
| 663 | } |
| 664 | |
| 665 | static sector_t ext2_bmap(struct address_space *mapping, sector_t block) |
| 666 | { |
| 667 | return generic_block_bmap(mapping,block,ext2_get_block); |
| 668 | } |
| 669 | |
| 670 | static int |
| 671 | ext2_get_blocks(struct inode *inode, sector_t iblock, unsigned long max_blocks, |
| 672 | struct buffer_head *bh_result, int create) |
| 673 | { |
| 674 | int ret; |
| 675 | |
| 676 | ret = ext2_get_block(inode, iblock, bh_result, create); |
| 677 | if (ret == 0) |
| 678 | bh_result->b_size = (1 << inode->i_blkbits); |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | static ssize_t |
| 683 | ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, |
| 684 | loff_t offset, unsigned long nr_segs) |
| 685 | { |
| 686 | struct file *file = iocb->ki_filp; |
| 687 | struct inode *inode = file->f_mapping->host; |
| 688 | |
| 689 | return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, |
| 690 | offset, nr_segs, ext2_get_blocks, NULL); |
| 691 | } |
| 692 | |
| 693 | static int |
| 694 | ext2_writepages(struct address_space *mapping, struct writeback_control *wbc) |
| 695 | { |
| 696 | return mpage_writepages(mapping, wbc, ext2_get_block); |
| 697 | } |
| 698 | |
| 699 | struct address_space_operations ext2_aops = { |
| 700 | .readpage = ext2_readpage, |
| 701 | .readpages = ext2_readpages, |
| 702 | .writepage = ext2_writepage, |
| 703 | .sync_page = block_sync_page, |
| 704 | .prepare_write = ext2_prepare_write, |
| 705 | .commit_write = generic_commit_write, |
| 706 | .bmap = ext2_bmap, |
| 707 | .direct_IO = ext2_direct_IO, |
| 708 | .writepages = ext2_writepages, |
Christoph Lameter | e965f96 | 2006-02-01 03:05:41 -0800 | [diff] [blame] | 709 | .migratepage = buffer_migrate_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | }; |
| 711 | |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 712 | struct address_space_operations ext2_aops_xip = { |
| 713 | .bmap = ext2_bmap, |
| 714 | .get_xip_page = ext2_get_xip_page, |
| 715 | }; |
| 716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | struct address_space_operations ext2_nobh_aops = { |
| 718 | .readpage = ext2_readpage, |
| 719 | .readpages = ext2_readpages, |
| 720 | .writepage = ext2_nobh_writepage, |
| 721 | .sync_page = block_sync_page, |
| 722 | .prepare_write = ext2_nobh_prepare_write, |
| 723 | .commit_write = nobh_commit_write, |
| 724 | .bmap = ext2_bmap, |
| 725 | .direct_IO = ext2_direct_IO, |
| 726 | .writepages = ext2_writepages, |
Christoph Lameter | e965f96 | 2006-02-01 03:05:41 -0800 | [diff] [blame] | 727 | .migratepage = buffer_migrate_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | }; |
| 729 | |
| 730 | /* |
| 731 | * Probably it should be a library function... search for first non-zero word |
| 732 | * or memcmp with zero_page, whatever is better for particular architecture. |
| 733 | * Linus? |
| 734 | */ |
| 735 | static inline int all_zeroes(__le32 *p, __le32 *q) |
| 736 | { |
| 737 | while (p < q) |
| 738 | if (*p++) |
| 739 | return 0; |
| 740 | return 1; |
| 741 | } |
| 742 | |
| 743 | /** |
| 744 | * ext2_find_shared - find the indirect blocks for partial truncation. |
| 745 | * @inode: inode in question |
| 746 | * @depth: depth of the affected branch |
| 747 | * @offsets: offsets of pointers in that branch (see ext2_block_to_path) |
| 748 | * @chain: place to store the pointers to partial indirect blocks |
| 749 | * @top: place to the (detached) top of branch |
| 750 | * |
| 751 | * This is a helper function used by ext2_truncate(). |
| 752 | * |
| 753 | * When we do truncate() we may have to clean the ends of several indirect |
| 754 | * blocks but leave the blocks themselves alive. Block is partially |
| 755 | * truncated if some data below the new i_size is refered from it (and |
| 756 | * it is on the path to the first completely truncated data block, indeed). |
| 757 | * We have to free the top of that path along with everything to the right |
| 758 | * of the path. Since no allocation past the truncation point is possible |
| 759 | * until ext2_truncate() finishes, we may safely do the latter, but top |
| 760 | * of branch may require special attention - pageout below the truncation |
| 761 | * point might try to populate it. |
| 762 | * |
| 763 | * We atomically detach the top of branch from the tree, store the block |
| 764 | * number of its root in *@top, pointers to buffer_heads of partially |
| 765 | * truncated blocks - in @chain[].bh and pointers to their last elements |
| 766 | * that should not be removed - in @chain[].p. Return value is the pointer |
| 767 | * to last filled element of @chain. |
| 768 | * |
| 769 | * The work left to caller to do the actual freeing of subtrees: |
| 770 | * a) free the subtree starting from *@top |
| 771 | * b) free the subtrees whose roots are stored in |
| 772 | * (@chain[i].p+1 .. end of @chain[i].bh->b_data) |
| 773 | * c) free the subtrees growing from the inode past the @chain[0].p |
| 774 | * (no partially truncated stuff there). |
| 775 | */ |
| 776 | |
| 777 | static Indirect *ext2_find_shared(struct inode *inode, |
| 778 | int depth, |
| 779 | int offsets[4], |
| 780 | Indirect chain[4], |
| 781 | __le32 *top) |
| 782 | { |
| 783 | Indirect *partial, *p; |
| 784 | int k, err; |
| 785 | |
| 786 | *top = 0; |
| 787 | for (k = depth; k > 1 && !offsets[k-1]; k--) |
| 788 | ; |
| 789 | partial = ext2_get_branch(inode, k, offsets, chain, &err); |
| 790 | if (!partial) |
| 791 | partial = chain + k-1; |
| 792 | /* |
| 793 | * If the branch acquired continuation since we've looked at it - |
| 794 | * fine, it should all survive and (new) top doesn't belong to us. |
| 795 | */ |
| 796 | write_lock(&EXT2_I(inode)->i_meta_lock); |
| 797 | if (!partial->key && *partial->p) { |
| 798 | write_unlock(&EXT2_I(inode)->i_meta_lock); |
| 799 | goto no_top; |
| 800 | } |
| 801 | for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--) |
| 802 | ; |
| 803 | /* |
| 804 | * OK, we've found the last block that must survive. The rest of our |
| 805 | * branch should be detached before unlocking. However, if that rest |
| 806 | * of branch is all ours and does not grow immediately from the inode |
| 807 | * it's easier to cheat and just decrement partial->p. |
| 808 | */ |
| 809 | if (p == chain + k - 1 && p > chain) { |
| 810 | p->p--; |
| 811 | } else { |
| 812 | *top = *p->p; |
| 813 | *p->p = 0; |
| 814 | } |
| 815 | write_unlock(&EXT2_I(inode)->i_meta_lock); |
| 816 | |
| 817 | while(partial > p) |
| 818 | { |
| 819 | brelse(partial->bh); |
| 820 | partial--; |
| 821 | } |
| 822 | no_top: |
| 823 | return partial; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * ext2_free_data - free a list of data blocks |
| 828 | * @inode: inode we are dealing with |
| 829 | * @p: array of block numbers |
| 830 | * @q: points immediately past the end of array |
| 831 | * |
| 832 | * We are freeing all blocks refered from that array (numbers are |
| 833 | * stored as little-endian 32-bit) and updating @inode->i_blocks |
| 834 | * appropriately. |
| 835 | */ |
| 836 | static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q) |
| 837 | { |
| 838 | unsigned long block_to_free = 0, count = 0; |
| 839 | unsigned long nr; |
| 840 | |
| 841 | for ( ; p < q ; p++) { |
| 842 | nr = le32_to_cpu(*p); |
| 843 | if (nr) { |
| 844 | *p = 0; |
| 845 | /* accumulate blocks to free if they're contiguous */ |
| 846 | if (count == 0) |
| 847 | goto free_this; |
| 848 | else if (block_to_free == nr - count) |
| 849 | count++; |
| 850 | else { |
| 851 | mark_inode_dirty(inode); |
| 852 | ext2_free_blocks (inode, block_to_free, count); |
| 853 | free_this: |
| 854 | block_to_free = nr; |
| 855 | count = 1; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | if (count > 0) { |
| 860 | mark_inode_dirty(inode); |
| 861 | ext2_free_blocks (inode, block_to_free, count); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | /** |
| 866 | * ext2_free_branches - free an array of branches |
| 867 | * @inode: inode we are dealing with |
| 868 | * @p: array of block numbers |
| 869 | * @q: pointer immediately past the end of array |
| 870 | * @depth: depth of the branches to free |
| 871 | * |
| 872 | * We are freeing all blocks refered from these branches (numbers are |
| 873 | * stored as little-endian 32-bit) and updating @inode->i_blocks |
| 874 | * appropriately. |
| 875 | */ |
| 876 | static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth) |
| 877 | { |
| 878 | struct buffer_head * bh; |
| 879 | unsigned long nr; |
| 880 | |
| 881 | if (depth--) { |
| 882 | int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb); |
| 883 | for ( ; p < q ; p++) { |
| 884 | nr = le32_to_cpu(*p); |
| 885 | if (!nr) |
| 886 | continue; |
| 887 | *p = 0; |
| 888 | bh = sb_bread(inode->i_sb, nr); |
| 889 | /* |
| 890 | * A read failure? Report error and clear slot |
| 891 | * (should be rare). |
| 892 | */ |
| 893 | if (!bh) { |
| 894 | ext2_error(inode->i_sb, "ext2_free_branches", |
| 895 | "Read failure, inode=%ld, block=%ld", |
| 896 | inode->i_ino, nr); |
| 897 | continue; |
| 898 | } |
| 899 | ext2_free_branches(inode, |
| 900 | (__le32*)bh->b_data, |
| 901 | (__le32*)bh->b_data + addr_per_block, |
| 902 | depth); |
| 903 | bforget(bh); |
| 904 | ext2_free_blocks(inode, nr, 1); |
| 905 | mark_inode_dirty(inode); |
| 906 | } |
| 907 | } else |
| 908 | ext2_free_data(inode, p, q); |
| 909 | } |
| 910 | |
| 911 | void ext2_truncate (struct inode * inode) |
| 912 | { |
| 913 | __le32 *i_data = EXT2_I(inode)->i_data; |
| 914 | int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb); |
| 915 | int offsets[4]; |
| 916 | Indirect chain[4]; |
| 917 | Indirect *partial; |
| 918 | __le32 nr = 0; |
| 919 | int n; |
| 920 | long iblock; |
| 921 | unsigned blocksize; |
| 922 | |
| 923 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 924 | S_ISLNK(inode->i_mode))) |
| 925 | return; |
| 926 | if (ext2_inode_is_fast_symlink(inode)) |
| 927 | return; |
| 928 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 929 | return; |
| 930 | |
| 931 | ext2_discard_prealloc(inode); |
| 932 | |
| 933 | blocksize = inode->i_sb->s_blocksize; |
| 934 | iblock = (inode->i_size + blocksize-1) |
| 935 | >> EXT2_BLOCK_SIZE_BITS(inode->i_sb); |
| 936 | |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 937 | if (mapping_is_xip(inode->i_mapping)) |
| 938 | xip_truncate_page(inode->i_mapping, inode->i_size); |
| 939 | else if (test_opt(inode->i_sb, NOBH)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
| 941 | else |
| 942 | block_truncate_page(inode->i_mapping, |
| 943 | inode->i_size, ext2_get_block); |
| 944 | |
| 945 | n = ext2_block_to_path(inode, iblock, offsets, NULL); |
| 946 | if (n == 0) |
| 947 | return; |
| 948 | |
| 949 | if (n == 1) { |
| 950 | ext2_free_data(inode, i_data+offsets[0], |
| 951 | i_data + EXT2_NDIR_BLOCKS); |
| 952 | goto do_indirects; |
| 953 | } |
| 954 | |
| 955 | partial = ext2_find_shared(inode, n, offsets, chain, &nr); |
| 956 | /* Kill the top of shared branch (already detached) */ |
| 957 | if (nr) { |
| 958 | if (partial == chain) |
| 959 | mark_inode_dirty(inode); |
| 960 | else |
| 961 | mark_buffer_dirty_inode(partial->bh, inode); |
| 962 | ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial); |
| 963 | } |
| 964 | /* Clear the ends of indirect blocks on the shared branch */ |
| 965 | while (partial > chain) { |
| 966 | ext2_free_branches(inode, |
| 967 | partial->p + 1, |
| 968 | (__le32*)partial->bh->b_data+addr_per_block, |
| 969 | (chain+n-1) - partial); |
| 970 | mark_buffer_dirty_inode(partial->bh, inode); |
| 971 | brelse (partial->bh); |
| 972 | partial--; |
| 973 | } |
| 974 | do_indirects: |
| 975 | /* Kill the remaining (whole) subtrees */ |
| 976 | switch (offsets[0]) { |
| 977 | default: |
| 978 | nr = i_data[EXT2_IND_BLOCK]; |
| 979 | if (nr) { |
| 980 | i_data[EXT2_IND_BLOCK] = 0; |
| 981 | mark_inode_dirty(inode); |
| 982 | ext2_free_branches(inode, &nr, &nr+1, 1); |
| 983 | } |
| 984 | case EXT2_IND_BLOCK: |
| 985 | nr = i_data[EXT2_DIND_BLOCK]; |
| 986 | if (nr) { |
| 987 | i_data[EXT2_DIND_BLOCK] = 0; |
| 988 | mark_inode_dirty(inode); |
| 989 | ext2_free_branches(inode, &nr, &nr+1, 2); |
| 990 | } |
| 991 | case EXT2_DIND_BLOCK: |
| 992 | nr = i_data[EXT2_TIND_BLOCK]; |
| 993 | if (nr) { |
| 994 | i_data[EXT2_TIND_BLOCK] = 0; |
| 995 | mark_inode_dirty(inode); |
| 996 | ext2_free_branches(inode, &nr, &nr+1, 3); |
| 997 | } |
| 998 | case EXT2_TIND_BLOCK: |
| 999 | ; |
| 1000 | } |
| 1001 | inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; |
| 1002 | if (inode_needs_sync(inode)) { |
| 1003 | sync_mapping_buffers(inode->i_mapping); |
| 1004 | ext2_sync_inode (inode); |
| 1005 | } else { |
| 1006 | mark_inode_dirty(inode); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino, |
| 1011 | struct buffer_head **p) |
| 1012 | { |
| 1013 | struct buffer_head * bh; |
| 1014 | unsigned long block_group; |
| 1015 | unsigned long block; |
| 1016 | unsigned long offset; |
| 1017 | struct ext2_group_desc * gdp; |
| 1018 | |
| 1019 | *p = NULL; |
| 1020 | if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) || |
| 1021 | ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count)) |
| 1022 | goto Einval; |
| 1023 | |
| 1024 | block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb); |
| 1025 | gdp = ext2_get_group_desc(sb, block_group, &bh); |
| 1026 | if (!gdp) |
| 1027 | goto Egdp; |
| 1028 | /* |
| 1029 | * Figure out the offset within the block group inode table |
| 1030 | */ |
| 1031 | offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb); |
| 1032 | block = le32_to_cpu(gdp->bg_inode_table) + |
| 1033 | (offset >> EXT2_BLOCK_SIZE_BITS(sb)); |
| 1034 | if (!(bh = sb_bread(sb, block))) |
| 1035 | goto Eio; |
| 1036 | |
| 1037 | *p = bh; |
| 1038 | offset &= (EXT2_BLOCK_SIZE(sb) - 1); |
| 1039 | return (struct ext2_inode *) (bh->b_data + offset); |
| 1040 | |
| 1041 | Einval: |
| 1042 | ext2_error(sb, "ext2_get_inode", "bad inode number: %lu", |
| 1043 | (unsigned long) ino); |
| 1044 | return ERR_PTR(-EINVAL); |
| 1045 | Eio: |
| 1046 | ext2_error(sb, "ext2_get_inode", |
| 1047 | "unable to read inode block - inode=%lu, block=%lu", |
| 1048 | (unsigned long) ino, block); |
| 1049 | Egdp: |
| 1050 | return ERR_PTR(-EIO); |
| 1051 | } |
| 1052 | |
| 1053 | void ext2_set_inode_flags(struct inode *inode) |
| 1054 | { |
| 1055 | unsigned int flags = EXT2_I(inode)->i_flags; |
| 1056 | |
| 1057 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); |
| 1058 | if (flags & EXT2_SYNC_FL) |
| 1059 | inode->i_flags |= S_SYNC; |
| 1060 | if (flags & EXT2_APPEND_FL) |
| 1061 | inode->i_flags |= S_APPEND; |
| 1062 | if (flags & EXT2_IMMUTABLE_FL) |
| 1063 | inode->i_flags |= S_IMMUTABLE; |
| 1064 | if (flags & EXT2_NOATIME_FL) |
| 1065 | inode->i_flags |= S_NOATIME; |
| 1066 | if (flags & EXT2_DIRSYNC_FL) |
| 1067 | inode->i_flags |= S_DIRSYNC; |
| 1068 | } |
| 1069 | |
| 1070 | void ext2_read_inode (struct inode * inode) |
| 1071 | { |
| 1072 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 1073 | ino_t ino = inode->i_ino; |
| 1074 | struct buffer_head * bh; |
| 1075 | struct ext2_inode * raw_inode = ext2_get_inode(inode->i_sb, ino, &bh); |
| 1076 | int n; |
| 1077 | |
| 1078 | #ifdef CONFIG_EXT2_FS_POSIX_ACL |
| 1079 | ei->i_acl = EXT2_ACL_NOT_CACHED; |
| 1080 | ei->i_default_acl = EXT2_ACL_NOT_CACHED; |
| 1081 | #endif |
| 1082 | if (IS_ERR(raw_inode)) |
| 1083 | goto bad_inode; |
| 1084 | |
| 1085 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); |
| 1086 | inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); |
| 1087 | inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); |
| 1088 | if (!(test_opt (inode->i_sb, NO_UID32))) { |
| 1089 | inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; |
| 1090 | inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; |
| 1091 | } |
| 1092 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); |
| 1093 | inode->i_size = le32_to_cpu(raw_inode->i_size); |
| 1094 | inode->i_atime.tv_sec = le32_to_cpu(raw_inode->i_atime); |
| 1095 | inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->i_ctime); |
| 1096 | inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->i_mtime); |
| 1097 | inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0; |
| 1098 | ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); |
| 1099 | /* We now have enough fields to check if the inode was active or not. |
| 1100 | * This is needed because nfsd might try to access dead inodes |
| 1101 | * the test is that same one that e2fsck uses |
| 1102 | * NeilBrown 1999oct15 |
| 1103 | */ |
| 1104 | if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) { |
| 1105 | /* this inode is deleted */ |
| 1106 | brelse (bh); |
| 1107 | goto bad_inode; |
| 1108 | } |
| 1109 | inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */ |
| 1110 | inode->i_blocks = le32_to_cpu(raw_inode->i_blocks); |
| 1111 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); |
| 1112 | ei->i_faddr = le32_to_cpu(raw_inode->i_faddr); |
| 1113 | ei->i_frag_no = raw_inode->i_frag; |
| 1114 | ei->i_frag_size = raw_inode->i_fsize; |
| 1115 | ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); |
| 1116 | ei->i_dir_acl = 0; |
| 1117 | if (S_ISREG(inode->i_mode)) |
| 1118 | inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32; |
| 1119 | else |
| 1120 | ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl); |
| 1121 | ei->i_dtime = 0; |
| 1122 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); |
| 1123 | ei->i_state = 0; |
| 1124 | ei->i_next_alloc_block = 0; |
| 1125 | ei->i_next_alloc_goal = 0; |
| 1126 | ei->i_prealloc_count = 0; |
| 1127 | ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb); |
| 1128 | ei->i_dir_start_lookup = 0; |
| 1129 | |
| 1130 | /* |
| 1131 | * NOTE! The in-memory inode i_data array is in little-endian order |
| 1132 | * even on big-endian machines: we do NOT byteswap the block numbers! |
| 1133 | */ |
| 1134 | for (n = 0; n < EXT2_N_BLOCKS; n++) |
| 1135 | ei->i_data[n] = raw_inode->i_block[n]; |
| 1136 | |
| 1137 | if (S_ISREG(inode->i_mode)) { |
| 1138 | inode->i_op = &ext2_file_inode_operations; |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 1139 | if (ext2_use_xip(inode->i_sb)) { |
| 1140 | inode->i_mapping->a_ops = &ext2_aops_xip; |
| 1141 | inode->i_fop = &ext2_xip_file_operations; |
| 1142 | } else if (test_opt(inode->i_sb, NOBH)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | inode->i_mapping->a_ops = &ext2_nobh_aops; |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 1144 | inode->i_fop = &ext2_file_operations; |
| 1145 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | inode->i_mapping->a_ops = &ext2_aops; |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 1147 | inode->i_fop = &ext2_file_operations; |
| 1148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | } else if (S_ISDIR(inode->i_mode)) { |
| 1150 | inode->i_op = &ext2_dir_inode_operations; |
| 1151 | inode->i_fop = &ext2_dir_operations; |
| 1152 | if (test_opt(inode->i_sb, NOBH)) |
| 1153 | inode->i_mapping->a_ops = &ext2_nobh_aops; |
| 1154 | else |
| 1155 | inode->i_mapping->a_ops = &ext2_aops; |
| 1156 | } else if (S_ISLNK(inode->i_mode)) { |
| 1157 | if (ext2_inode_is_fast_symlink(inode)) |
| 1158 | inode->i_op = &ext2_fast_symlink_inode_operations; |
| 1159 | else { |
| 1160 | inode->i_op = &ext2_symlink_inode_operations; |
| 1161 | if (test_opt(inode->i_sb, NOBH)) |
| 1162 | inode->i_mapping->a_ops = &ext2_nobh_aops; |
| 1163 | else |
| 1164 | inode->i_mapping->a_ops = &ext2_aops; |
| 1165 | } |
| 1166 | } else { |
| 1167 | inode->i_op = &ext2_special_inode_operations; |
| 1168 | if (raw_inode->i_block[0]) |
| 1169 | init_special_inode(inode, inode->i_mode, |
| 1170 | old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); |
| 1171 | else |
| 1172 | init_special_inode(inode, inode->i_mode, |
| 1173 | new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); |
| 1174 | } |
| 1175 | brelse (bh); |
| 1176 | ext2_set_inode_flags(inode); |
| 1177 | return; |
| 1178 | |
| 1179 | bad_inode: |
| 1180 | make_bad_inode(inode); |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | static int ext2_update_inode(struct inode * inode, int do_sync) |
| 1185 | { |
| 1186 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 1187 | struct super_block *sb = inode->i_sb; |
| 1188 | ino_t ino = inode->i_ino; |
| 1189 | uid_t uid = inode->i_uid; |
| 1190 | gid_t gid = inode->i_gid; |
| 1191 | struct buffer_head * bh; |
| 1192 | struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh); |
| 1193 | int n; |
| 1194 | int err = 0; |
| 1195 | |
| 1196 | if (IS_ERR(raw_inode)) |
| 1197 | return -EIO; |
| 1198 | |
| 1199 | /* For fields not not tracking in the in-memory inode, |
| 1200 | * initialise them to zero for new inodes. */ |
| 1201 | if (ei->i_state & EXT2_STATE_NEW) |
| 1202 | memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size); |
| 1203 | |
| 1204 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); |
| 1205 | if (!(test_opt(sb, NO_UID32))) { |
| 1206 | raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid)); |
| 1207 | raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid)); |
| 1208 | /* |
| 1209 | * Fix up interoperability with old kernels. Otherwise, old inodes get |
| 1210 | * re-used with the upper 16 bits of the uid/gid intact |
| 1211 | */ |
| 1212 | if (!ei->i_dtime) { |
| 1213 | raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid)); |
| 1214 | raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid)); |
| 1215 | } else { |
| 1216 | raw_inode->i_uid_high = 0; |
| 1217 | raw_inode->i_gid_high = 0; |
| 1218 | } |
| 1219 | } else { |
| 1220 | raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid)); |
| 1221 | raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid)); |
| 1222 | raw_inode->i_uid_high = 0; |
| 1223 | raw_inode->i_gid_high = 0; |
| 1224 | } |
| 1225 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); |
| 1226 | raw_inode->i_size = cpu_to_le32(inode->i_size); |
| 1227 | raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec); |
| 1228 | raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec); |
| 1229 | raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec); |
| 1230 | |
| 1231 | raw_inode->i_blocks = cpu_to_le32(inode->i_blocks); |
| 1232 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); |
| 1233 | raw_inode->i_flags = cpu_to_le32(ei->i_flags); |
| 1234 | raw_inode->i_faddr = cpu_to_le32(ei->i_faddr); |
| 1235 | raw_inode->i_frag = ei->i_frag_no; |
| 1236 | raw_inode->i_fsize = ei->i_frag_size; |
| 1237 | raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl); |
| 1238 | if (!S_ISREG(inode->i_mode)) |
| 1239 | raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl); |
| 1240 | else { |
| 1241 | raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32); |
| 1242 | if (inode->i_size > 0x7fffffffULL) { |
| 1243 | if (!EXT2_HAS_RO_COMPAT_FEATURE(sb, |
| 1244 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE) || |
| 1245 | EXT2_SB(sb)->s_es->s_rev_level == |
| 1246 | cpu_to_le32(EXT2_GOOD_OLD_REV)) { |
| 1247 | /* If this is the first large file |
| 1248 | * created, add a flag to the superblock. |
| 1249 | */ |
| 1250 | lock_kernel(); |
| 1251 | ext2_update_dynamic_rev(sb); |
| 1252 | EXT2_SET_RO_COMPAT_FEATURE(sb, |
| 1253 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE); |
| 1254 | unlock_kernel(); |
| 1255 | ext2_write_super(sb); |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); |
| 1261 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
| 1262 | if (old_valid_dev(inode->i_rdev)) { |
| 1263 | raw_inode->i_block[0] = |
| 1264 | cpu_to_le32(old_encode_dev(inode->i_rdev)); |
| 1265 | raw_inode->i_block[1] = 0; |
| 1266 | } else { |
| 1267 | raw_inode->i_block[0] = 0; |
| 1268 | raw_inode->i_block[1] = |
| 1269 | cpu_to_le32(new_encode_dev(inode->i_rdev)); |
| 1270 | raw_inode->i_block[2] = 0; |
| 1271 | } |
| 1272 | } else for (n = 0; n < EXT2_N_BLOCKS; n++) |
| 1273 | raw_inode->i_block[n] = ei->i_data[n]; |
| 1274 | mark_buffer_dirty(bh); |
| 1275 | if (do_sync) { |
| 1276 | sync_dirty_buffer(bh); |
| 1277 | if (buffer_req(bh) && !buffer_uptodate(bh)) { |
| 1278 | printk ("IO error syncing ext2 inode [%s:%08lx]\n", |
| 1279 | sb->s_id, (unsigned long) ino); |
| 1280 | err = -EIO; |
| 1281 | } |
| 1282 | } |
| 1283 | ei->i_state &= ~EXT2_STATE_NEW; |
| 1284 | brelse (bh); |
| 1285 | return err; |
| 1286 | } |
| 1287 | |
| 1288 | int ext2_write_inode(struct inode *inode, int wait) |
| 1289 | { |
| 1290 | return ext2_update_inode(inode, wait); |
| 1291 | } |
| 1292 | |
| 1293 | int ext2_sync_inode(struct inode *inode) |
| 1294 | { |
| 1295 | struct writeback_control wbc = { |
| 1296 | .sync_mode = WB_SYNC_ALL, |
| 1297 | .nr_to_write = 0, /* sys_fsync did this */ |
| 1298 | }; |
| 1299 | return sync_inode(inode, &wbc); |
| 1300 | } |
| 1301 | |
| 1302 | int ext2_setattr(struct dentry *dentry, struct iattr *iattr) |
| 1303 | { |
| 1304 | struct inode *inode = dentry->d_inode; |
| 1305 | int error; |
| 1306 | |
| 1307 | error = inode_change_ok(inode, iattr); |
| 1308 | if (error) |
| 1309 | return error; |
| 1310 | if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) || |
| 1311 | (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) { |
| 1312 | error = DQUOT_TRANSFER(inode, iattr) ? -EDQUOT : 0; |
| 1313 | if (error) |
| 1314 | return error; |
| 1315 | } |
| 1316 | error = inode_setattr(inode, iattr); |
| 1317 | if (!error && (iattr->ia_valid & ATTR_MODE)) |
| 1318 | error = ext2_acl_chmod(inode); |
| 1319 | return error; |
| 1320 | } |