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