blob: 527c46d9bc1f035148ab5cb8598954feac481ede [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/time.h>
26#include <linux/highuid.h>
27#include <linux/pagemap.h>
28#include <linux/quotaops.h>
29#include <linux/module.h>
30#include <linux/writeback.h>
31#include <linux/buffer_head.h>
32#include <linux/mpage.h>
Josef Bacik68c9d702008-10-03 17:32:43 -040033#include <linux/fiemap.h>
Duane Griffin8d6d0c42008-12-19 20:47:13 +000034#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "ext2.h"
36#include "acl.h"
Carsten Otte6d791252005-06-23 22:05:26 -070037#include "xip.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39MODULE_AUTHOR("Remy Card and others");
40MODULE_DESCRIPTION("Second Extended Filesystem");
41MODULE_LICENSE("GPL");
42
Christoph Hellwiga9185b42010-03-05 09:21:37 +010043static int __ext2_write_inode(struct inode *inode, int do_sync);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Test whether an inode is a fast symlink.
47 */
48static inline int ext2_inode_is_fast_symlink(struct inode *inode)
49{
50 int ea_blocks = EXT2_I(inode)->i_file_acl ?
51 (inode->i_sb->s_blocksize >> 9) : 0;
52
53 return (S_ISLNK(inode->i_mode) &&
54 inode->i_blocks - ea_blocks == 0);
55}
56
57/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * Called at the last iput() if i_nlink is zero.
59 */
60void ext2_delete_inode (struct inode * inode)
61{
Christoph Hellwig907f4552010-03-03 09:05:06 -050062 if (!is_bad_inode(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -050063 dquot_initialize(inode);
Mark Fashehfef26652005-09-09 13:01:31 -070064 truncate_inode_pages(&inode->i_data, 0);
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (is_bad_inode(inode))
67 goto no_delete;
68 EXT2_I(inode)->i_dtime = get_seconds();
69 mark_inode_dirty(inode);
Christoph Hellwiga9185b42010-03-05 09:21:37 +010070 __ext2_write_inode(inode, inode_needs_sync(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 inode->i_size = 0;
73 if (inode->i_blocks)
74 ext2_truncate (inode);
75 ext2_free_inode (inode);
76
77 return;
78no_delete:
79 clear_inode(inode); /* We must guarantee clearing of inode... */
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082typedef struct {
83 __le32 *p;
84 __le32 key;
85 struct buffer_head *bh;
86} Indirect;
87
88static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
89{
90 p->key = *(p->p = v);
91 p->bh = bh;
92}
93
94static inline int verify_chain(Indirect *from, Indirect *to)
95{
96 while (from <= to && from->key == *from->p)
97 from++;
98 return (from > to);
99}
100
101/**
102 * ext2_block_to_path - parse the block number into array of offsets
103 * @inode: inode in question (we are only interested in its superblock)
104 * @i_block: block number to be parsed
105 * @offsets: array to store the offsets in
106 * @boundary: set this non-zero if the referred-to block is likely to be
107 * followed (on disk) by an indirect block.
108 * To store the locations of file's data ext2 uses a data structure common
109 * for UNIX filesystems - tree of pointers anchored in the inode, with
110 * data blocks at leaves and indirect blocks in intermediate nodes.
111 * This function translates the block number into path in that tree -
112 * return value is the path length and @offsets[n] is the offset of
113 * pointer to (n+1)th node in the nth one. If @block is out of range
114 * (negative or too large) warning is printed and zero returned.
115 *
116 * Note: function doesn't find node addresses, so no IO is needed. All
117 * we need to know is the capacity of indirect blocks (taken from the
118 * inode->i_sb).
119 */
120
121/*
122 * Portability note: the last comparison (check that we fit into triple
123 * indirect block) is spelled differently, because otherwise on an
124 * architecture with 32-bit longs and 8Kb pages we might get into trouble
125 * if our filesystem had 8Kb blocks. We might use long long, but that would
126 * kill us on x86. Oh, well, at least the sign propagation does not matter -
127 * i_block would have to be negative in the very beginning, so we would not
128 * get there at all.
129 */
130
131static int ext2_block_to_path(struct inode *inode,
132 long i_block, int offsets[4], int *boundary)
133{
134 int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
135 int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
136 const long direct_blocks = EXT2_NDIR_BLOCKS,
137 indirect_blocks = ptrs,
138 double_blocks = (1 << (ptrs_bits * 2));
139 int n = 0;
140 int final = 0;
141
142 if (i_block < 0) {
Alexey Fisher2314b072009-11-19 19:12:51 +0100143 ext2_msg(inode->i_sb, KERN_WARNING,
144 "warning: %s: block < 0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 } else if (i_block < direct_blocks) {
146 offsets[n++] = i_block;
147 final = direct_blocks;
148 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
149 offsets[n++] = EXT2_IND_BLOCK;
150 offsets[n++] = i_block;
151 final = ptrs;
152 } else if ((i_block -= indirect_blocks) < double_blocks) {
153 offsets[n++] = EXT2_DIND_BLOCK;
154 offsets[n++] = i_block >> ptrs_bits;
155 offsets[n++] = i_block & (ptrs - 1);
156 final = ptrs;
157 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
158 offsets[n++] = EXT2_TIND_BLOCK;
159 offsets[n++] = i_block >> (ptrs_bits * 2);
160 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
161 offsets[n++] = i_block & (ptrs - 1);
162 final = ptrs;
163 } else {
Alexey Fisher2314b072009-11-19 19:12:51 +0100164 ext2_msg(inode->i_sb, KERN_WARNING,
165 "warning: %s: block is too big", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 if (boundary)
Martin J. Bligha686cd82007-10-16 23:30:46 -0700168 *boundary = final - 1 - (i_block & (ptrs - 1));
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return n;
171}
172
173/**
174 * ext2_get_branch - read the chain of indirect blocks leading to data
175 * @inode: inode in question
176 * @depth: depth of the chain (1 - direct pointer, etc.)
177 * @offsets: offsets of pointers in inode/indirect blocks
178 * @chain: place to store the result
179 * @err: here we store the error value
180 *
181 * Function fills the array of triples <key, p, bh> and returns %NULL
182 * if everything went OK or the pointer to the last filled triple
183 * (incomplete one) otherwise. Upon the return chain[i].key contains
184 * the number of (i+1)-th block in the chain (as it is stored in memory,
185 * i.e. little-endian 32-bit), chain[i].p contains the address of that
186 * number (it points into struct inode for i==0 and into the bh->b_data
187 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
188 * block for i>0 and NULL for i==0. In other words, it holds the block
189 * numbers of the chain, addresses they were taken from (and where we can
190 * verify that chain did not change) and buffer_heads hosting these
191 * numbers.
192 *
193 * Function stops when it stumbles upon zero pointer (absent block)
194 * (pointer to last triple returned, *@err == 0)
195 * or when it gets an IO error reading an indirect block
196 * (ditto, *@err == -EIO)
197 * or when it notices that chain had been changed while it was reading
198 * (ditto, *@err == -EAGAIN)
199 * or when it reads all @depth-1 indirect blocks successfully and finds
200 * the whole chain, all way to the data (returns %NULL, *err == 0).
201 */
202static Indirect *ext2_get_branch(struct inode *inode,
203 int depth,
204 int *offsets,
205 Indirect chain[4],
206 int *err)
207{
208 struct super_block *sb = inode->i_sb;
209 Indirect *p = chain;
210 struct buffer_head *bh;
211
212 *err = 0;
213 /* i_data is not going away, no lock needed */
214 add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets);
215 if (!p->key)
216 goto no_block;
217 while (--depth) {
218 bh = sb_bread(sb, le32_to_cpu(p->key));
219 if (!bh)
220 goto failure;
221 read_lock(&EXT2_I(inode)->i_meta_lock);
222 if (!verify_chain(chain, p))
223 goto changed;
224 add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
225 read_unlock(&EXT2_I(inode)->i_meta_lock);
226 if (!p->key)
227 goto no_block;
228 }
229 return NULL;
230
231changed:
232 read_unlock(&EXT2_I(inode)->i_meta_lock);
233 brelse(bh);
234 *err = -EAGAIN;
235 goto no_block;
236failure:
237 *err = -EIO;
238no_block:
239 return p;
240}
241
242/**
243 * ext2_find_near - find a place for allocation with sufficient locality
244 * @inode: owner
245 * @ind: descriptor of indirect block.
246 *
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000247 * This function returns the preferred place for block allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * It is used when heuristic for sequential allocation fails.
249 * Rules are:
250 * + if there is a block to the left of our position - allocate near it.
251 * + if pointer will live in indirect block - allocate near that block.
252 * + if pointer will live in inode - allocate in the same cylinder group.
253 *
254 * In the latter case we colour the starting block by the callers PID to
255 * prevent it from clashing with concurrent allocations for a different inode
256 * in the same block group. The PID is used here so that functionally related
257 * files will be close-by on-disk.
258 *
259 * Caller must make sure that @ind is valid and will stay that way.
260 */
261
Akinobu Mita4c8b3122008-04-28 02:16:02 -0700262static ext2_fsblk_t ext2_find_near(struct inode *inode, Indirect *ind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct ext2_inode_info *ei = EXT2_I(inode);
265 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
266 __le32 *p;
Akinobu Mita4c8b3122008-04-28 02:16:02 -0700267 ext2_fsblk_t bg_start;
268 ext2_fsblk_t colour;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /* Try to find previous block */
271 for (p = ind->p - 1; p >= start; p--)
272 if (*p)
273 return le32_to_cpu(*p);
274
275 /* No such thing, so let's try location of indirect block */
276 if (ind->bh)
277 return ind->bh->b_blocknr;
278
279 /*
280 * It is going to be refered from inode itself? OK, just put it into
281 * the same cylinder group then.
282 */
Akinobu Mita24097d12008-04-28 02:16:01 -0700283 bg_start = ext2_group_first_block_no(inode->i_sb, ei->i_block_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 colour = (current->pid % 16) *
285 (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16);
286 return bg_start + colour;
287}
288
289/**
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000290 * ext2_find_goal - find a preferred place for allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * @inode: owner
292 * @block: block we want
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * @partial: pointer to the last triple within a chain
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 *
Martin J. Bligha686cd82007-10-16 23:30:46 -0700295 * Returns preferred place for a block (the goal).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
297
Akinobu Mita4c8b3122008-04-28 02:16:02 -0700298static inline ext2_fsblk_t ext2_find_goal(struct inode *inode, long block,
299 Indirect *partial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Martin J. Bligha686cd82007-10-16 23:30:46 -0700301 struct ext2_block_alloc_info *block_i;
302
303 block_i = EXT2_I(inode)->i_block_alloc_info;
304
305 /*
306 * try the heuristic for sequential allocation,
307 * failing that at least try to get decent locality.
308 */
309 if (block_i && (block == block_i->last_alloc_logical_block + 1)
310 && (block_i->last_alloc_physical_block != 0)) {
311 return block_i->last_alloc_physical_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Martin J. Bligha686cd82007-10-16 23:30:46 -0700313
314 return ext2_find_near(inode, partial);
315}
316
317/**
318 * ext2_blks_to_allocate: Look up the block map and count the number
319 * of direct blocks need to be allocated for the given branch.
320 *
321 * @branch: chain of indirect blocks
322 * @k: number of blocks need for indirect blocks
323 * @blks: number of data blocks to be mapped.
324 * @blocks_to_boundary: the offset in the indirect block
325 *
326 * return the total number of blocks to be allocate, including the
327 * direct and indirect blocks.
328 */
329static int
330ext2_blks_to_allocate(Indirect * branch, int k, unsigned long blks,
331 int blocks_to_boundary)
332{
333 unsigned long count = 0;
334
335 /*
336 * Simple case, [t,d]Indirect block(s) has not allocated yet
337 * then it's clear blocks on that path have not allocated
338 */
339 if (k > 0) {
340 /* right now don't hanel cross boundary allocation */
341 if (blks < blocks_to_boundary + 1)
342 count += blks;
343 else
344 count += blocks_to_boundary + 1;
345 return count;
346 }
347
348 count++;
349 while (count < blks && count <= blocks_to_boundary
350 && le32_to_cpu(*(branch[0].p + count)) == 0) {
351 count++;
352 }
353 return count;
354}
355
356/**
357 * ext2_alloc_blocks: multiple allocate blocks needed for a branch
358 * @indirect_blks: the number of blocks need to allocate for indirect
359 * blocks
360 *
361 * @new_blocks: on return it will store the new block numbers for
362 * the indirect blocks(if needed) and the first direct block,
363 * @blks: on return it will store the total number of allocated
364 * direct blocks
365 */
366static int ext2_alloc_blocks(struct inode *inode,
367 ext2_fsblk_t goal, int indirect_blks, int blks,
368 ext2_fsblk_t new_blocks[4], int *err)
369{
370 int target, i;
371 unsigned long count = 0;
372 int index = 0;
373 ext2_fsblk_t current_block = 0;
374 int ret = 0;
375
376 /*
377 * Here we try to allocate the requested multiple blocks at once,
378 * on a best-effort basis.
379 * To build a branch, we should allocate blocks for
380 * the indirect blocks(if not allocated yet), and at least
381 * the first direct block of this branch. That's the
382 * minimum number of blocks need to allocate(required)
383 */
384 target = blks + indirect_blks;
385
386 while (1) {
387 count = target;
388 /* allocating blocks for indirect blocks and direct blocks */
389 current_block = ext2_new_blocks(inode,goal,&count,err);
390 if (*err)
391 goto failed_out;
392
393 target -= count;
394 /* allocate blocks for indirect blocks */
395 while (index < indirect_blks && count) {
396 new_blocks[index++] = current_block++;
397 count--;
398 }
399
400 if (count > 0)
401 break;
402 }
403
404 /* save the new block number for the first direct block */
405 new_blocks[index] = current_block;
406
407 /* total number of blocks allocated for direct blocks */
408 ret = count;
409 *err = 0;
410 return ret;
411failed_out:
412 for (i = 0; i <index; i++)
413 ext2_free_blocks(inode, new_blocks[i], 1);
414 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417/**
418 * ext2_alloc_branch - allocate and set up a chain of blocks.
419 * @inode: owner
420 * @num: depth of the chain (number of blocks to allocate)
421 * @offsets: offsets (in the blocks) to store the pointers to next.
422 * @branch: place to store the chain in.
423 *
424 * This function allocates @num blocks, zeroes out all but the last one,
425 * links them into chain and (if we are synchronous) writes them to disk.
426 * In other words, it prepares a branch that can be spliced onto the
427 * inode. It stores the information about that chain in the branch[], in
428 * the same format as ext2_get_branch() would do. We are calling it after
429 * we had read the existing part of chain and partial points to the last
430 * triple of that (one with zero ->key). Upon the exit we have the same
431 * picture as after the successful ext2_get_block(), excpet that in one
432 * place chain is disconnected - *branch->p is still zero (we did not
433 * set the last link), but branch->key contains the number that should
434 * be placed into *branch->p to fill that gap.
435 *
436 * If allocation fails we free all blocks we've allocated (and forget
437 * their buffer_heads) and return the error value the from failed
438 * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
439 * as described above and return 0.
440 */
441
442static int ext2_alloc_branch(struct inode *inode,
Martin J. Bligha686cd82007-10-16 23:30:46 -0700443 int indirect_blks, int *blks, ext2_fsblk_t goal,
444 int *offsets, Indirect *branch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 int blocksize = inode->i_sb->s_blocksize;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700447 int i, n = 0;
448 int err = 0;
449 struct buffer_head *bh;
450 int num;
451 ext2_fsblk_t new_blocks[4];
452 ext2_fsblk_t current_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Martin J. Bligha686cd82007-10-16 23:30:46 -0700454 num = ext2_alloc_blocks(inode, goal, indirect_blks,
455 *blks, new_blocks, &err);
456 if (err)
457 return err;
458
459 branch[0].key = cpu_to_le32(new_blocks[0]);
460 /*
461 * metadata blocks and data blocks are allocated.
462 */
463 for (n = 1; n <= indirect_blks; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
Martin J. Bligha686cd82007-10-16 23:30:46 -0700465 * Get buffer_head for parent block, zero it out
466 * and set the pointer to new one, then send
467 * parent to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
Martin J. Bligha686cd82007-10-16 23:30:46 -0700469 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
470 branch[n].bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 lock_buffer(bh);
472 memset(bh->b_data, 0, blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 branch[n].p = (__le32 *) bh->b_data + offsets[n];
Martin J. Bligha686cd82007-10-16 23:30:46 -0700474 branch[n].key = cpu_to_le32(new_blocks[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *branch[n].p = branch[n].key;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700476 if ( n == indirect_blks) {
477 current_block = new_blocks[n];
478 /*
479 * End of chain, update the last new metablock of
480 * the chain to point to the new allocated
481 * data blocks numbers
482 */
483 for (i=1; i < num; i++)
484 *(branch[n].p + i) = cpu_to_le32(++current_block);
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 set_buffer_uptodate(bh);
487 unlock_buffer(bh);
488 mark_buffer_dirty_inode(bh, inode);
489 /* We used to sync bh here if IS_SYNC(inode).
Jan Karaa2a735a2009-08-18 17:54:11 +0200490 * But we now rely upon generic_write_sync()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * and b_inode_buffers. But not for directories.
492 */
493 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
494 sync_dirty_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Martin J. Bligha686cd82007-10-16 23:30:46 -0700496 *blks = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return err;
498}
499
500/**
Martin J. Bligha686cd82007-10-16 23:30:46 -0700501 * ext2_splice_branch - splice the allocated branch onto inode.
502 * @inode: owner
503 * @block: (logical) number of block we are adding
Martin J. Bligha686cd82007-10-16 23:30:46 -0700504 * @where: location of missing link
505 * @num: number of indirect blocks we are adding
506 * @blks: number of direct blocks we are adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 *
Martin J. Bligha686cd82007-10-16 23:30:46 -0700508 * This function fills the missing link and does all housekeeping needed in
509 * inode (->i_blocks, etc.). In case of success we end up with the full
510 * chain to new block and return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Martin J. Bligha686cd82007-10-16 23:30:46 -0700512static void ext2_splice_branch(struct inode *inode,
513 long block, Indirect *where, int num, int blks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int i;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700516 struct ext2_block_alloc_info *block_i;
517 ext2_fsblk_t current_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Martin J. Bligha686cd82007-10-16 23:30:46 -0700519 block_i = EXT2_I(inode)->i_block_alloc_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Martin J. Bligha686cd82007-10-16 23:30:46 -0700521 /* XXX LOCKING probably should have i_meta_lock ?*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* That's it */
523
524 *where->p = where->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Martin J. Bligha686cd82007-10-16 23:30:46 -0700526 /*
527 * Update the host buffer_head or inode to point to more just allocated
528 * direct blocks blocks
529 */
530 if (num == 0 && blks > 1) {
531 current_block = le32_to_cpu(where->key) + 1;
532 for (i = 1; i < blks; i++)
533 *(where->p + i ) = cpu_to_le32(current_block++);
534 }
535
536 /*
537 * update the most recently allocated logical & physical block
538 * in i_block_alloc_info, to assist find the proper goal block for next
539 * allocation
540 */
541 if (block_i) {
542 block_i->last_alloc_logical_block = block + blks - 1;
543 block_i->last_alloc_physical_block =
544 le32_to_cpu(where[num].key) + blks - 1;
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* We are done with atomic stuff, now do the rest of housekeeping */
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* had we spliced it onto indirect block? */
550 if (where->bh)
551 mark_buffer_dirty_inode(where->bh, inode);
552
Martin J. Bligha686cd82007-10-16 23:30:46 -0700553 inode->i_ctime = CURRENT_TIME_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557/*
558 * Allocation strategy is simple: if we have to allocate something, we will
559 * have to go the whole way to leaf. So let's do it before attaching anything
560 * to tree, set linkage between the newborn blocks, write them if sync is
561 * required, recheck the path, free and repeat if check fails, otherwise
562 * set the last missing link (that will protect us from any truncate-generated
563 * removals - all blocks on the path are immune now) and possibly force the
564 * write on the parent block.
565 * That has a nice additional property: no special recovery from the failed
566 * allocations is needed - we simply release blocks and do not touch anything
567 * reachable from inode.
Martin J. Bligha686cd82007-10-16 23:30:46 -0700568 *
569 * `handle' can be NULL if create == 0.
570 *
Martin J. Bligha686cd82007-10-16 23:30:46 -0700571 * return > 0, # of blocks mapped or allocated.
572 * return = 0, if plain lookup failed.
573 * return < 0, error case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
Martin J. Bligha686cd82007-10-16 23:30:46 -0700575static int ext2_get_blocks(struct inode *inode,
576 sector_t iblock, unsigned long maxblocks,
577 struct buffer_head *bh_result,
578 int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 int err = -EIO;
581 int offsets[4];
582 Indirect chain[4];
583 Indirect *partial;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700584 ext2_fsblk_t goal;
585 int indirect_blks;
586 int blocks_to_boundary = 0;
587 int depth;
588 struct ext2_inode_info *ei = EXT2_I(inode);
589 int count = 0;
590 ext2_fsblk_t first_block = 0;
591
592 depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (depth == 0)
Martin J. Bligha686cd82007-10-16 23:30:46 -0700595 return (err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Jan Kara316cb4ef32009-04-13 14:40:14 -0700597 partial = ext2_get_branch(inode, depth, offsets, chain, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* Simplest case - block found, no allocation needed */
599 if (!partial) {
Martin J. Bligha686cd82007-10-16 23:30:46 -0700600 first_block = le32_to_cpu(chain[depth - 1].key);
601 clear_buffer_new(bh_result); /* What's this do? */
602 count++;
603 /*map more blocks*/
604 while (count < maxblocks && count <= blocks_to_boundary) {
605 ext2_fsblk_t blk;
606
Jan Kara316cb4ef32009-04-13 14:40:14 -0700607 if (!verify_chain(chain, chain + depth - 1)) {
Martin J. Bligha686cd82007-10-16 23:30:46 -0700608 /*
609 * Indirect block might be removed by
610 * truncate while we were reading it.
611 * Handling of that case: forget what we've
612 * got now, go to reread.
613 */
Jan Kara316cb4ef32009-04-13 14:40:14 -0700614 err = -EAGAIN;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700615 count = 0;
Jan Kara316cb4ef32009-04-13 14:40:14 -0700616 break;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700617 }
618 blk = le32_to_cpu(*(chain[depth-1].p + count));
619 if (blk == first_block + count)
620 count++;
621 else
622 break;
623 }
Jan Kara316cb4ef32009-04-13 14:40:14 -0700624 if (err != -EAGAIN)
625 goto got_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 /* Next simple case - plain lookup or failed read of indirect block */
Martin J. Bligha686cd82007-10-16 23:30:46 -0700629 if (!create || err == -EIO)
630 goto cleanup;
631
632 mutex_lock(&ei->truncate_mutex);
Jan Kara316cb4ef32009-04-13 14:40:14 -0700633 /*
634 * If the indirect block is missing while we are reading
635 * the chain(ext3_get_branch() returns -EAGAIN err), or
636 * if the chain has been changed after we grab the semaphore,
637 * (either because another process truncated this branch, or
638 * another get_block allocated this branch) re-grab the chain to see if
639 * the request block has been allocated or not.
640 *
641 * Since we already block the truncate/other get_block
642 * at this point, we will have the current copy of the chain when we
643 * splice the branch into the tree.
644 */
645 if (err == -EAGAIN || !verify_chain(chain, partial)) {
646 while (partial > chain) {
647 brelse(partial->bh);
648 partial--;
649 }
650 partial = ext2_get_branch(inode, depth, offsets, chain, &err);
651 if (!partial) {
652 count++;
653 mutex_unlock(&ei->truncate_mutex);
654 if (err)
655 goto cleanup;
656 clear_buffer_new(bh_result);
657 goto got_it;
658 }
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /*
Martin J. Bligha686cd82007-10-16 23:30:46 -0700662 * Okay, we need to do block allocation. Lazily initialize the block
663 * allocation info here if necessary
664 */
665 if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
666 ext2_init_block_alloc_info(inode);
667
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800668 goal = ext2_find_goal(inode, iblock, partial);
Martin J. Bligha686cd82007-10-16 23:30:46 -0700669
670 /* the number of blocks need to allocate for [d,t]indirect blocks */
671 indirect_blks = (chain + depth) - partial - 1;
672 /*
673 * Next look up the indirect map to count the totoal number of
674 * direct blocks to allocate for this branch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
Martin J. Bligha686cd82007-10-16 23:30:46 -0700676 count = ext2_blks_to_allocate(partial, indirect_blks,
677 maxblocks, blocks_to_boundary);
678 /*
679 * XXX ???? Block out ext2_truncate while we alter the tree
680 */
681 err = ext2_alloc_branch(inode, indirect_blks, &count, goal,
682 offsets + (partial - chain), partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Martin J. Bligha686cd82007-10-16 23:30:46 -0700684 if (err) {
685 mutex_unlock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto cleanup;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Carsten Otte6d791252005-06-23 22:05:26 -0700689 if (ext2_use_xip(inode->i_sb)) {
690 /*
691 * we need to clear the block
692 */
693 err = ext2_clear_xip_target (inode,
694 le32_to_cpu(chain[depth-1].key));
Martin J. Bligha686cd82007-10-16 23:30:46 -0700695 if (err) {
696 mutex_unlock(&ei->truncate_mutex);
Carsten Otte6d791252005-06-23 22:05:26 -0700697 goto cleanup;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700698 }
Carsten Otte6d791252005-06-23 22:05:26 -0700699 }
700
Martin J. Bligha686cd82007-10-16 23:30:46 -0700701 ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
702 mutex_unlock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 set_buffer_new(bh_result);
Martin J. Bligha686cd82007-10-16 23:30:46 -0700704got_it:
705 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
706 if (count > blocks_to_boundary)
707 set_buffer_boundary(bh_result);
708 err = count;
709 /* Clean up and exit */
710 partial = chain + depth - 1; /* the whole chain */
711cleanup:
712 while (partial > chain) {
713 brelse(partial->bh);
714 partial--;
715 }
716 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Martin J. Bligha686cd82007-10-16 23:30:46 -0700719int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
720{
721 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
722 int ret = ext2_get_blocks(inode, iblock, max_blocks,
723 bh_result, create);
724 if (ret > 0) {
725 bh_result->b_size = (ret << inode->i_blkbits);
726 ret = 0;
727 }
728 return ret;
729
730}
731
Josef Bacik68c9d702008-10-03 17:32:43 -0400732int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
733 u64 start, u64 len)
734{
735 return generic_block_fiemap(inode, fieinfo, start, len,
736 ext2_get_block);
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739static int ext2_writepage(struct page *page, struct writeback_control *wbc)
740{
741 return block_write_full_page(page, ext2_get_block, wbc);
742}
743
744static int ext2_readpage(struct file *file, struct page *page)
745{
746 return mpage_readpage(page, ext2_get_block);
747}
748
749static int
750ext2_readpages(struct file *file, struct address_space *mapping,
751 struct list_head *pages, unsigned nr_pages)
752{
753 return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
754}
755
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700756int __ext2_write_begin(struct file *file, struct address_space *mapping,
757 loff_t pos, unsigned len, unsigned flags,
758 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700760 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
761 ext2_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764static int
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700765ext2_write_begin(struct file *file, struct address_space *mapping,
766 loff_t pos, unsigned len, unsigned flags,
767 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700769 *pagep = NULL;
770 return __ext2_write_begin(file, mapping, pos, len, flags, pagep,fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Nick Piggin03158cd2007-10-16 01:25:25 -0700773static int
774ext2_nobh_write_begin(struct file *file, struct address_space *mapping,
775 loff_t pos, unsigned len, unsigned flags,
776 struct page **pagep, void **fsdata)
777{
778 /*
779 * Dir-in-pagecache still uses ext2_write_begin. Would have to rework
780 * directory handling code to pass around offsets rather than struct
781 * pages in order to make this work easily.
782 */
783 return nobh_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
784 ext2_get_block);
785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787static int ext2_nobh_writepage(struct page *page,
788 struct writeback_control *wbc)
789{
790 return nobh_writepage(page, ext2_get_block, wbc);
791}
792
793static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
794{
795 return generic_block_bmap(mapping,block,ext2_get_block);
796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798static ssize_t
799ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
800 loff_t offset, unsigned long nr_segs)
801{
802 struct file *file = iocb->ki_filp;
803 struct inode *inode = file->f_mapping->host;
804
805 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800806 offset, nr_segs, ext2_get_block, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809static int
810ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
811{
812 return mpage_writepages(mapping, wbc, ext2_get_block);
813}
814
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700815const struct address_space_operations ext2_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .readpage = ext2_readpage,
817 .readpages = ext2_readpages,
818 .writepage = ext2_writepage,
819 .sync_page = block_sync_page,
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700820 .write_begin = ext2_write_begin,
821 .write_end = generic_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 .bmap = ext2_bmap,
823 .direct_IO = ext2_direct_IO,
824 .writepages = ext2_writepages,
Christoph Lametere965f962006-02-01 03:05:41 -0800825 .migratepage = buffer_migrate_page,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -0700826 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +0200827 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828};
829
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700830const struct address_space_operations ext2_aops_xip = {
Carsten Otte6d791252005-06-23 22:05:26 -0700831 .bmap = ext2_bmap,
Nick Piggin70688e42008-04-28 02:13:02 -0700832 .get_xip_mem = ext2_get_xip_mem,
Carsten Otte6d791252005-06-23 22:05:26 -0700833};
834
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700835const struct address_space_operations ext2_nobh_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 .readpage = ext2_readpage,
837 .readpages = ext2_readpages,
838 .writepage = ext2_nobh_writepage,
839 .sync_page = block_sync_page,
Nick Piggin03158cd2007-10-16 01:25:25 -0700840 .write_begin = ext2_nobh_write_begin,
841 .write_end = nobh_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 .bmap = ext2_bmap,
843 .direct_IO = ext2_direct_IO,
844 .writepages = ext2_writepages,
Christoph Lametere965f962006-02-01 03:05:41 -0800845 .migratepage = buffer_migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +0200846 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847};
848
849/*
850 * Probably it should be a library function... search for first non-zero word
851 * or memcmp with zero_page, whatever is better for particular architecture.
852 * Linus?
853 */
854static inline int all_zeroes(__le32 *p, __le32 *q)
855{
856 while (p < q)
857 if (*p++)
858 return 0;
859 return 1;
860}
861
862/**
863 * ext2_find_shared - find the indirect blocks for partial truncation.
864 * @inode: inode in question
865 * @depth: depth of the affected branch
866 * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
867 * @chain: place to store the pointers to partial indirect blocks
868 * @top: place to the (detached) top of branch
869 *
870 * This is a helper function used by ext2_truncate().
871 *
872 * When we do truncate() we may have to clean the ends of several indirect
873 * blocks but leave the blocks themselves alive. Block is partially
874 * truncated if some data below the new i_size is refered from it (and
875 * it is on the path to the first completely truncated data block, indeed).
876 * We have to free the top of that path along with everything to the right
877 * of the path. Since no allocation past the truncation point is possible
878 * until ext2_truncate() finishes, we may safely do the latter, but top
879 * of branch may require special attention - pageout below the truncation
880 * point might try to populate it.
881 *
882 * We atomically detach the top of branch from the tree, store the block
883 * number of its root in *@top, pointers to buffer_heads of partially
884 * truncated blocks - in @chain[].bh and pointers to their last elements
885 * that should not be removed - in @chain[].p. Return value is the pointer
886 * to last filled element of @chain.
887 *
888 * The work left to caller to do the actual freeing of subtrees:
889 * a) free the subtree starting from *@top
890 * b) free the subtrees whose roots are stored in
891 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
892 * c) free the subtrees growing from the inode past the @chain[0].p
893 * (no partially truncated stuff there).
894 */
895
896static Indirect *ext2_find_shared(struct inode *inode,
897 int depth,
898 int offsets[4],
899 Indirect chain[4],
900 __le32 *top)
901{
902 Indirect *partial, *p;
903 int k, err;
904
905 *top = 0;
906 for (k = depth; k > 1 && !offsets[k-1]; k--)
907 ;
908 partial = ext2_get_branch(inode, k, offsets, chain, &err);
909 if (!partial)
910 partial = chain + k-1;
911 /*
912 * If the branch acquired continuation since we've looked at it -
913 * fine, it should all survive and (new) top doesn't belong to us.
914 */
915 write_lock(&EXT2_I(inode)->i_meta_lock);
916 if (!partial->key && *partial->p) {
917 write_unlock(&EXT2_I(inode)->i_meta_lock);
918 goto no_top;
919 }
920 for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
921 ;
922 /*
923 * OK, we've found the last block that must survive. The rest of our
924 * branch should be detached before unlocking. However, if that rest
925 * of branch is all ours and does not grow immediately from the inode
926 * it's easier to cheat and just decrement partial->p.
927 */
928 if (p == chain + k - 1 && p > chain) {
929 p->p--;
930 } else {
931 *top = *p->p;
932 *p->p = 0;
933 }
934 write_unlock(&EXT2_I(inode)->i_meta_lock);
935
936 while(partial > p)
937 {
938 brelse(partial->bh);
939 partial--;
940 }
941no_top:
942 return partial;
943}
944
945/**
946 * ext2_free_data - free a list of data blocks
947 * @inode: inode we are dealing with
948 * @p: array of block numbers
949 * @q: points immediately past the end of array
950 *
951 * We are freeing all blocks refered from that array (numbers are
952 * stored as little-endian 32-bit) and updating @inode->i_blocks
953 * appropriately.
954 */
955static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
956{
957 unsigned long block_to_free = 0, count = 0;
958 unsigned long nr;
959
960 for ( ; p < q ; p++) {
961 nr = le32_to_cpu(*p);
962 if (nr) {
963 *p = 0;
964 /* accumulate blocks to free if they're contiguous */
965 if (count == 0)
966 goto free_this;
967 else if (block_to_free == nr - count)
968 count++;
969 else {
970 mark_inode_dirty(inode);
971 ext2_free_blocks (inode, block_to_free, count);
972 free_this:
973 block_to_free = nr;
974 count = 1;
975 }
976 }
977 }
978 if (count > 0) {
979 mark_inode_dirty(inode);
980 ext2_free_blocks (inode, block_to_free, count);
981 }
982}
983
984/**
985 * ext2_free_branches - free an array of branches
986 * @inode: inode we are dealing with
987 * @p: array of block numbers
988 * @q: pointer immediately past the end of array
989 * @depth: depth of the branches to free
990 *
991 * We are freeing all blocks refered from these branches (numbers are
992 * stored as little-endian 32-bit) and updating @inode->i_blocks
993 * appropriately.
994 */
995static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
996{
997 struct buffer_head * bh;
998 unsigned long nr;
999
1000 if (depth--) {
1001 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
1002 for ( ; p < q ; p++) {
1003 nr = le32_to_cpu(*p);
1004 if (!nr)
1005 continue;
1006 *p = 0;
1007 bh = sb_bread(inode->i_sb, nr);
1008 /*
1009 * A read failure? Report error and clear slot
1010 * (should be rare).
1011 */
1012 if (!bh) {
1013 ext2_error(inode->i_sb, "ext2_free_branches",
1014 "Read failure, inode=%ld, block=%ld",
1015 inode->i_ino, nr);
1016 continue;
1017 }
1018 ext2_free_branches(inode,
1019 (__le32*)bh->b_data,
1020 (__le32*)bh->b_data + addr_per_block,
1021 depth);
1022 bforget(bh);
1023 ext2_free_blocks(inode, nr, 1);
1024 mark_inode_dirty(inode);
1025 }
1026 } else
1027 ext2_free_data(inode, p, q);
1028}
1029
Martin J. Bligha686cd82007-10-16 23:30:46 -07001030void ext2_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 __le32 *i_data = EXT2_I(inode)->i_data;
Martin J. Bligha686cd82007-10-16 23:30:46 -07001033 struct ext2_inode_info *ei = EXT2_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
1035 int offsets[4];
1036 Indirect chain[4];
1037 Indirect *partial;
1038 __le32 nr = 0;
1039 int n;
1040 long iblock;
1041 unsigned blocksize;
1042
1043 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1044 S_ISLNK(inode->i_mode)))
1045 return;
1046 if (ext2_inode_is_fast_symlink(inode))
1047 return;
1048 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1049 return;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 blocksize = inode->i_sb->s_blocksize;
1052 iblock = (inode->i_size + blocksize-1)
1053 >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
1054
Carsten Otte6d791252005-06-23 22:05:26 -07001055 if (mapping_is_xip(inode->i_mapping))
1056 xip_truncate_page(inode->i_mapping, inode->i_size);
1057 else if (test_opt(inode->i_sb, NOBH))
Nick Piggin03158cd2007-10-16 01:25:25 -07001058 nobh_truncate_page(inode->i_mapping,
1059 inode->i_size, ext2_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 else
1061 block_truncate_page(inode->i_mapping,
1062 inode->i_size, ext2_get_block);
1063
1064 n = ext2_block_to_path(inode, iblock, offsets, NULL);
1065 if (n == 0)
1066 return;
1067
Martin J. Bligha686cd82007-10-16 23:30:46 -07001068 /*
1069 * From here we block out all ext2_get_block() callers who want to
1070 * modify the block allocation tree.
1071 */
1072 mutex_lock(&ei->truncate_mutex);
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (n == 1) {
1075 ext2_free_data(inode, i_data+offsets[0],
1076 i_data + EXT2_NDIR_BLOCKS);
1077 goto do_indirects;
1078 }
1079
1080 partial = ext2_find_shared(inode, n, offsets, chain, &nr);
1081 /* Kill the top of shared branch (already detached) */
1082 if (nr) {
1083 if (partial == chain)
1084 mark_inode_dirty(inode);
1085 else
1086 mark_buffer_dirty_inode(partial->bh, inode);
1087 ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
1088 }
1089 /* Clear the ends of indirect blocks on the shared branch */
1090 while (partial > chain) {
1091 ext2_free_branches(inode,
1092 partial->p + 1,
1093 (__le32*)partial->bh->b_data+addr_per_block,
1094 (chain+n-1) - partial);
1095 mark_buffer_dirty_inode(partial->bh, inode);
1096 brelse (partial->bh);
1097 partial--;
1098 }
1099do_indirects:
1100 /* Kill the remaining (whole) subtrees */
1101 switch (offsets[0]) {
1102 default:
1103 nr = i_data[EXT2_IND_BLOCK];
1104 if (nr) {
1105 i_data[EXT2_IND_BLOCK] = 0;
1106 mark_inode_dirty(inode);
1107 ext2_free_branches(inode, &nr, &nr+1, 1);
1108 }
1109 case EXT2_IND_BLOCK:
1110 nr = i_data[EXT2_DIND_BLOCK];
1111 if (nr) {
1112 i_data[EXT2_DIND_BLOCK] = 0;
1113 mark_inode_dirty(inode);
1114 ext2_free_branches(inode, &nr, &nr+1, 2);
1115 }
1116 case EXT2_DIND_BLOCK:
1117 nr = i_data[EXT2_TIND_BLOCK];
1118 if (nr) {
1119 i_data[EXT2_TIND_BLOCK] = 0;
1120 mark_inode_dirty(inode);
1121 ext2_free_branches(inode, &nr, &nr+1, 3);
1122 }
1123 case EXT2_TIND_BLOCK:
1124 ;
1125 }
Martin J. Bligha686cd82007-10-16 23:30:46 -07001126
1127 ext2_discard_reservation(inode);
1128
1129 mutex_unlock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1131 if (inode_needs_sync(inode)) {
1132 sync_mapping_buffers(inode->i_mapping);
1133 ext2_sync_inode (inode);
1134 } else {
1135 mark_inode_dirty(inode);
1136 }
1137}
1138
1139static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
1140 struct buffer_head **p)
1141{
1142 struct buffer_head * bh;
1143 unsigned long block_group;
1144 unsigned long block;
1145 unsigned long offset;
1146 struct ext2_group_desc * gdp;
1147
1148 *p = NULL;
1149 if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
1150 ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
1151 goto Einval;
1152
1153 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
Eric Sandeenef2fb672007-10-16 23:26:30 -07001154 gdp = ext2_get_group_desc(sb, block_group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (!gdp)
1156 goto Egdp;
1157 /*
1158 * Figure out the offset within the block group inode table
1159 */
1160 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
1161 block = le32_to_cpu(gdp->bg_inode_table) +
1162 (offset >> EXT2_BLOCK_SIZE_BITS(sb));
1163 if (!(bh = sb_bread(sb, block)))
1164 goto Eio;
1165
1166 *p = bh;
1167 offset &= (EXT2_BLOCK_SIZE(sb) - 1);
1168 return (struct ext2_inode *) (bh->b_data + offset);
1169
1170Einval:
1171 ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
1172 (unsigned long) ino);
1173 return ERR_PTR(-EINVAL);
1174Eio:
1175 ext2_error(sb, "ext2_get_inode",
1176 "unable to read inode block - inode=%lu, block=%lu",
1177 (unsigned long) ino, block);
1178Egdp:
1179 return ERR_PTR(-EIO);
1180}
1181
1182void ext2_set_inode_flags(struct inode *inode)
1183{
1184 unsigned int flags = EXT2_I(inode)->i_flags;
1185
1186 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1187 if (flags & EXT2_SYNC_FL)
1188 inode->i_flags |= S_SYNC;
1189 if (flags & EXT2_APPEND_FL)
1190 inode->i_flags |= S_APPEND;
1191 if (flags & EXT2_IMMUTABLE_FL)
1192 inode->i_flags |= S_IMMUTABLE;
1193 if (flags & EXT2_NOATIME_FL)
1194 inode->i_flags |= S_NOATIME;
1195 if (flags & EXT2_DIRSYNC_FL)
1196 inode->i_flags |= S_DIRSYNC;
1197}
1198
Jan Kara4f99ed62007-05-08 00:31:04 -07001199/* Propagate flags from i_flags to EXT2_I(inode)->i_flags */
1200void ext2_get_inode_flags(struct ext2_inode_info *ei)
1201{
1202 unsigned int flags = ei->vfs_inode.i_flags;
1203
1204 ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL|
1205 EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL);
1206 if (flags & S_SYNC)
1207 ei->i_flags |= EXT2_SYNC_FL;
1208 if (flags & S_APPEND)
1209 ei->i_flags |= EXT2_APPEND_FL;
1210 if (flags & S_IMMUTABLE)
1211 ei->i_flags |= EXT2_IMMUTABLE_FL;
1212 if (flags & S_NOATIME)
1213 ei->i_flags |= EXT2_NOATIME_FL;
1214 if (flags & S_DIRSYNC)
1215 ei->i_flags |= EXT2_DIRSYNC_FL;
1216}
1217
David Howells52fcf702008-02-07 00:15:35 -08001218struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
David Howells52fcf702008-02-07 00:15:35 -08001220 struct ext2_inode_info *ei;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 struct buffer_head * bh;
David Howells52fcf702008-02-07 00:15:35 -08001222 struct ext2_inode *raw_inode;
1223 struct inode *inode;
1224 long ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 int n;
1226
David Howells52fcf702008-02-07 00:15:35 -08001227 inode = iget_locked(sb, ino);
1228 if (!inode)
1229 return ERR_PTR(-ENOMEM);
1230 if (!(inode->i_state & I_NEW))
1231 return inode;
1232
1233 ei = EXT2_I(inode);
Martin J. Bligha686cd82007-10-16 23:30:46 -07001234 ei->i_block_alloc_info = NULL;
1235
David Howells52fcf702008-02-07 00:15:35 -08001236 raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
1237 if (IS_ERR(raw_inode)) {
1238 ret = PTR_ERR(raw_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 goto bad_inode;
David Howells52fcf702008-02-07 00:15:35 -08001240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
1243 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
1244 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
1245 if (!(test_opt (inode->i_sb, NO_UID32))) {
1246 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
1247 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
1248 }
1249 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
1250 inode->i_size = le32_to_cpu(raw_inode->i_size);
Markus Rechberger4d7bf112007-05-08 00:23:39 -07001251 inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
1252 inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
1253 inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
1255 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
1256 /* We now have enough fields to check if the inode was active or not.
1257 * This is needed because nfsd might try to access dead inodes
1258 * the test is that same one that e2fsck uses
1259 * NeilBrown 1999oct15
1260 */
1261 if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
1262 /* this inode is deleted */
1263 brelse (bh);
David Howells52fcf702008-02-07 00:15:35 -08001264 ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 goto bad_inode;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
1268 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
1269 ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
1270 ei->i_frag_no = raw_inode->i_frag;
1271 ei->i_frag_size = raw_inode->i_fsize;
1272 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
1273 ei->i_dir_acl = 0;
1274 if (S_ISREG(inode->i_mode))
1275 inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
1276 else
1277 ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
1278 ei->i_dtime = 0;
1279 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
1280 ei->i_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
1282 ei->i_dir_start_lookup = 0;
1283
1284 /*
1285 * NOTE! The in-memory inode i_data array is in little-endian order
1286 * even on big-endian machines: we do NOT byteswap the block numbers!
1287 */
1288 for (n = 0; n < EXT2_N_BLOCKS; n++)
1289 ei->i_data[n] = raw_inode->i_block[n];
1290
1291 if (S_ISREG(inode->i_mode)) {
1292 inode->i_op = &ext2_file_inode_operations;
Carsten Otte6d791252005-06-23 22:05:26 -07001293 if (ext2_use_xip(inode->i_sb)) {
1294 inode->i_mapping->a_ops = &ext2_aops_xip;
1295 inode->i_fop = &ext2_xip_file_operations;
1296 } else if (test_opt(inode->i_sb, NOBH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 inode->i_mapping->a_ops = &ext2_nobh_aops;
Carsten Otte6d791252005-06-23 22:05:26 -07001298 inode->i_fop = &ext2_file_operations;
1299 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 inode->i_mapping->a_ops = &ext2_aops;
Carsten Otte6d791252005-06-23 22:05:26 -07001301 inode->i_fop = &ext2_file_operations;
1302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 } else if (S_ISDIR(inode->i_mode)) {
1304 inode->i_op = &ext2_dir_inode_operations;
1305 inode->i_fop = &ext2_dir_operations;
1306 if (test_opt(inode->i_sb, NOBH))
1307 inode->i_mapping->a_ops = &ext2_nobh_aops;
1308 else
1309 inode->i_mapping->a_ops = &ext2_aops;
1310 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffin8d6d0c42008-12-19 20:47:13 +00001311 if (ext2_inode_is_fast_symlink(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 inode->i_op = &ext2_fast_symlink_inode_operations;
Duane Griffin8d6d0c42008-12-19 20:47:13 +00001313 nd_terminate_link(ei->i_data, inode->i_size,
1314 sizeof(ei->i_data) - 1);
1315 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 inode->i_op = &ext2_symlink_inode_operations;
1317 if (test_opt(inode->i_sb, NOBH))
1318 inode->i_mapping->a_ops = &ext2_nobh_aops;
1319 else
1320 inode->i_mapping->a_ops = &ext2_aops;
1321 }
1322 } else {
1323 inode->i_op = &ext2_special_inode_operations;
1324 if (raw_inode->i_block[0])
1325 init_special_inode(inode, inode->i_mode,
1326 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
1327 else
1328 init_special_inode(inode, inode->i_mode,
1329 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
1330 }
1331 brelse (bh);
1332 ext2_set_inode_flags(inode);
David Howells52fcf702008-02-07 00:15:35 -08001333 unlock_new_inode(inode);
1334 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336bad_inode:
David Howells52fcf702008-02-07 00:15:35 -08001337 iget_failed(inode);
1338 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001341static int __ext2_write_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct ext2_inode_info *ei = EXT2_I(inode);
1344 struct super_block *sb = inode->i_sb;
1345 ino_t ino = inode->i_ino;
1346 uid_t uid = inode->i_uid;
1347 gid_t gid = inode->i_gid;
1348 struct buffer_head * bh;
1349 struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
1350 int n;
1351 int err = 0;
1352
1353 if (IS_ERR(raw_inode))
1354 return -EIO;
1355
1356 /* For fields not not tracking in the in-memory inode,
1357 * initialise them to zero for new inodes. */
1358 if (ei->i_state & EXT2_STATE_NEW)
1359 memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
1360
Jan Kara4f99ed62007-05-08 00:31:04 -07001361 ext2_get_inode_flags(ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
1363 if (!(test_opt(sb, NO_UID32))) {
1364 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
1365 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
1366/*
1367 * Fix up interoperability with old kernels. Otherwise, old inodes get
1368 * re-used with the upper 16 bits of the uid/gid intact
1369 */
1370 if (!ei->i_dtime) {
1371 raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
1372 raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
1373 } else {
1374 raw_inode->i_uid_high = 0;
1375 raw_inode->i_gid_high = 0;
1376 }
1377 } else {
1378 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
1379 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
1380 raw_inode->i_uid_high = 0;
1381 raw_inode->i_gid_high = 0;
1382 }
1383 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
1384 raw_inode->i_size = cpu_to_le32(inode->i_size);
1385 raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
1386 raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
1387 raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
1388
1389 raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
1390 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
1391 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
1392 raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
1393 raw_inode->i_frag = ei->i_frag_no;
1394 raw_inode->i_fsize = ei->i_frag_size;
1395 raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
1396 if (!S_ISREG(inode->i_mode))
1397 raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
1398 else {
1399 raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
1400 if (inode->i_size > 0x7fffffffULL) {
1401 if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
1402 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
1403 EXT2_SB(sb)->s_es->s_rev_level ==
1404 cpu_to_le32(EXT2_GOOD_OLD_REV)) {
1405 /* If this is the first large file
1406 * created, add a flag to the superblock.
1407 */
Jan Blunckc15271f2010-04-14 14:38:38 +02001408 spin_lock(&EXT2_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 ext2_update_dynamic_rev(sb);
1410 EXT2_SET_RO_COMPAT_FEATURE(sb,
1411 EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
Jan Blunckc15271f2010-04-14 14:38:38 +02001412 spin_unlock(&EXT2_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 ext2_write_super(sb);
1414 }
1415 }
1416 }
1417
1418 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
1419 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1420 if (old_valid_dev(inode->i_rdev)) {
1421 raw_inode->i_block[0] =
1422 cpu_to_le32(old_encode_dev(inode->i_rdev));
1423 raw_inode->i_block[1] = 0;
1424 } else {
1425 raw_inode->i_block[0] = 0;
1426 raw_inode->i_block[1] =
1427 cpu_to_le32(new_encode_dev(inode->i_rdev));
1428 raw_inode->i_block[2] = 0;
1429 }
1430 } else for (n = 0; n < EXT2_N_BLOCKS; n++)
1431 raw_inode->i_block[n] = ei->i_data[n];
1432 mark_buffer_dirty(bh);
1433 if (do_sync) {
1434 sync_dirty_buffer(bh);
1435 if (buffer_req(bh) && !buffer_uptodate(bh)) {
1436 printk ("IO error syncing ext2 inode [%s:%08lx]\n",
1437 sb->s_id, (unsigned long) ino);
1438 err = -EIO;
1439 }
1440 }
1441 ei->i_state &= ~EXT2_STATE_NEW;
1442 brelse (bh);
1443 return err;
1444}
1445
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001446int ext2_write_inode(struct inode *inode, struct writeback_control *wbc)
1447{
1448 return __ext2_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1449}
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451int ext2_sync_inode(struct inode *inode)
1452{
1453 struct writeback_control wbc = {
1454 .sync_mode = WB_SYNC_ALL,
1455 .nr_to_write = 0, /* sys_fsync did this */
1456 };
1457 return sync_inode(inode, &wbc);
1458}
1459
1460int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
1461{
1462 struct inode *inode = dentry->d_inode;
1463 int error;
1464
1465 error = inode_change_ok(inode, iattr);
1466 if (error)
1467 return error;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001468
Dmitry Monakhov12755622010-04-08 22:04:20 +04001469 if (is_quota_modification(inode, iattr))
Christoph Hellwig871a2932010-03-03 09:05:07 -05001470 dquot_initialize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
1472 (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001473 error = dquot_transfer(inode, iattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (error)
1475 return error;
1476 }
1477 error = inode_setattr(inode, iattr);
1478 if (!error && (iattr->ia_valid & ATTR_MODE))
1479 error = ext2_acl_chmod(inode);
1480 return error;
1481}