1 /*
2 * linux/fs/ext3/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@redhat.com), 1993, 1998
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
19 * 64-bit file support on 64-bit platforms by Jakub Jelinek
20 * (jj@sunsite.ms.mff.cuni.cz)
21 *
22 * Assorted race fixes, rewrite of ext3_get_block() by Al Viro, 2000
23 */
24
25 #include <linux/module.h>
26 #include <linux/fs.h>
27 #include <linux/time.h>
28 #include <linux/ext3_jbd.h>
29 #include <linux/jbd.h>
30 #include <linux/smp_lock.h>
31 #include <linux/highuid.h>
32 #include <linux/pagemap.h>
33 #include <linux/quotaops.h>
34 #include <linux/string.h>
35 #include <linux/buffer_head.h>
36 #include <linux/writeback.h>
37 #include <linux/mpage.h>
38 #include <linux/uio.h>
39 #include "xattr.h"
40 #include "acl.h"
41
42 static int ext3_writepage_trans_blocks(struct inode *inode);
43
44 /*
45 * Test whether an inode is a fast symlink.
46 */
47 static inline int ext3_inode_is_fast_symlink(struct inode *inode)
48 {
49 int ea_blocks = EXT3_I(inode)->i_file_acl ?
50 (inode->i_sb->s_blocksize >> 9) : 0;
51
52 return (S_ISLNK(inode->i_mode) &&
53 inode->i_blocks - ea_blocks == 0);
54 }
55
56 /* The ext3 forget function must perform a revoke if we are freeing data
57 * which has been journaled. Metadata (eg. indirect blocks) must be
58 * revoked in all cases.
59 *
60 * "bh" may be NULL: a metadata block may have been freed from memory
61 * but there may still be a record of it in the journal, and that record
62 * still needs to be revoked.
63 */
64
65 int ext3_forget(handle_t *handle, int is_metadata,
66 struct inode *inode, struct buffer_head *bh,
67 int blocknr)
68 {
69 int err;
70
71 might_sleep();
72
73 BUFFER_TRACE(bh, "enter");
74
75 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
76 "data mode %lx\n",
77 bh, is_metadata, inode->i_mode,
78 test_opt(inode->i_sb, DATA_FLAGS));
79
80 /* Never use the revoke function if we are doing full data
81 * journaling: there is no need to, and a V1 superblock won't
82 * support it. Otherwise, only skip the revoke on un-journaled
83 * data blocks. */
84
85 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ||
86 (!is_metadata && !ext3_should_journal_data(inode))) {
87 if (bh) {
88 BUFFER_TRACE(bh, "call journal_forget");
89 return ext3_journal_forget(handle, bh);
90 }
91 return 0;
92 }
93
94 /*
95 * data!=journal && (is_metadata || should_journal_data(inode))
96 */
97 BUFFER_TRACE(bh, "call ext3_journal_revoke");
98 err = ext3_journal_revoke(handle, blocknr, bh);
99 if (err)
100 ext3_abort(inode->i_sb, __FUNCTION__,
101 "error %d when attempting revoke", err);
102 BUFFER_TRACE(bh, "exit");
103 return err;
104 }
105
106 /*
107 * Work out how many blocks we need to progress with the next chunk of a
108 * truncate transaction.
109 */
110
111 static unsigned long blocks_for_truncate(struct inode *inode)
112 {
113 unsigned long needed;
114
115 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
116
117 /* Give ourselves just enough room to cope with inodes in which
118 * i_blocks is corrupt: we've seen disk corruptions in the past
119 * which resulted in random data in an inode which looked enough
120 * like a regular file for ext3 to try to delete it. Things
121 * will go a bit crazy if that happens, but at least we should
122 * try not to panic the whole kernel. */
123 if (needed < 2)
124 needed = 2;
125
126 /* But we need to bound the transaction so we don't overflow the
127 * journal. */
128 if (needed > EXT3_MAX_TRANS_DATA)
129 needed = EXT3_MAX_TRANS_DATA;
130
131 return EXT3_DATA_TRANS_BLOCKS + needed;
132 }
133
134 /*
135 * Truncate transactions can be complex and absolutely huge. So we need to
136 * be able to restart the transaction at a conventient checkpoint to make
137 * sure we don't overflow the journal.
138 *
139 * start_transaction gets us a new handle for a truncate transaction,
140 * and extend_transaction tries to extend the existing one a bit. If
141 * extend fails, we need to propagate the failure up and restart the
142 * transaction in the top-level truncate loop. --sct
143 */
144
145 static handle_t *start_transaction(struct inode *inode)
146 {
147 handle_t *result;
148
149 result = ext3_journal_start(inode, blocks_for_truncate(inode));
150 if (!IS_ERR(result))
151 return result;
152
153 ext3_std_error(inode->i_sb, PTR_ERR(result));
154 return result;
155 }
156
157 /*
158 * Try to extend this transaction for the purposes of truncation.
159 *
160 * Returns 0 if we managed to create more room. If we can't create more
161 * room, and the transaction must be restarted we return 1.
162 */
163 static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
164 {
165 if (handle->h_buffer_credits > EXT3_RESERVE_TRANS_BLOCKS)
166 return 0;
167 if (!ext3_journal_extend(handle, blocks_for_truncate(inode)))
168 return 0;
169 return 1;
170 }
171
172 /*
173 * Restart the transaction associated with *handle. This does a commit,
174 * so before we call here everything must be consistently dirtied against
175 * this transaction.
176 */
177 static int ext3_journal_test_restart(handle_t *handle, struct inode *inode)
178 {
179 jbd_debug(2, "restarting handle %p\n", handle);
180 return ext3_journal_restart(handle, blocks_for_truncate(inode));
181 }
182
183 /*
184 * Called at the last iput() if i_nlink is zero.
185 */
186 void ext3_delete_inode (struct inode * inode)
187 {
188 handle_t *handle;
189
190 if (is_bad_inode(inode))
191 goto no_delete;
192
193 handle = start_transaction(inode);
194 if (IS_ERR(handle)) {
195 /* If we're going to skip the normal cleanup, we still
196 * need to make sure that the in-core orphan linked list
197 * is properly cleaned up. */
198 ext3_orphan_del(NULL, inode);
199 goto no_delete;
200 }
201
202 if (IS_SYNC(inode))
203 handle->h_sync = 1;
204 inode->i_size = 0;
205 if (inode->i_blocks)
206 ext3_truncate(inode);
207 /*
208 * Kill off the orphan record which ext3_truncate created.
209 * AKPM: I think this can be inside the above `if'.
210 * Note that ext3_orphan_del() has to be able to cope with the
211 * deletion of a non-existent orphan - this is because we don't
212 * know if ext3_truncate() actually created an orphan record.
213 * (Well, we could do this if we need to, but heck - it works)
214 */
215 ext3_orphan_del(handle, inode);
216 EXT3_I(inode)->i_dtime = get_seconds();
217
218 /*
219 * One subtle ordering requirement: if anything has gone wrong
220 * (transaction abort, IO errors, whatever), then we can still
221 * do these next steps (the fs will already have been marked as
222 * having errors), but we can't free the inode if the mark_dirty
223 * fails.
224 */
225 if (ext3_mark_inode_dirty(handle, inode))
226 /* If that failed, just do the required in-core inode clear. */
227 clear_inode(inode);
228 else
229 ext3_free_inode(handle, inode);
230 ext3_journal_stop(handle);
231 return;
232 no_delete:
233 clear_inode(inode); /* We must guarantee clearing of inode... */
234 }
235
236 static int ext3_alloc_block (handle_t *handle,
237 struct inode * inode, unsigned long goal, int *err)
238 {
239 unsigned long result;
240
241 result = ext3_new_block(handle, inode, goal, err);
242 return result;
243 }
244
245
246 typedef struct {
247 __le32 *p;
248 __le32 key;
249 struct buffer_head *bh;
250 } Indirect;
251
252 static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
253 {
254 p->key = *(p->p = v);
255 p->bh = bh;
256 }
257
258 static inline int verify_chain(Indirect *from, Indirect *to)
259 {
260 while (from <= to && from->key == *from->p)
261 from++;
262 return (from > to);
263 }
264
265 /**
266 * ext3_block_to_path - parse the block number into array of offsets
267 * @inode: inode in question (we are only interested in its superblock)
268 * @i_block: block number to be parsed
269 * @offsets: array to store the offsets in
270 * @boundary: set this non-zero if the referred-to block is likely to be
271 * followed (on disk) by an indirect block.
272 *
273 * To store the locations of file's data ext3 uses a data structure common
274 * for UNIX filesystems - tree of pointers anchored in the inode, with
275 * data blocks at leaves and indirect blocks in intermediate nodes.
276 * This function translates the block number into path in that tree -
277 * return value is the path length and @offsets[n] is the offset of
278 * pointer to (n+1)th node in the nth one. If @block is out of range
279 * (negative or too large) warning is printed and zero returned.
280 *
281 * Note: function doesn't find node addresses, so no IO is needed. All
282 * we need to know is the capacity of indirect blocks (taken from the
283 * inode->i_sb).
284 */
285
286 /*
287 * Portability note: the last comparison (check that we fit into triple
288 * indirect block) is spelled differently, because otherwise on an
289 * architecture with 32-bit longs and 8Kb pages we might get into trouble
290 * if our filesystem had 8Kb blocks. We might use long long, but that would
291 * kill us on x86. Oh, well, at least the sign propagation does not matter -
292 * i_block would have to be negative in the very beginning, so we would not
293 * get there at all.
294 */
295
296 static int ext3_block_to_path(struct inode *inode,
297 long i_block, int offsets[4], int *boundary)
298 {
299 int ptrs = EXT3_ADDR_PER_BLOCK(inode->i_sb);
300 int ptrs_bits = EXT3_ADDR_PER_BLOCK_BITS(inode->i_sb);
301 const long direct_blocks = EXT3_NDIR_BLOCKS,
302 indirect_blocks = ptrs,
303 double_blocks = (1 << (ptrs_bits * 2));
304 int n = 0;
305 int final = 0;
306
307 if (i_block < 0) {
308 ext3_warning (inode->i_sb, "ext3_block_to_path", "block < 0");
309 } else if (i_block < direct_blocks) {
310 offsets[n++] = i_block;
311 final = direct_blocks;
312 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
313 offsets[n++] = EXT3_IND_BLOCK;
314 offsets[n++] = i_block;
315 final = ptrs;
316 } else if ((i_block -= indirect_blocks) < double_blocks) {
317 offsets[n++] = EXT3_DIND_BLOCK;
318 offsets[n++] = i_block >> ptrs_bits;
319 offsets[n++] = i_block & (ptrs - 1);
320 final = ptrs;
321 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
322 offsets[n++] = EXT3_TIND_BLOCK;
323 offsets[n++] = i_block >> (ptrs_bits * 2);
324 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
325 offsets[n++] = i_block & (ptrs - 1);
326 final = ptrs;
327 } else {
328 ext3_warning (inode->i_sb, "ext3_block_to_path", "block > big");
329 }
330 if (boundary)
331 *boundary = (i_block & (ptrs - 1)) == (final - 1);
332 return n;
333 }
334
335 /**
336 * ext3_get_branch - read the chain of indirect blocks leading to data
337 * @inode: inode in question
338 * @depth: depth of the chain (1 - direct pointer, etc.)
339 * @offsets: offsets of pointers in inode/indirect blocks
340 * @chain: place to store the result
341 * @err: here we store the error value
342 *
343 * Function fills the array of triples <key, p, bh> and returns %NULL
344 * if everything went OK or the pointer to the last filled triple
345 * (incomplete one) otherwise. Upon the return chain[i].key contains
346 * the number of (i+1)-th block in the chain (as it is stored in memory,
347 * i.e. little-endian 32-bit), chain[i].p contains the address of that
348 * number (it points into struct inode for i==0 and into the bh->b_data
349 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
350 * block for i>0 and NULL for i==0. In other words, it holds the block
351 * numbers of the chain, addresses they were taken from (and where we can
352 * verify that chain did not change) and buffer_heads hosting these
353 * numbers.
354 *
355 * Function stops when it stumbles upon zero pointer (absent block)
356 * (pointer to last triple returned, *@err == 0)
357 * or when it gets an IO error reading an indirect block
358 * (ditto, *@err == -EIO)
359 * or when it notices that chain had been changed while it was reading
360 * (ditto, *@err == -EAGAIN)
361 * or when it reads all @depth-1 indirect blocks successfully and finds
362 * the whole chain, all way to the data (returns %NULL, *err == 0).
363 */
364 static Indirect *ext3_get_branch(struct inode *inode, int depth, int *offsets,
365 Indirect chain[4], int *err)
366 {
367 struct super_block *sb = inode->i_sb;
368 Indirect *p = chain;
369 struct buffer_head *bh;
370
371 *err = 0;
372 /* i_data is not going away, no lock needed */
373 add_chain (chain, NULL, EXT3_I(inode)->i_data + *offsets);
374 if (!p->key)
375 goto no_block;
376 while (--depth) {
377 bh = sb_bread(sb, le32_to_cpu(p->key));
378 if (!bh)
379 goto failure;
380 /* Reader: pointers */
381 if (!verify_chain(chain, p))
382 goto changed;
383 add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
384 /* Reader: end */
385 if (!p->key)
386 goto no_block;
387 }
388 return NULL;
389
390 changed:
391 brelse(bh);
392 *err = -EAGAIN;
393 goto no_block;
394 failure:
395 *err = -EIO;
396 no_block:
397 return p;
398 }
399
400 /**
401 * ext3_find_near - find a place for allocation with sufficient locality
402 * @inode: owner
403 * @ind: descriptor of indirect block.
404 *
405 * This function returns the prefered place for block allocation.
406 * It is used when heuristic for sequential allocation fails.
407 * Rules are:
408 * + if there is a block to the left of our position - allocate near it.
409 * + if pointer will live in indirect block - allocate near that block.
410 * + if pointer will live in inode - allocate in the same
411 * cylinder group.
412 *
413 * In the latter case we colour the starting block by the callers PID to
414 * prevent it from clashing with concurrent allocations for a different inode
415 * in the same block group. The PID is used here so that functionally related
416 * files will be close-by on-disk.
417 *
418 * Caller must make sure that @ind is valid and will stay that way.
419 */
420
421 static unsigned long ext3_find_near(struct inode *inode, Indirect *ind)
422 {
423 struct ext3_inode_info *ei = EXT3_I(inode);
424 __le32 *start = ind->bh ? (__le32*) ind->bh->b_data : ei->i_data;
425 __le32 *p;
426 unsigned long bg_start;
427 unsigned long colour;
428
429 /* Try to find previous block */
430 for (p = ind->p - 1; p >= start; p--)
431 if (*p)
432 return le32_to_cpu(*p);
433
434 /* No such thing, so let's try location of indirect block */
435 if (ind->bh)
436 return ind->bh->b_blocknr;
437
438 /*
439 * It is going to be refered from inode itself? OK, just put it into
440 * the same cylinder group then.
441 */
442 bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
443 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
444 colour = (current->pid % 16) *
445 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
446 return bg_start + colour;
447 }
448
449 /**
450 * ext3_find_goal - find a prefered place for allocation.
451 * @inode: owner
452 * @block: block we want
453 * @chain: chain of indirect blocks
454 * @partial: pointer to the last triple within a chain
455 * @goal: place to store the result.
456 *
457 * Normally this function find the prefered place for block allocation,
458 * stores it in *@goal and returns zero. If the branch had been changed
459 * under us we return -EAGAIN.
460 */
461
462 static int ext3_find_goal(struct inode *inode, long block, Indirect chain[4],
463 Indirect *partial, unsigned long *goal)
464 {
465 struct ext3_inode_info *ei = EXT3_I(inode);
466 /* Writer: ->i_next_alloc* */
467 if ((block == ei->i_next_alloc_block + 1)&& ei->i_next_alloc_goal) {
468 ei->i_next_alloc_block++;
469 ei->i_next_alloc_goal++;
470 }
471 /* Writer: end */
472 /* Reader: pointers, ->i_next_alloc* */
473 if (verify_chain(chain, partial)) {
474 /*
475 * try the heuristic for sequential allocation,
476 * failing that at least try to get decent locality.
477 */
478 if (block == ei->i_next_alloc_block)
479 *goal = ei->i_next_alloc_goal;
480 if (!*goal)
481 *goal = ext3_find_near(inode, partial);
482 return 0;
483 }
484 /* Reader: end */
485 return -EAGAIN;
486 }
487
488 /**
489 * ext3_alloc_branch - allocate and set up a chain of blocks.
490 * @inode: owner
491 * @num: depth of the chain (number of blocks to allocate)
492 * @offsets: offsets (in the blocks) to store the pointers to next.
493 * @branch: place to store the chain in.
494 *
495 * This function allocates @num blocks, zeroes out all but the last one,
496 * links them into chain and (if we are synchronous) writes them to disk.
497 * In other words, it prepares a branch that can be spliced onto the
498 * inode. It stores the information about that chain in the branch[], in
499 * the same format as ext3_get_branch() would do. We are calling it after
500 * we had read the existing part of chain and partial points to the last
501 * triple of that (one with zero ->key). Upon the exit we have the same
502 * picture as after the successful ext3_get_block(), excpet that in one
503 * place chain is disconnected - *branch->p is still zero (we did not
504 * set the last link), but branch->key contains the number that should
505 * be placed into *branch->p to fill that gap.
506 *
507 * If allocation fails we free all blocks we've allocated (and forget
508 * their buffer_heads) and return the error value the from failed
509 * ext3_alloc_block() (normally -ENOSPC). Otherwise we set the chain
510 * as described above and return 0.
511 */
512
513 static int ext3_alloc_branch(handle_t *handle, struct inode *inode,
514 int num,
515 unsigned long goal,
516 int *offsets,
517 Indirect *branch)
518 {
519 int blocksize = inode->i_sb->s_blocksize;
520 int n = 0, keys = 0;
521 int err = 0;
522 int i;
523 int parent = ext3_alloc_block(handle, inode, goal, &err);
524
525 branch[0].key = cpu_to_le32(parent);
526 if (parent) {
527 for (n = 1; n < num; n++) {
528 struct buffer_head *bh;
529 /* Allocate the next block */
530 int nr = ext3_alloc_block(handle, inode, parent, &err);
531 if (!nr)
532 break;
533 branch[n].key = cpu_to_le32(nr);
534 keys = n+1;
535
536 /*
537 * Get buffer_head for parent block, zero it out
538 * and set the pointer to new one, then send
539 * parent to disk.
540 */
541 bh = sb_getblk(inode->i_sb, parent);
542 branch[n].bh = bh;
543 lock_buffer(bh);
544 BUFFER_TRACE(bh, "call get_create_access");
545 err = ext3_journal_get_create_access(handle, bh);
546 if (err) {
547 unlock_buffer(bh);
548 brelse(bh);
549 break;
550 }
551
552 memset(bh->b_data, 0, blocksize);
553 branch[n].p = (__le32*) bh->b_data + offsets[n];
554 *branch[n].p = branch[n].key;
555 BUFFER_TRACE(bh, "marking uptodate");
556 set_buffer_uptodate(bh);
557 unlock_buffer(bh);
558
559 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
560 err = ext3_journal_dirty_metadata(handle, bh);
561 if (err)
562 break;
563
564 parent = nr;
565 }
566 }
567 if (n == num)
568 return 0;
569
570 /* Allocation failed, free what we already allocated */
571 for (i = 1; i < keys; i++) {
572 BUFFER_TRACE(branch[i].bh, "call journal_forget");
573 ext3_journal_forget(handle, branch[i].bh);
574 }
575 for (i = 0; i < keys; i++)
576 ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1);
577 return err;
578 }
579
580 /**
581 * ext3_splice_branch - splice the allocated branch onto inode.
582 * @inode: owner
583 * @block: (logical) number of block we are adding
584 * @chain: chain of indirect blocks (with a missing link - see
585 * ext3_alloc_branch)
586 * @where: location of missing link
587 * @num: number of blocks we are adding
588 *
589 * This function verifies that chain (up to the missing link) had not
590 * changed, fills the missing link and does all housekeeping needed in
591 * inode (->i_blocks, etc.). In case of success we end up with the full
592 * chain to new block and return 0. Otherwise (== chain had been changed)
593 * we free the new blocks (forgetting their buffer_heads, indeed) and
594 * return -EAGAIN.
595 */
596
597 static int ext3_splice_branch(handle_t *handle, struct inode *inode, long block,
598 Indirect chain[4], Indirect *where, int num)
599 {
600 int i;
601 int err = 0;
602 struct ext3_inode_info *ei = EXT3_I(inode);
603
604 /*
605 * If we're splicing into a [td]indirect block (as opposed to the
606 * inode) then we need to get write access to the [td]indirect block
607 * before the splice.
608 */
609 if (where->bh) {
610 BUFFER_TRACE(where->bh, "get_write_access");
611 err = ext3_journal_get_write_access(handle, where->bh);
612 if (err)
613 goto err_out;
614 }
615 /* Verify that place we are splicing to is still there and vacant */
616
617 /* Writer: pointers, ->i_next_alloc* */
618 if (!verify_chain(chain, where-1) || *where->p)
619 /* Writer: end */
620 goto changed;
621
622 /* That's it */
623
624 *where->p = where->key;
625 ei->i_next_alloc_block = block;
626 ei->i_next_alloc_goal = le32_to_cpu(where[num-1].key);
627 /* Writer: end */
628
629 /* We are done with atomic stuff, now do the rest of housekeeping */
630
631 inode->i_ctime = CURRENT_TIME_SEC;
632 ext3_mark_inode_dirty(handle, inode);
633
634 /* had we spliced it onto indirect block? */
635 if (where->bh) {
636 /*
637 * akpm: If we spliced it onto an indirect block, we haven't
638 * altered the inode. Note however that if it is being spliced
639 * onto an indirect block at the very end of the file (the
640 * file is growing) then we *will* alter the inode to reflect
641 * the new i_size. But that is not done here - it is done in
642 * generic_commit_write->__mark_inode_dirty->ext3_dirty_inode.
643 */
644 jbd_debug(5, "splicing indirect only\n");
645 BUFFER_TRACE(where->bh, "call ext3_journal_dirty_metadata");
646 err = ext3_journal_dirty_metadata(handle, where->bh);
647 if (err)
648 goto err_out;
649 } else {
650 /*
651 * OK, we spliced it into the inode itself on a direct block.
652 * Inode was dirtied above.
653 */
654 jbd_debug(5, "splicing direct\n");
655 }
656 return err;
657
658 changed:
659 /*
660 * AKPM: if where[i].bh isn't part of the current updating
661 * transaction then we explode nastily. Test this code path.
662 */
663 jbd_debug(1, "the chain changed: try again\n");
664 err = -EAGAIN;
665
666 err_out:
667 for (i = 1; i < num; i++) {
668 BUFFER_TRACE(where[i].bh, "call journal_forget");
669 ext3_journal_forget(handle, where[i].bh);
670 }
671 /* For the normal collision cleanup case, we free up the blocks.
672 * On genuine filesystem errors we don't even think about doing
673 * that. */
674 if (err == -EAGAIN)
675 for (i = 0; i < num; i++)
676 ext3_free_blocks(handle, inode,
677 le32_to_cpu(where[i].key), 1);
678 return err;
679 }
680
681 /*
682 * Allocation strategy is simple: if we have to allocate something, we will
683 * have to go the whole way to leaf. So let's do it before attaching anything
684 * to tree, set linkage between the newborn blocks, write them if sync is
685 * required, recheck the path, free and repeat if check fails, otherwise
686 * set the last missing link (that will protect us from any truncate-generated
687 * removals - all blocks on the path are immune now) and possibly force the
688 * write on the parent block.
689 * That has a nice additional property: no special recovery from the failed
690 * allocations is needed - we simply release blocks and do not touch anything
691 * reachable from inode.
692 *
693 * akpm: `handle' can be NULL if create == 0.
694 *
695 * The BKL may not be held on entry here. Be sure to take it early.
696 */
697
698 static int
699 ext3_get_block_handle(handle_t *handle, struct inode *inode, sector_t iblock,
700 struct buffer_head *bh_result, int create, int extend_disksize)
701 {
702 int err = -EIO;
703 int offsets[4];
704 Indirect chain[4];
705 Indirect *partial;
706 unsigned long goal;
707 int left;
708 int boundary = 0;
709 int depth = ext3_block_to_path(inode, iblock, offsets, &boundary);
710 struct ext3_inode_info *ei = EXT3_I(inode);
711
712 J_ASSERT(handle != NULL || create == 0);
713
714 if (depth == 0)
715 goto out;
716
717 reread:
718 partial = ext3_get_branch(inode, depth, offsets, chain, &err);
719
720 /* Simplest case - block found, no allocation needed */
721 if (!partial) {
722 clear_buffer_new(bh_result);
723 got_it:
724 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
725 if (boundary)
726 set_buffer_boundary(bh_result);
727 /* Clean up and exit */
728 partial = chain+depth-1; /* the whole chain */
729 goto cleanup;
730 }
731
732 /* Next simple case - plain lookup or failed read of indirect block */
733 if (!create || err == -EIO) {
734 cleanup:
735 while (partial > chain) {
736 BUFFER_TRACE(partial->bh, "call brelse");
737 brelse(partial->bh);
738 partial--;
739 }
740 BUFFER_TRACE(bh_result, "returned");
741 out:
742 return err;
743 }
744
745 /*
746 * Indirect block might be removed by truncate while we were
747 * reading it. Handling of that case (forget what we've got and
748 * reread) is taken out of the main path.
749 */
750 if (err == -EAGAIN)
751 goto changed;
752
753 goal = 0;
754 down(&ei->truncate_sem);
755 if (ext3_find_goal(inode, iblock, chain, partial, &goal) < 0) {
756 up(&ei->truncate_sem);
757 goto changed;
758 }
759
760 left = (chain + depth) - partial;
761
762 /*
763 * Block out ext3_truncate while we alter the tree
764 */
765 err = ext3_alloc_branch(handle, inode, left, goal,
766 offsets+(partial-chain), partial);
767
768 /* The ext3_splice_branch call will free and forget any buffers
769 * on the new chain if there is a failure, but that risks using
770 * up transaction credits, especially for bitmaps where the
771 * credits cannot be returned. Can we handle this somehow? We
772 * may need to return -EAGAIN upwards in the worst case. --sct */
773 if (!err)
774 err = ext3_splice_branch(handle, inode, iblock, chain,
775 partial, left);
776 /* i_disksize growing is protected by truncate_sem
777 * don't forget to protect it if you're about to implement
778 * concurrent ext3_get_block() -bzzz */
779 if (!err && extend_disksize && inode->i_size > ei->i_disksize)
780 ei->i_disksize = inode->i_size;
781 up(&ei->truncate_sem);
782 if (err == -EAGAIN)
783 goto changed;
784 if (err)
785 goto cleanup;
786
787 set_buffer_new(bh_result);
788 goto got_it;
789
790 changed:
791 while (partial > chain) {
792 jbd_debug(1, "buffer chain changed, retrying\n");
793 BUFFER_TRACE(partial->bh, "brelsing");
794 brelse(partial->bh);
795 partial--;
796 }
797 goto reread;
798 }
799
800 static int ext3_get_block(struct inode *inode, sector_t iblock,
801 struct buffer_head *bh_result, int create)
802 {
803 handle_t *handle = NULL;
804 int ret;
805
806 if (create) {
807 handle = ext3_journal_current_handle();
808 J_ASSERT(handle != 0);
809 }
810 ret = ext3_get_block_handle(handle, inode, iblock,
811 bh_result, create, 1);
812 return ret;
813 }
814
815 #define DIO_CREDITS (EXT3_RESERVE_TRANS_BLOCKS + 32)
816
817 static int
818 ext3_direct_io_get_blocks(struct inode *inode, sector_t iblock,
819 unsigned long max_blocks, struct buffer_head *bh_result,
820 int create)
821 {
822 handle_t *handle = journal_current_handle();
823 int ret = 0;
824
825 if (!handle)
826 goto get_block; /* A read */
827
828 if (handle->h_transaction->t_state == T_LOCKED) {
829 /*
830 * Huge direct-io writes can hold off commits for long
831 * periods of time. Let this commit run.
832 */
833 ext3_journal_stop(handle);
834 handle = ext3_journal_start(inode, DIO_CREDITS);
835 if (IS_ERR(handle))
836 ret = PTR_ERR(handle);
837 goto get_block;
838 }
839
840 if (handle->h_buffer_credits <= EXT3_RESERVE_TRANS_BLOCKS) {
841 /*
842 * Getting low on buffer credits...
843 */
844 ret = ext3_journal_extend(handle, DIO_CREDITS);
845 if (ret > 0) {
846 /*
847 * Couldn't extend the transaction. Start a new one.
848 */
849 ret = ext3_journal_restart(handle, DIO_CREDITS);
850 }
851 }
852
853 get_block:
854 if (ret == 0)
855 ret = ext3_get_block_handle(handle, inode, iblock,
856 bh_result, create, 0);
857 bh_result->b_size = (1 << inode->i_blkbits);
858 return ret;
859 }
860
861 /*
862 * `handle' can be NULL if create is zero
863 */
864 struct buffer_head *ext3_getblk(handle_t *handle, struct inode * inode,
865 long block, int create, int * errp)
866 {
867 struct buffer_head dummy;
868 int fatal = 0, err;
869
870 J_ASSERT(handle != NULL || create == 0);
871
872 dummy.b_state = 0;
873 dummy.b_blocknr = -1000;
874 buffer_trace_init(&dummy.b_history);
875 *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
876 if (!*errp && buffer_mapped(&dummy)) {
877 struct buffer_head *bh;
878 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
879 if (buffer_new(&dummy)) {
880 J_ASSERT(create != 0);
881 J_ASSERT(handle != 0);
882
883 /* Now that we do not always journal data, we
884 should keep in mind whether this should
885 always journal the new buffer as metadata.
886 For now, regular file writes use
887 ext3_get_block instead, so it's not a
888 problem. */
889 lock_buffer(bh);
890 BUFFER_TRACE(bh, "call get_create_access");
891 fatal = ext3_journal_get_create_access(handle, bh);
892 if (!fatal && !buffer_uptodate(bh)) {
893 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
894 set_buffer_uptodate(bh);
895 }
896 unlock_buffer(bh);
897 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
898 err = ext3_journal_dirty_metadata(handle, bh);
899 if (!fatal)
900 fatal = err;
901 } else {
902 BUFFER_TRACE(bh, "not a new buffer");
903 }
904 if (fatal) {
905 *errp = fatal;
906 brelse(bh);
907 bh = NULL;
908 }
909 return bh;
910 }
911 return NULL;
912 }
913
914 struct buffer_head *ext3_bread(handle_t *handle, struct inode * inode,
915 int block, int create, int *err)
916 {
917 struct buffer_head * bh;
918
919 bh = ext3_getblk(handle, inode, block, create, err);
920 if (!bh)
921 return bh;
922 if (buffer_uptodate(bh))
923 return bh;
924 ll_rw_block(READ, 1, &bh);
925 wait_on_buffer(bh);
926 if (buffer_uptodate(bh))
927 return bh;
928 put_bh(bh);
929 *err = -EIO;
930 return NULL;
931 }
932
933 static int walk_page_buffers( handle_t *handle,
934 struct buffer_head *head,
935 unsigned from,
936 unsigned to,
937 int *partial,
938 int (*fn)( handle_t *handle,
939 struct buffer_head *bh))
940 {
941 struct buffer_head *bh;
942 unsigned block_start, block_end;
943 unsigned blocksize = head->b_size;
944 int err, ret = 0;
945 struct buffer_head *next;
946
947 for ( bh = head, block_start = 0;
948 ret == 0 && (bh != head || !block_start);
949 block_start = block_end, bh = next)
950 {
951 next = bh->b_this_page;
952 block_end = block_start + blocksize;
953 if (block_end <= from || block_start >= to) {
954 if (partial && !buffer_uptodate(bh))
955 *partial = 1;
956 continue;
957 }
958 err = (*fn)(handle, bh);
959 if (!ret)
960 ret = err;
961 }
962 return ret;
963 }
964
965 /*
966 * To preserve ordering, it is essential that the hole instantiation and
967 * the data write be encapsulated in a single transaction. We cannot
968 * close off a transaction and start a new one between the ext3_get_block()
969 * and the commit_write(). So doing the journal_start at the start of
970 * prepare_write() is the right place.
971 *
972 * Also, this function can nest inside ext3_writepage() ->
973 * block_write_full_page(). In that case, we *know* that ext3_writepage()
974 * has generated enough buffer credits to do the whole page. So we won't
975 * block on the journal in that case, which is good, because the caller may
976 * be PF_MEMALLOC.
977 *
978 * By accident, ext3 can be reentered when a transaction is open via
979 * quota file writes. If we were to commit the transaction while thus
980 * reentered, there can be a deadlock - we would be holding a quota
981 * lock, and the commit would never complete if another thread had a
982 * transaction open and was blocking on the quota lock - a ranking
983 * violation.
984 *
985 * So what we do is to rely on the fact that journal_stop/journal_start
986 * will _not_ run commit under these circumstances because handle->h_ref
987 * is elevated. We'll still have enough credits for the tiny quotafile
988 * write.
989 */
990
991 static int do_journal_get_write_access(handle_t *handle,
992 struct buffer_head *bh)
993 {
994 if (!buffer_mapped(bh) || buffer_freed(bh))
995 return 0;
996 return ext3_journal_get_write_access(handle, bh);
997 }
998
999 static int ext3_prepare_write(struct file *file, struct page *page,
1000 unsigned from, unsigned to)
1001 {
1002 struct inode *inode = page->mapping->host;
1003 int ret, needed_blocks = ext3_writepage_trans_blocks(inode);
1004 handle_t *handle;
1005 int retries = 0;
1006
1007 retry:
1008 handle = ext3_journal_start(inode, needed_blocks);
1009 if (IS_ERR(handle)) {
1010 ret = PTR_ERR(handle);
1011 goto out;
1012 }
1013 ret = block_prepare_write(page, from, to, ext3_get_block);
1014 if (ret)
1015 goto prepare_write_failed;
1016
1017 if (ext3_should_journal_data(inode)) {
1018 ret = walk_page_buffers(handle, page_buffers(page),
1019 from, to, NULL, do_journal_get_write_access);
1020 }
1021 prepare_write_failed:
1022 if (ret)
1023 ext3_journal_stop(handle);
1024 if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
1025 goto retry;
1026 out:
1027 return ret;
1028 }
1029
1030 int
1031 ext3_journal_dirty_data(handle_t *handle, struct buffer_head *bh)
1032 {
1033 int err = journal_dirty_data(handle, bh);
1034 if (err)
1035 ext3_journal_abort_handle(__FUNCTION__, __FUNCTION__,
1036 bh, handle,err);
1037 return err;
1038 }
1039
1040 /* For commit_write() in data=journal mode */
1041 static int commit_write_fn(handle_t *handle, struct buffer_head *bh)
1042 {
1043 if (!buffer_mapped(bh) || buffer_freed(bh))
1044 return 0;
1045 set_buffer_uptodate(bh);
1046 return ext3_journal_dirty_metadata(handle, bh);
1047 }
1048
1049 /*
1050 * We need to pick up the new inode size which generic_commit_write gave us
1051 * `file' can be NULL - eg, when called from page_symlink().
1052 *
1053 * ext3 never places buffers on inode->i_mapping->private_list. metadata
1054 * buffers are managed internally.
1055 */
1056
1057 static int ext3_ordered_commit_write(struct file *file, struct page *page,
1058 unsigned from, unsigned to)
1059 {
1060 handle_t *handle = ext3_journal_current_handle();
1061 struct inode *inode = page->mapping->host;
1062 int ret = 0, ret2;
1063
1064 ret = walk_page_buffers(handle, page_buffers(page),
1065 from, to, NULL, ext3_journal_dirty_data);
1066
1067 if (ret == 0) {
1068 /*
1069 * generic_commit_write() will run mark_inode_dirty() if i_size
1070 * changes. So let's piggyback the i_disksize mark_inode_dirty
1071 * into that.
1072 */
1073 loff_t new_i_size;
1074
1075 new_i_size = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1076 if (new_i_size > EXT3_I(inode)->i_disksize)
1077 EXT3_I(inode)->i_disksize = new_i_size;
1078 ret = generic_commit_write(file, page, from, to);
1079 }
1080 ret2 = ext3_journal_stop(handle);
1081 if (!ret)
1082 ret = ret2;
1083 return ret;
1084 }
1085
1086 static int ext3_writeback_commit_write(struct file *file, struct page *page,
1087 unsigned from, unsigned to)
1088 {
1089 handle_t *handle = ext3_journal_current_handle();
1090 struct inode *inode = page->mapping->host;
1091 int ret = 0, ret2;
1092 loff_t new_i_size;
1093
1094 new_i_size = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1095 if (new_i_size > EXT3_I(inode)->i_disksize)
1096 EXT3_I(inode)->i_disksize = new_i_size;
1097 ret = generic_commit_write(file, page, from, to);
1098 ret2 = ext3_journal_stop(handle);
1099 if (!ret)
1100 ret = ret2;
1101 return ret;
1102 }
1103
1104 static int ext3_journalled_commit_write(struct file *file,
1105 struct page *page, unsigned from, unsigned to)
1106 {
1107 handle_t *handle = ext3_journal_current_handle();
1108 struct inode *inode = page->mapping->host;
1109 int ret = 0, ret2;
1110 int partial = 0;
1111 loff_t pos;
1112
1113 /*
1114 * Here we duplicate the generic_commit_write() functionality
1115 */
1116 pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1117
1118 ret = walk_page_buffers(handle, page_buffers(page), from,
1119 to, &partial, commit_write_fn);
1120 if (!partial)
1121 SetPageUptodate(page);
1122 if (pos > inode->i_size)
1123 i_size_write(inode, pos);
1124 EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
1125 if (inode->i_size > EXT3_I(inode)->i_disksize) {
1126 EXT3_I(inode)->i_disksize = inode->i_size;
1127 ret2 = ext3_mark_inode_dirty(handle, inode);
1128 if (!ret)
1129 ret = ret2;
1130 }
1131 ret2 = ext3_journal_stop(handle);
1132 if (!ret)
1133 ret = ret2;
1134 return ret;
1135 }
1136
1137 /*
1138 * bmap() is special. It gets used by applications such as lilo and by
1139 * the swapper to find the on-disk block of a specific piece of data.
1140 *
1141 * Naturally, this is dangerous if the block concerned is still in the
1142 * journal. If somebody makes a swapfile on an ext3 data-journaling
1143 * filesystem and enables swap, then they may get a nasty shock when the
1144 * data getting swapped to that swapfile suddenly gets overwritten by
1145 * the original zero's written out previously to the journal and
1146 * awaiting writeback in the kernel's buffer cache.
1147 *
1148 * So, if we see any bmap calls here on a modified, data-journaled file,
1149 * take extra steps to flush any blocks which might be in the cache.
1150 */
1151 static sector_t ext3_bmap(struct address_space *mapping, sector_t block)
1152 {
1153 struct inode *inode = mapping->host;
1154 journal_t *journal;
1155 int err;
1156
1157 if (EXT3_I(inode)->i_state & EXT3_STATE_JDATA) {
1158 /*
1159 * This is a REALLY heavyweight approach, but the use of
1160 * bmap on dirty files is expected to be extremely rare:
1161 * only if we run lilo or swapon on a freshly made file
1162 * do we expect this to happen.
1163 *
1164 * (bmap requires CAP_SYS_RAWIO so this does not
1165 * represent an unprivileged user DOS attack --- we'd be
1166 * in trouble if mortal users could trigger this path at
1167 * will.)
1168 *
1169 * NB. EXT3_STATE_JDATA is not set on files other than
1170 * regular files. If somebody wants to bmap a directory
1171 * or symlink and gets confused because the buffer
1172 * hasn't yet been flushed to disk, they deserve
1173 * everything they get.
1174 */
1175
1176 EXT3_I(inode)->i_state &= ~EXT3_STATE_JDATA;
1177 journal = EXT3_JOURNAL(inode);
1178 journal_lock_updates(journal);
1179 err = journal_flush(journal);
1180 journal_unlock_updates(journal);
1181
1182 if (err)
1183 return 0;
1184 }
1185
1186 return generic_block_bmap(mapping,block,ext3_get_block);
1187 }
1188
1189 static int bget_one(handle_t *handle, struct buffer_head *bh)
1190 {
1191 get_bh(bh);
1192 return 0;
1193 }
1194
1195 static int bput_one(handle_t *handle, struct buffer_head *bh)
1196 {
1197 put_bh(bh);
1198 return 0;
1199 }
1200
1201 static int journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh)
1202 {
1203 if (buffer_mapped(bh))
1204 return ext3_journal_dirty_data(handle, bh);
1205 return 0;
1206 }
1207
1208 /*
1209 * Note that we always start a transaction even if we're not journalling
1210 * data. This is to preserve ordering: any hole instantiation within
1211 * __block_write_full_page -> ext3_get_block() should be journalled
1212 * along with the data so we don't crash and then get metadata which
1213 * refers to old data.
1214 *
1215 * In all journalling modes block_write_full_page() will start the I/O.
1216 *
1217 * Problem:
1218 *
1219 * ext3_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1220 * ext3_writepage()
1221 *
1222 * Similar for:
1223 *
1224 * ext3_file_write() -> generic_file_write() -> __alloc_pages() -> ...
1225 *
1226 * Same applies to ext3_get_block(). We will deadlock on various things like
1227 * lock_journal and i_truncate_sem.
1228 *
1229 * Setting PF_MEMALLOC here doesn't work - too many internal memory
1230 * allocations fail.
1231 *
1232 * 16May01: If we're reentered then journal_current_handle() will be
1233 * non-zero. We simply *return*.
1234 *
1235 * 1 July 2001: @@@ FIXME:
1236 * In journalled data mode, a data buffer may be metadata against the
1237 * current transaction. But the same file is part of a shared mapping
1238 * and someone does a writepage() on it.
1239 *
1240 * We will move the buffer onto the async_data list, but *after* it has
1241 * been dirtied. So there's a small window where we have dirty data on
1242 * BJ_Metadata.
1243 *
1244 * Note that this only applies to the last partial page in the file. The
1245 * bit which block_write_full_page() uses prepare/commit for. (That's
1246 * broken code anyway: it's wrong for msync()).
1247 *
1248 * It's a rare case: affects the final partial page, for journalled data
1249 * where the file is subject to bith write() and writepage() in the same
1250 * transction. To fix it we'll need a custom block_write_full_page().
1251 * We'll probably need that anyway for journalling writepage() output.
1252 *
1253 * We don't honour synchronous mounts for writepage(). That would be
1254 * disastrous. Any write() or metadata operation will sync the fs for
1255 * us.
1256 *
1257 * AKPM2: if all the page's buffers are mapped to disk and !data=journal,
1258 * we don't need to open a transaction here.
1259 */
1260 static int ext3_ordered_writepage(struct page *page,
1261 struct writeback_control *wbc)
1262 {
1263 struct inode *inode = page->mapping->host;
1264 struct buffer_head *page_bufs;
1265 handle_t *handle = NULL;
1266 int ret = 0;
1267 int err;
1268
1269 J_ASSERT(PageLocked(page));
1270
1271 /*
1272 * We give up here if we're reentered, because it might be for a
1273 * different filesystem.
1274 */
1275 if (ext3_journal_current_handle())
1276 goto out_fail;
1277
1278 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1279
1280 if (IS_ERR(handle)) {
1281 ret = PTR_ERR(handle);
1282 goto out_fail;
1283 }
1284
1285 if (!page_has_buffers(page)) {
1286 create_empty_buffers(page, inode->i_sb->s_blocksize,
1287 (1 << BH_Dirty)|(1 << BH_Uptodate));
1288 }
1289 page_bufs = page_buffers(page);
1290 walk_page_buffers(handle, page_bufs, 0,
1291 PAGE_CACHE_SIZE, NULL, bget_one);
1292
1293 ret = block_write_full_page(page, ext3_get_block, wbc);
1294
1295 /*
1296 * The page can become unlocked at any point now, and
1297 * truncate can then come in and change things. So we
1298 * can't touch *page from now on. But *page_bufs is
1299 * safe due to elevated refcount.
1300 */
1301
1302 /*
1303 * And attach them to the current transaction. But only if
1304 * block_write_full_page() succeeded. Otherwise they are unmapped,
1305 * and generally junk.
1306 */
1307 if (ret == 0) {
1308 err = walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE,
1309 NULL, journal_dirty_data_fn);
1310 if (!ret)
1311 ret = err;
1312 }
1313 walk_page_buffers(handle, page_bufs, 0,
1314 PAGE_CACHE_SIZE, NULL, bput_one);
1315 err = ext3_journal_stop(handle);
1316 if (!ret)
1317 ret = err;
1318 return ret;
1319
1320 out_fail:
1321 redirty_page_for_writepage(wbc, page);
1322 unlock_page(page);
1323 return ret;
1324 }
1325
1326 static int ext3_writeback_writepage(struct page *page,
1327 struct writeback_control *wbc)
1328 {
1329 struct inode *inode = page->mapping->host;
1330 handle_t *handle = NULL;
1331 int ret = 0;
1332 int err;
1333
1334 if (ext3_journal_current_handle())
1335 goto out_fail;
1336
1337 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1338 if (IS_ERR(handle)) {
1339 ret = PTR_ERR(handle);
1340 goto out_fail;
1341 }
1342
1343 ret = block_write_full_page(page, ext3_get_block, wbc);
1344 err = ext3_journal_stop(handle);
1345 if (!ret)
1346 ret = err;
1347 return ret;
1348
1349 out_fail:
1350 redirty_page_for_writepage(wbc, page);
1351 unlock_page(page);
1352 return ret;
1353 }
1354
1355 static int ext3_journalled_writepage(struct page *page,
1356 struct writeback_control *wbc)
1357 {
1358 struct inode *inode = page->mapping->host;
1359 handle_t *handle = NULL;
1360 int ret = 0;
1361 int err;
1362
1363 if (ext3_journal_current_handle())
1364 goto no_write;
1365
1366 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1367 if (IS_ERR(handle)) {
1368 ret = PTR_ERR(handle);
1369 goto no_write;
1370 }
1371
1372 if (!page_has_buffers(page) || PageChecked(page)) {
1373 /*
1374 * It's mmapped pagecache. Add buffers and journal it. There
1375 * doesn't seem much point in redirtying the page here.
1376 */
1377 ClearPageChecked(page);
1378 ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
1379 ext3_get_block);
1380 if (ret != 0)
1381 goto out_unlock;
1382 ret = walk_page_buffers(handle, page_buffers(page), 0,
1383 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
1384
1385 err = walk_page_buffers(handle, page_buffers(page), 0,
1386 PAGE_CACHE_SIZE, NULL, commit_write_fn);
1387 if (ret == 0)
1388 ret = err;
1389 EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
1390 unlock_page(page);
1391 } else {
1392 /*
1393 * It may be a page full of checkpoint-mode buffers. We don't
1394 * really know unless we go poke around in the buffer_heads.
1395 * But block_write_full_page will do the right thing.
1396 */
1397 ret = block_write_full_page(page, ext3_get_block, wbc);
1398 }
1399 err = ext3_journal_stop(handle);
1400 if (!ret)
1401 ret = err;
1402 out:
1403 return ret;
1404
1405 no_write:
1406 redirty_page_for_writepage(wbc, page);
1407 out_unlock:
1408 unlock_page(page);
1409 goto out;
1410 }
1411
1412 static int ext3_readpage(struct file *file, struct page *page)
1413 {
1414 return mpage_readpage(page, ext3_get_block);
1415 }
1416
1417 static int
1418 ext3_readpages(struct file *file, struct address_space *mapping,
1419 struct list_head *pages, unsigned nr_pages)
1420 {
1421 return mpage_readpages(mapping, pages, nr_pages, ext3_get_block);
1422 }
1423
1424 static int ext3_invalidatepage(struct page *page, unsigned long offset)
1425 {
1426 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1427
1428 /*
1429 * If it's a full truncate we just forget about the pending dirtying
1430 */
1431 if (offset == 0)
1432 ClearPageChecked(page);
1433
1434 return journal_invalidatepage(journal, page, offset);
1435 }
1436
1437 static int ext3_releasepage(struct page *page, int wait)
1438 {
1439 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1440
1441 WARN_ON(PageChecked(page));
1442 return journal_try_to_free_buffers(journal, page, wait);
1443 }
1444
1445 /*
1446 * If the O_DIRECT write will extend the file then add this inode to the
1447 * orphan list. So recovery will truncate it back to the original size
1448 * if the machine crashes during the write.
1449 *
1450 * If the O_DIRECT write is intantiating holes inside i_size and the machine
1451 * crashes then stale disk data _may_ be exposed inside the file.
1452 */
1453 static ssize_t ext3_direct_IO(int rw, struct kiocb *iocb,
1454 const struct iovec *iov, loff_t offset,
1455 unsigned long nr_segs)
1456 {
1457 struct file *file = iocb->ki_filp;
1458 struct inode *inode = file->f_mapping->host;
1459 struct ext3_inode_info *ei = EXT3_I(inode);
1460 handle_t *handle = NULL;
1461 ssize_t ret;
1462 int orphan = 0;
1463 size_t count = iov_length(iov, nr_segs);
1464
1465 if (rw == WRITE) {
1466 loff_t final_size = offset + count;
1467
1468 handle = ext3_journal_start(inode, DIO_CREDITS);
1469 if (IS_ERR(handle)) {
1470 ret = PTR_ERR(handle);
1471 goto out;
1472 }
1473 if (final_size > inode->i_size) {
1474 ret = ext3_orphan_add(handle, inode);
1475 if (ret)
1476 goto out_stop;
1477 orphan = 1;
1478 ei->i_disksize = inode->i_size;
1479 }
1480 }
1481
1482 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1483 offset, nr_segs,
1484 ext3_direct_io_get_blocks, NULL);
1485
1486 /*
1487 * Reacquire the handle: ext3_direct_io_get_block() can restart the
1488 * transaction
1489 */
1490 handle = journal_current_handle();
1491
1492 out_stop:
1493 if (handle) {
1494 int err;
1495
1496 if (orphan && inode->i_nlink)
1497 ext3_orphan_del(handle, inode);
1498 if (orphan && ret > 0) {
1499 loff_t end = offset + ret;
1500 if (end > inode->i_size) {
1501 ei->i_disksize = end;
1502 i_size_write(inode, end);
1503 /*
1504 * We're going to return a positive `ret'
1505 * here due to non-zero-length I/O, so there's
1506 * no way of reporting error returns from
1507 * ext3_mark_inode_dirty() to userspace. So
1508 * ignore it.
1509 */
1510 ext3_mark_inode_dirty(handle, inode);
1511 }
1512 }
1513 err = ext3_journal_stop(handle);
1514 if (ret == 0)
1515 ret = err;
1516 }
1517 out:
1518 return ret;
1519 }
1520
1521 /*
1522 * Pages can be marked dirty completely asynchronously from ext3's journalling
1523 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
1524 * much here because ->set_page_dirty is called under VFS locks. The page is
1525 * not necessarily locked.
1526 *
1527 * We cannot just dirty the page and leave attached buffers clean, because the
1528 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
1529 * or jbddirty because all the journalling code will explode.
1530 *
1531 * So what we do is to mark the page "pending dirty" and next time writepage
1532 * is called, propagate that into the buffers appropriately.
1533 */
1534 static int ext3_journalled_set_page_dirty(struct page *page)
1535 {
1536 SetPageChecked(page);
1537 return __set_page_dirty_nobuffers(page);
1538 }
1539
1540 static struct address_space_operations ext3_ordered_aops = {
1541 .readpage = ext3_readpage,
1542 .readpages = ext3_readpages,
1543 .writepage = ext3_ordered_writepage,
1544 .sync_page = block_sync_page,
1545 .prepare_write = ext3_prepare_write,
1546 .commit_write = ext3_ordered_commit_write,
1547 .bmap = ext3_bmap,
1548 .invalidatepage = ext3_invalidatepage,
1549 .releasepage = ext3_releasepage,
1550 .direct_IO = ext3_direct_IO,
1551 };
1552
1553 static struct address_space_operations ext3_writeback_aops = {
1554 .readpage = ext3_readpage,
1555 .readpages = ext3_readpages,
1556 .writepage = ext3_writeback_writepage,
1557 .sync_page = block_sync_page,
1558 .prepare_write = ext3_prepare_write,
1559 .commit_write = ext3_writeback_commit_write,
1560 .bmap = ext3_bmap,
1561 .invalidatepage = ext3_invalidatepage,
1562 .releasepage = ext3_releasepage,
1563 .direct_IO = ext3_direct_IO,
1564 };
1565
1566 static struct address_space_operations ext3_journalled_aops = {
1567 .readpage = ext3_readpage,
1568 .readpages = ext3_readpages,
1569 .writepage = ext3_journalled_writepage,
1570 .sync_page = block_sync_page,
1571 .prepare_write = ext3_prepare_write,
1572 .commit_write = ext3_journalled_commit_write,
1573 .set_page_dirty = ext3_journalled_set_page_dirty,
1574 .bmap = ext3_bmap,
1575 .invalidatepage = ext3_invalidatepage,
1576 .releasepage = ext3_releasepage,
1577 };
1578
1579 void ext3_set_aops(struct inode *inode)
1580 {
1581 if (ext3_should_order_data(inode))
1582 inode->i_mapping->a_ops = &ext3_ordered_aops;
1583 else if (ext3_should_writeback_data(inode))
1584 inode->i_mapping->a_ops = &ext3_writeback_aops;
1585 else
1586 inode->i_mapping->a_ops = &ext3_journalled_aops;
1587 }
1588
1589 /*
1590 * ext3_block_truncate_page() zeroes out a mapping from file offset `from'
1591 * up to the end of the block which corresponds to `from'.
1592 * This required during truncate. We need to physically zero the tail end
1593 * of that block so it doesn't yield old data if the file is later grown.
1594 */
1595 static int ext3_block_truncate_page(handle_t *handle, struct page *page,
1596 struct address_space *mapping, loff_t from)
1597 {
1598 unsigned long index = from >> PAGE_CACHE_SHIFT;
1599 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1600 unsigned blocksize, iblock, length, pos;
1601 struct inode *inode = mapping->host;
1602 struct buffer_head *bh;
1603 int err;
1604 void *kaddr;
1605
1606 blocksize = inode->i_sb->s_blocksize;
1607 length = blocksize - (offset & (blocksize - 1));
1608 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1609
1610 if (!page_has_buffers(page))
1611 create_empty_buffers(page, blocksize, 0);
1612
1613 /* Find the buffer that contains "offset" */
1614 bh = page_buffers(page);
1615 pos = blocksize;
1616 while (offset >= pos) {
1617 bh = bh->b_this_page;
1618 iblock++;
1619 pos += blocksize;
1620 }
1621
1622 err = 0;
1623 if (buffer_freed(bh)) {
1624 BUFFER_TRACE(bh, "freed: skip");
1625 goto unlock;
1626 }
1627
1628 if (!buffer_mapped(bh)) {
1629 BUFFER_TRACE(bh, "unmapped");
1630 ext3_get_block(inode, iblock, bh, 0);
1631 /* unmapped? It's a hole - nothing to do */
1632 if (!buffer_mapped(bh)) {
1633 BUFFER_TRACE(bh, "still unmapped");
1634 goto unlock;
1635 }
1636 }
1637
1638 /* Ok, it's mapped. Make sure it's up-to-date */
1639 if (PageUptodate(page))
1640 set_buffer_uptodate(bh);
1641
1642 if (!buffer_uptodate(bh)) {
1643 err = -EIO;
1644 ll_rw_block(READ, 1, &bh);
1645 wait_on_buffer(bh);
1646 /* Uhhuh. Read error. Complain and punt. */
1647 if (!buffer_uptodate(bh))
1648 goto unlock;
1649 }
1650
1651 if (ext3_should_journal_data(inode)) {
1652 BUFFER_TRACE(bh, "get write access");
1653 err = ext3_journal_get_write_access(handle, bh);
1654 if (err)
1655 goto unlock;
1656 }
1657
1658 kaddr = kmap_atomic(page, KM_USER0);
1659 memset(kaddr + offset, 0, length);
1660 flush_dcache_page(page);
1661 kunmap_atomic(kaddr, KM_USER0);
1662
1663 BUFFER_TRACE(bh, "zeroed end of block");
1664
1665 err = 0;
1666 if (ext3_should_journal_data(inode)) {
1667 err = ext3_journal_dirty_metadata(handle, bh);
1668 } else {
1669 if (ext3_should_order_data(inode))
1670 err = ext3_journal_dirty_data(handle, bh);
1671 mark_buffer_dirty(bh);
1672 }
1673
1674 unlock:
1675 unlock_page(page);
1676 page_cache_release(page);
1677 return err;
1678 }
1679
1680 /*
1681 * Probably it should be a library function... search for first non-zero word
1682 * or memcmp with zero_page, whatever is better for particular architecture.
1683 * Linus?
1684 */
1685 static inline int all_zeroes(__le32 *p, __le32 *q)
1686 {
1687 while (p < q)
1688 if (*p++)
1689 return 0;
1690 return 1;
1691 }
1692
1693 /**
1694 * ext3_find_shared - find the indirect blocks for partial truncation.
1695 * @inode: inode in question
1696 * @depth: depth of the affected branch
1697 * @offsets: offsets of pointers in that branch (see ext3_block_to_path)
1698 * @chain: place to store the pointers to partial indirect blocks
1699 * @top: place to the (detached) top of branch
1700 *
1701 * This is a helper function used by ext3_truncate().
1702 *
1703 * When we do truncate() we may have to clean the ends of several
1704 * indirect blocks but leave the blocks themselves alive. Block is
1705 * partially truncated if some data below the new i_size is refered
1706 * from it (and it is on the path to the first completely truncated
1707 * data block, indeed). We have to free the top of that path along
1708 * with everything to the right of the path. Since no allocation
1709 * past the truncation point is possible until ext3_truncate()
1710 * finishes, we may safely do the latter, but top of branch may
1711 * require special attention - pageout below the truncation point
1712 * might try to populate it.
1713 *
1714 * We atomically detach the top of branch from the tree, store the
1715 * block number of its root in *@top, pointers to buffer_heads of
1716 * partially truncated blocks - in @chain[].bh and pointers to
1717 * their last elements that should not be removed - in
1718 * @chain[].p. Return value is the pointer to last filled element
1719 * of @chain.
1720 *
1721 * The work left to caller to do the actual freeing of subtrees:
1722 * a) free the subtree starting from *@top
1723 * b) free the subtrees whose roots are stored in
1724 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
1725 * c) free the subtrees growing from the inode past the @chain[0].
1726 * (no partially truncated stuff there). */
1727
1728 static Indirect *ext3_find_shared(struct inode *inode,
1729 int depth,
1730 int offsets[4],
1731 Indirect chain[4],
1732 __le32 *top)
1733 {
1734 Indirect *partial, *p;
1735 int k, err;
1736
1737 *top = 0;
1738 /* Make k index the deepest non-null offest + 1 */
1739 for (k = depth; k > 1 && !offsets[k-1]; k--)
1740 ;
1741 partial = ext3_get_branch(inode, k, offsets, chain, &err);
1742 /* Writer: pointers */
1743 if (!partial)
1744 partial = chain + k-1;
1745 /*
1746 * If the branch acquired continuation since we've looked at it -
1747 * fine, it should all survive and (new) top doesn't belong to us.
1748 */
1749 if (!partial->key && *partial->p)
1750 /* Writer: end */
1751 goto no_top;
1752 for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
1753 ;
1754 /*
1755 * OK, we've found the last block that must survive. The rest of our
1756 * branch should be detached before unlocking. However, if that rest
1757 * of branch is all ours and does not grow immediately from the inode
1758 * it's easier to cheat and just decrement partial->p.
1759 */
1760 if (p == chain + k - 1 && p > chain) {
1761 p->p--;
1762 } else {
1763 *top = *p->p;
1764 /* Nope, don't do this in ext3. Must leave the tree intact */
1765 #if 0
1766 *p->p = 0;
1767 #endif
1768 }
1769 /* Writer: end */
1770
1771 while(partial > p)
1772 {
1773 brelse(partial->bh);
1774 partial--;
1775 }
1776 no_top:
1777 return partial;
1778 }
1779
1780 /*
1781 * Zero a number of block pointers in either an inode or an indirect block.
1782 * If we restart the transaction we must again get write access to the
1783 * indirect block for further modification.
1784 *
1785 * We release `count' blocks on disk, but (last - first) may be greater
1786 * than `count' because there can be holes in there.
1787 */
1788 static void
1789 ext3_clear_blocks(handle_t *handle, struct inode *inode, struct buffer_head *bh,
1790 unsigned long block_to_free, unsigned long count,
1791 __le32 *first, __le32 *last)
1792 {
1793 __le32 *p;
1794 if (try_to_extend_transaction(handle, inode)) {
1795 if (bh) {
1796 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1797 ext3_journal_dirty_metadata(handle, bh);
1798 }
1799 ext3_mark_inode_dirty(handle, inode);
1800 ext3_journal_test_restart(handle, inode);
1801 if (bh) {
1802 BUFFER_TRACE(bh, "retaking write access");
1803 ext3_journal_get_write_access(handle, bh);
1804 }
1805 }
1806
1807 /*
1808 * Any buffers which are on the journal will be in memory. We find
1809 * them on the hash table so journal_revoke() will run journal_forget()
1810 * on them. We've already detached each block from the file, so
1811 * bforget() in journal_forget() should be safe.
1812 *
1813 * AKPM: turn on bforget in journal_forget()!!!
1814 */
1815 for (p = first; p < last; p++) {
1816 u32 nr = le32_to_cpu(*p);
1817 if (nr) {
1818 struct buffer_head *bh;
1819
1820 *p = 0;
1821 bh = sb_find_get_block(inode->i_sb, nr);
1822 ext3_forget(handle, 0, inode, bh, nr);
1823 }
1824 }
1825
1826 ext3_free_blocks(handle, inode, block_to_free, count);
1827 }
1828
1829 /**
1830 * ext3_free_data - free a list of data blocks
1831 * @handle: handle for this transaction
1832 * @inode: inode we are dealing with
1833 * @this_bh: indirect buffer_head which contains *@first and *@last
1834 * @first: array of block numbers
1835 * @last: points immediately past the end of array
1836 *
1837 * We are freeing all blocks refered from that array (numbers are stored as
1838 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
1839 *
1840 * We accumulate contiguous runs of blocks to free. Conveniently, if these
1841 * blocks are contiguous then releasing them at one time will only affect one
1842 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
1843 * actually use a lot of journal space.
1844 *
1845 * @this_bh will be %NULL if @first and @last point into the inode's direct
1846 * block pointers.
1847 */
1848 static void ext3_free_data(handle_t *handle, struct inode *inode,
1849 struct buffer_head *this_bh,
1850 __le32 *first, __le32 *last)
1851 {
1852 unsigned long block_to_free = 0; /* Starting block # of a run */
1853 unsigned long count = 0; /* Number of blocks in the run */
1854 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
1855 corresponding to
1856 block_to_free */
1857 unsigned long nr; /* Current block # */
1858 __le32 *p; /* Pointer into inode/ind
1859 for current block */
1860 int err;
1861
1862 if (this_bh) { /* For indirect block */
1863 BUFFER_TRACE(this_bh, "get_write_access");
1864 err = ext3_journal_get_write_access(handle, this_bh);
1865 /* Important: if we can't update the indirect pointers
1866 * to the blocks, we can't free them. */
1867 if (err)
1868 return;
1869 }
1870
1871 for (p = first; p < last; p++) {
1872 nr = le32_to_cpu(*p);
1873 if (nr) {
1874 /* accumulate blocks to free if they're contiguous */
1875 if (count == 0) {
1876 block_to_free = nr;
1877 block_to_free_p = p;
1878 count = 1;
1879 } else if (nr == block_to_free + count) {
1880 count++;
1881 } else {
1882 ext3_clear_blocks(handle, inode, this_bh,
1883 block_to_free,
1884 count, block_to_free_p, p);
1885 block_to_free = nr;
1886 block_to_free_p = p;
1887 count = 1;
1888 }
1889 }
1890 }
1891
1892 if (count > 0)
1893 ext3_clear_blocks(handle, inode, this_bh, block_to_free,
1894 count, block_to_free_p, p);
1895
1896 if (this_bh) {
1897 BUFFER_TRACE(this_bh, "call ext3_journal_dirty_metadata");
1898 ext3_journal_dirty_metadata(handle, this_bh);
1899 }
1900 }
1901
1902 /**
1903 * ext3_free_branches - free an array of branches
1904 * @handle: JBD handle for this transaction
1905 * @inode: inode we are dealing with
1906 * @parent_bh: the buffer_head which contains *@first and *@last
1907 * @first: array of block numbers
1908 * @last: pointer immediately past the end of array
1909 * @depth: depth of the branches to free
1910 *
1911 * We are freeing all blocks refered from these branches (numbers are
1912 * stored as little-endian 32-bit) and updating @inode->i_blocks
1913 * appropriately.
1914 */
1915 static void ext3_free_branches(handle_t *handle, struct inode *inode,
1916 struct buffer_head *parent_bh,
1917 __le32 *first, __le32 *last, int depth)
1918 {
1919 unsigned long nr;
1920 __le32 *p;
1921
1922 if (is_handle_aborted(handle))
1923 return;
1924
1925 if (depth--) {
1926 struct buffer_head *bh;
1927 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
1928 p = last;
1929 while (--p >= first) {
1930 nr = le32_to_cpu(*p);
1931 if (!nr)
1932 continue; /* A hole */
1933
1934 /* Go read the buffer for the next level down */
1935 bh = sb_bread(inode->i_sb, nr);
1936
1937 /*
1938 * A read failure? Report error and clear slot
1939 * (should be rare).
1940 */
1941 if (!bh) {
1942 ext3_error(inode->i_sb, "ext3_free_branches",
1943 "Read failure, inode=%ld, block=%ld",
1944 inode->i_ino, nr);
1945 continue;
1946 }
1947
1948 /* This zaps the entire block. Bottom up. */
1949 BUFFER_TRACE(bh, "free child branches");
1950 ext3_free_branches(handle, inode, bh,
1951 (__le32*)bh->b_data,
1952 (__le32*)bh->b_data + addr_per_block,
1953 depth);
1954
1955 /*
1956 * We've probably journalled the indirect block several
1957 * times during the truncate. But it's no longer
1958 * needed and we now drop it from the transaction via
1959 * journal_revoke().
1960 *
1961 * That's easy if it's exclusively part of this
1962 * transaction. But if it's part of the committing
1963 * transaction then journal_forget() will simply
1964 * brelse() it. That means that if the underlying
1965 * block is reallocated in ext3_get_block(),
1966 * unmap_underlying_metadata() will find this block
1967 * and will try to get rid of it. damn, damn.
1968 *
1969 * If this block has already been committed to the
1970 * journal, a revoke record will be written. And
1971 * revoke records must be emitted *before* clearing
1972 * this block's bit in the bitmaps.
1973 */
1974 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
1975
1976 /*
1977 * Everything below this this pointer has been
1978 * released. Now let this top-of-subtree go.
1979 *
1980 * We want the freeing of this indirect block to be
1981 * atomic in the journal with the updating of the
1982 * bitmap block which owns it. So make some room in
1983 * the journal.
1984 *
1985 * We zero the parent pointer *after* freeing its
1986 * pointee in the bitmaps, so if extend_transaction()
1987 * for some reason fails to put the bitmap changes and
1988 * the release into the same transaction, recovery
1989 * will merely complain about releasing a free block,
1990 * rather than leaking blocks.
1991 */
1992 if (is_handle_aborted(handle))
1993 return;
1994 if (try_to_extend_transaction(handle, inode)) {
1995 ext3_mark_inode_dirty(handle, inode);
1996 ext3_journal_test_restart(handle, inode);
1997 }
1998
1999 ext3_free_blocks(handle, inode, nr, 1);
2000
2001 if (parent_bh) {
2002 /*
2003 * The block which we have just freed is
2004 * pointed to by an indirect block: journal it
2005 */
2006 BUFFER_TRACE(parent_bh, "get_write_access");
2007 if (!ext3_journal_get_write_access(handle,
2008 parent_bh)){
2009 *p = 0;
2010 BUFFER_TRACE(parent_bh,
2011 "call ext3_journal_dirty_metadata");
2012 ext3_journal_dirty_metadata(handle,
2013 parent_bh);
2014 }
2015 }
2016 }
2017 } else {
2018 /* We have reached the bottom of the tree. */
2019 BUFFER_TRACE(parent_bh, "free data blocks");
2020 ext3_free_data(handle, inode, parent_bh, first, last);
2021 }
2022 }
2023
2024 /*
2025 * ext3_truncate()
2026 *
2027 * We block out ext3_get_block() block instantiations across the entire
2028 * transaction, and VFS/VM ensures that ext3_truncate() cannot run
2029 * simultaneously on behalf of the same inode.
2030 *
2031 * As we work through the truncate and commmit bits of it to the journal there
2032 * is one core, guiding principle: the file's tree must always be consistent on
2033 * disk. We must be able to restart the truncate after a crash.
2034 *
2035 * The file's tree may be transiently inconsistent in memory (although it
2036 * probably isn't), but whenever we close off and commit a journal transaction,
2037 * the contents of (the filesystem + the journal) must be consistent and
2038 * restartable. It's pretty simple, really: bottom up, right to left (although
2039 * left-to-right works OK too).
2040 *
2041 * Note that at recovery time, journal replay occurs *before* the restart of
2042 * truncate against the orphan inode list.
2043 *
2044 * The committed inode has the new, desired i_size (which is the same as
2045 * i_disksize in this case). After a crash, ext3_orphan_cleanup() will see
2046 * that this inode's truncate did not complete and it will again call
2047 * ext3_truncate() to have another go. So there will be instantiated blocks
2048 * to the right of the truncation point in a crashed ext3 filesystem. But
2049 * that's fine - as long as they are linked from the inode, the post-crash
2050 * ext3_truncate() run will find them and release them.
2051 */
2052
2053 void ext3_truncate(struct inode * inode)
2054 {
2055 handle_t *handle;
2056 struct ext3_inode_info *ei = EXT3_I(inode);
2057 __le32 *i_data = ei->i_data;
2058 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
2059 struct address_space *mapping = inode->i_mapping;
2060 int offsets[4];
2061 Indirect chain[4];
2062 Indirect *partial;
2063 __le32 nr = 0;
2064 int n;
2065 long last_block;
2066 unsigned blocksize = inode->i_sb->s_blocksize;
2067 struct page *page;
2068
2069 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2070 S_ISLNK(inode->i_mode)))
2071 return;
2072 if (ext3_inode_is_fast_symlink(inode))
2073 return;
2074 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2075 return;
2076
2077 ext3_discard_reservation(inode);
2078
2079 /*
2080 * We have to lock the EOF page here, because lock_page() nests
2081 * outside journal_start().
2082 */
2083 if ((inode->i_size & (blocksize - 1)) == 0) {
2084 /* Block boundary? Nothing to do */
2085 page = NULL;
2086 } else {
2087 page = grab_cache_page(mapping,
2088 inode->i_size >> PAGE_CACHE_SHIFT);
2089 if (!page)
2090 return;
2091 }
2092
2093 handle = start_transaction(inode);
2094 if (IS_ERR(handle)) {
2095 if (page) {
2096 clear_highpage(page);
2097 flush_dcache_page(page);
2098 unlock_page(page);
2099 page_cache_release(page);
2100 }
2101 return; /* AKPM: return what? */
2102 }
2103
2104 last_block = (inode->i_size + blocksize-1)
2105 >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
2106
2107 if (page)
2108 ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2109
2110 n = ext3_block_to_path(inode, last_block, offsets, NULL);
2111 if (n == 0)
2112 goto out_stop; /* error */
2113
2114 /*
2115 * OK. This truncate is going to happen. We add the inode to the
2116 * orphan list, so that if this truncate spans multiple transactions,
2117 * and we crash, we will resume the truncate when the filesystem
2118 * recovers. It also marks the inode dirty, to catch the new size.
2119 *
2120 * Implication: the file must always be in a sane, consistent
2121 * truncatable state while each transaction commits.
2122 */
2123 if (ext3_orphan_add(handle, inode))
2124 goto out_stop;
2125
2126 /*
2127 * The orphan list entry will now protect us from any crash which
2128 * occurs before the truncate completes, so it is now safe to propagate
2129 * the new, shorter inode size (held for now in i_size) into the
2130 * on-disk inode. We do this via i_disksize, which is the value which
2131 * ext3 *really* writes onto the disk inode.
2132 */
2133 ei->i_disksize = inode->i_size;
2134
2135 /*
2136 * From here we block out all ext3_get_block() callers who want to
2137 * modify the block allocation tree.
2138 */
2139 down(&ei->truncate_sem);
2140
2141 if (n == 1) { /* direct blocks */
2142 ext3_free_data(handle, inode, NULL, i_data+offsets[0],
2143 i_data + EXT3_NDIR_BLOCKS);
2144 goto do_indirects;
2145 }
2146
2147 partial = ext3_find_shared(inode, n, offsets, chain, &nr);
2148 /* Kill the top of shared branch (not detached) */
2149 if (nr) {
2150 if (partial == chain) {
2151 /* Shared branch grows from the inode */
2152 ext3_free_branches(handle, inode, NULL,
2153 &nr, &nr+1, (chain+n-1) - partial);
2154 *partial->p = 0;
2155 /*
2156 * We mark the inode dirty prior to restart,
2157 * and prior to stop. No need for it here.
2158 */
2159 } else {
2160 /* Shared branch grows from an indirect block */
2161 BUFFER_TRACE(partial->bh, "get_write_access");
2162 ext3_free_branches(handle, inode, partial->bh,
2163 partial->p,
2164 partial->p+1, (chain+n-1) - partial);
2165 }
2166 }
2167 /* Clear the ends of indirect blocks on the shared branch */
2168 while (partial > chain) {
2169 ext3_free_branches(handle, inode, partial->bh, partial->p + 1,
2170 (__le32*)partial->bh->b_data+addr_per_block,
2171 (chain+n-1) - partial);
2172 BUFFER_TRACE(partial->bh, "call brelse");
2173 brelse (partial->bh);
2174 partial--;
2175 }
2176 do_indirects:
2177 /* Kill the remaining (whole) subtrees */
2178 switch (offsets[0]) {
2179 default:
2180 nr = i_data[EXT3_IND_BLOCK];
2181 if (nr) {
2182 ext3_free_branches(handle, inode, NULL,
2183 &nr, &nr+1, 1);
2184 i_data[EXT3_IND_BLOCK] = 0;
2185 }
2186 case EXT3_IND_BLOCK:
2187 nr = i_data[EXT3_DIND_BLOCK];
2188 if (nr) {
2189 ext3_free_branches(handle, inode, NULL,
2190 &nr, &nr+1, 2);
2191 i_data[EXT3_DIND_BLOCK] = 0;
2192 }
2193 case EXT3_DIND_BLOCK:
2194 nr = i_data[EXT3_TIND_BLOCK];
2195 if (nr) {
2196 ext3_free_branches(handle, inode, NULL,
2197 &nr, &nr+1, 3);
2198 i_data[EXT3_TIND_BLOCK] = 0;
2199 }
2200 case EXT3_TIND_BLOCK:
2201 ;
2202 }
2203 up(&ei->truncate_sem);
2204 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
2205 ext3_mark_inode_dirty(handle, inode);
2206
2207 /* In a multi-transaction truncate, we only make the final
2208 * transaction synchronous */
2209 if (IS_SYNC(inode))
2210 handle->h_sync = 1;
2211 out_stop:
2212 /*
2213 * If this was a simple ftruncate(), and the file will remain alive
2214 * then we need to clear up the orphan record which we created above.
2215 * However, if this was a real unlink then we were called by
2216 * ext3_delete_inode(), and we allow that function to clean up the
2217 * orphan info for us.
2218 */
2219 if (inode->i_nlink)
2220 ext3_orphan_del(handle, inode);
2221
2222 ext3_journal_stop(handle);
2223 }
2224
2225 static unsigned long ext3_get_inode_block(struct super_block *sb,
2226 unsigned long ino, struct ext3_iloc *iloc)
2227 {
2228 unsigned long desc, group_desc, block_group;
2229 unsigned long offset, block;
2230 struct buffer_head *bh;
2231 struct ext3_group_desc * gdp;
2232
2233
2234 if ((ino != EXT3_ROOT_INO &&
2235 ino != EXT3_JOURNAL_INO &&
2236 ino != EXT3_RESIZE_INO &&
2237 ino < EXT3_FIRST_INO(sb)) ||
2238 ino > le32_to_cpu(
2239 EXT3_SB(sb)->s_es->s_inodes_count)) {
2240 ext3_error (sb, "ext3_get_inode_block",
2241 "bad inode number: %lu", ino);
2242 return 0;
2243 }
2244 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
2245 if (block_group >= EXT3_SB(sb)->s_groups_count) {
2246 ext3_error (sb, "ext3_get_inode_block",
2247 "group >= groups count");
2248 return 0;
2249 }
2250 smp_rmb();
2251 group_desc = block_group >> EXT3_DESC_PER_BLOCK_BITS(sb);
2252 desc = block_group & (EXT3_DESC_PER_BLOCK(sb) - 1);
2253 bh = EXT3_SB(sb)->s_group_desc[group_desc];
2254 if (!bh) {
2255 ext3_error (sb, "ext3_get_inode_block",
2256 "Descriptor not loaded");
2257 return 0;
2258 }
2259
2260 gdp = (struct ext3_group_desc *) bh->b_data;
2261 /*
2262 * Figure out the offset within the block group inode table
2263 */
2264 offset = ((ino - 1) % EXT3_INODES_PER_GROUP(sb)) *
2265 EXT3_INODE_SIZE(sb);
2266 block = le32_to_cpu(gdp[desc].bg_inode_table) +
2267 (offset >> EXT3_BLOCK_SIZE_BITS(sb));
2268
2269 iloc->block_group = block_group;
2270 iloc->offset = offset & (EXT3_BLOCK_SIZE(sb) - 1);
2271 return block;
2272 }
2273
2274 /*
2275 * ext3_get_inode_loc returns with an extra refcount against the inode's
2276 * underlying buffer_head on success. If 'in_mem' is true, we have all
2277 * data in memory that is needed to recreate the on-disk version of this
2278 * inode.
2279 */
2280 static int __ext3_get_inode_loc(struct inode *inode,
2281 struct ext3_iloc *iloc, int in_mem)
2282 {
2283 unsigned long block;
2284 struct buffer_head *bh;
2285
2286 block = ext3_get_inode_block(inode->i_sb, inode->i_ino, iloc);
2287 if (!block)
2288 return -EIO;
2289
2290 bh = sb_getblk(inode->i_sb, block);
2291 if (!bh) {
2292 ext3_error (inode->i_sb, "ext3_get_inode_loc",
2293 "unable to read inode block - "
2294 "inode=%lu, block=%lu", inode->i_ino, block);
2295 return -EIO;
2296 }
2297 if (!buffer_uptodate(bh)) {
2298 lock_buffer(bh);
2299 if (buffer_uptodate(bh)) {
2300 /* someone brought it uptodate while we waited */
2301 unlock_buffer(bh);
2302 goto has_buffer;
2303 }
2304
2305 /*
2306 * If we have all information of the inode in memory and this
2307 * is the only valid inode in the block, we need not read the
2308 * block.
2309 */
2310 if (in_mem) {
2311 struct buffer_head *bitmap_bh;
2312 struct ext3_group_desc *desc;
2313 int inodes_per_buffer;
2314 int inode_offset, i;
2315 int block_group;
2316 int start;
2317
2318 block_group = (inode->i_ino - 1) /
2319 EXT3_INODES_PER_GROUP(inode->i_sb);
2320 inodes_per_buffer = bh->b_size /
2321 EXT3_INODE_SIZE(inode->i_sb);
2322 inode_offset = ((inode->i_ino - 1) %
2323 EXT3_INODES_PER_GROUP(inode->i_sb));
2324 start = inode_offset & ~(inodes_per_buffer - 1);
2325
2326 /* Is the inode bitmap in cache? */
2327 desc = ext3_get_group_desc(inode->i_sb,
2328 block_group, NULL);
2329 if (!desc)
2330 goto make_io;
2331
2332 bitmap_bh = sb_getblk(inode->i_sb,
2333 le32_to_cpu(desc->bg_inode_bitmap));
2334 if (!bitmap_bh)
2335 goto make_io;
2336
2337 /*
2338 * If the inode bitmap isn't in cache then the
2339 * optimisation may end up performing two reads instead
2340 * of one, so skip it.
2341 */
2342 if (!buffer_uptodate(bitmap_bh)) {
2343 brelse(bitmap_bh);
2344 goto make_io;
2345 }
2346 for (i = start; i < start + inodes_per_buffer; i++) {
2347 if (i == inode_offset)
2348 continue;
2349 if (ext3_test_bit(i, bitmap_bh->b_data))
2350 break;
2351 }
2352 brelse(bitmap_bh);
2353 if (i == start + inodes_per_buffer) {
2354 /* all other inodes are free, so skip I/O */
2355 memset(bh->b_data, 0, bh->b_size);
2356 set_buffer_uptodate(bh);
2357 unlock_buffer(bh);
2358 goto has_buffer;
2359 }
2360 }
2361
2362 make_io:
2363 /*
2364 * There are other valid inodes in the buffer, this inode
2365 * has in-inode xattrs, or we don't have this inode in memory.
2366 * Read the block from disk.
2367 */
2368 get_bh(bh);
2369 bh->b_end_io = end_buffer_read_sync;
2370 submit_bh(READ, bh);
2371 wait_on_buffer(bh);
2372 if (!buffer_uptodate(bh)) {
2373 ext3_error(inode->i_sb, "ext3_get_inode_loc",
2374 "unable to read inode block - "
2375 "inode=%lu, block=%lu",
2376 inode->i_ino, block);
2377 brelse(bh);
2378 return -EIO;
2379 }
2380 }
2381 has_buffer:
2382 iloc->bh = bh;
2383 return 0;
2384 }
2385
2386 int ext3_get_inode_loc(struct inode *inode, struct ext3_iloc *iloc)
2387 {
2388 /* We have all inode data except xattrs in memory here. */
2389 return __ext3_get_inode_loc(inode, iloc,
2390 !(EXT3_I(inode)->i_state & EXT3_STATE_XATTR));
2391 }
2392
2393 void ext3_set_inode_flags(struct inode *inode)
2394 {
2395 unsigned int flags = EXT3_I(inode)->i_flags;
2396
2397 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
2398 if (flags & EXT3_SYNC_FL)
2399 inode->i_flags |= S_SYNC;
2400 if (flags & EXT3_APPEND_FL)
2401 inode->i_flags |= S_APPEND;
2402 if (flags & EXT3_IMMUTABLE_FL)
2403 inode->i_flags |= S_IMMUTABLE;
2404 if (flags & EXT3_NOATIME_FL)
2405 inode->i_flags |= S_NOATIME;
2406 if (flags & EXT3_DIRSYNC_FL)
2407 inode->i_flags |= S_DIRSYNC;
2408 }
2409
2410 void ext3_read_inode(struct inode * inode)
2411 {
2412 struct ext3_iloc iloc;
2413 struct ext3_inode *raw_inode;
2414 struct ext3_inode_info *ei = EXT3_I(inode);
2415 struct buffer_head *bh;
2416 int block;
2417
2418 #ifdef CONFIG_EXT3_FS_POSIX_ACL
2419 ei->i_acl = EXT3_ACL_NOT_CACHED;
2420 ei->i_default_acl = EXT3_ACL_NOT_CACHED;
2421 #endif
2422 ei->i_rsv_window.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
2423
2424 if (__ext3_get_inode_loc(inode, &iloc, 0))
2425 goto bad_inode;
2426 bh = iloc.bh;
2427 raw_inode = ext3_raw_inode(&iloc);
2428 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
2429 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
2430 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
2431 if(!(test_opt (inode->i_sb, NO_UID32))) {
2432 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
2433 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
2434 }
2435 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
2436 inode->i_size = le32_to_cpu(raw_inode->i_size);
2437 inode->i_atime.tv_sec = le32_to_cpu(raw_inode->i_atime);
2438 inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->i_ctime);
2439 inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->i_mtime);
2440 inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_mtime.tv_nsec = 0;
2441
2442 ei->i_state = 0;
2443 ei->i_next_alloc_block = 0;
2444 ei->i_next_alloc_goal = 0;
2445 ei->i_dir_start_lookup = 0;
2446 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
2447 /* We now have enough fields to check if the inode was active or not.
2448 * This is needed because nfsd might try to access dead inodes
2449 * the test is that same one that e2fsck uses
2450 * NeilBrown 1999oct15
2451 */
2452 if (inode->i_nlink == 0) {
2453 if (inode->i_mode == 0 ||
2454 !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) {
2455 /* this inode is deleted */
2456 brelse (bh);
2457 goto bad_inode;
2458 }
2459 /* The only unlinked inodes we let through here have
2460 * valid i_mode and are being read by the orphan
2461 * recovery code: that's fine, we're about to complete
2462 * the process of deleting those. */
2463 }
2464 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size
2465 * (for stat), not the fs block
2466 * size */
2467 inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
2468 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
2469 #ifdef EXT3_FRAGMENTS
2470 ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
2471 ei->i_frag_no = raw_inode->i_frag;
2472 ei->i_frag_size = raw_inode->i_fsize;
2473 #endif
2474 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
2475 if (!S_ISREG(inode->i_mode)) {
2476 ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
2477 } else {
2478 inode->i_size |=
2479 ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
2480 }
2481 ei->i_disksize = inode->i_size;
2482 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
2483 ei->i_block_group = iloc.block_group;
2484 ei->i_rsv_window.rsv_start = 0;
2485 ei->i_rsv_window.rsv_end= 0;
2486 atomic_set(&ei->i_rsv_window.rsv_goal_size, EXT3_DEFAULT_RESERVE_BLOCKS);
2487 seqlock_init(&ei->i_rsv_window.rsv_seqlock);
2488 /*
2489 * NOTE! The in-memory inode i_data array is in little-endian order
2490 * even on big-endian machines: we do NOT byteswap the block numbers!
2491 */
2492 for (block = 0; block < EXT3_N_BLOCKS; block++)
2493 ei->i_data[block] = raw_inode->i_block[block];
2494 INIT_LIST_HEAD(&ei->i_orphan);
2495
2496 if (inode->i_ino >= EXT3_FIRST_INO(inode->i_sb) + 1 &&
2497 EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) {
2498 /*
2499 * When mke2fs creates big inodes it does not zero out
2500 * the unused bytes above EXT3_GOOD_OLD_INODE_SIZE,
2501 * so ignore those first few inodes.
2502 */
2503 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
2504 if (EXT3_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
2505 EXT3_INODE_SIZE(inode->i_sb))
2506 goto bad_inode;
2507 if (ei->i_extra_isize == 0) {
2508 /* The extra space is currently unused. Use it. */
2509 ei->i_extra_isize = sizeof(struct ext3_inode) -
2510 EXT3_GOOD_OLD_INODE_SIZE;
2511 } else {
2512 __le32 *magic = (void *)raw_inode +
2513 EXT3_GOOD_OLD_INODE_SIZE +
2514 ei->i_extra_isize;
2515 if (*magic == cpu_to_le32(EXT3_XATTR_MAGIC))
2516 ei->i_state |= EXT3_STATE_XATTR;
2517 }
2518 } else
2519 ei->i_extra_isize = 0;
2520
2521 if (S_ISREG(inode->i_mode)) {
2522 inode->i_op = &ext3_file_inode_operations;
2523 inode->i_fop = &ext3_file_operations;
2524 ext3_set_aops(inode);
2525 } else if (S_ISDIR(inode->i_mode)) {
2526 inode->i_op = &ext3_dir_inode_operations;
2527 inode->i_fop = &ext3_dir_operations;
2528 } else if (S_ISLNK(inode->i_mode)) {
2529 if (ext3_inode_is_fast_symlink(inode))
2530 inode->i_op = &ext3_fast_symlink_inode_operations;
2531 else {
2532 inode->i_op = &ext3_symlink_inode_operations;
2533 ext3_set_aops(inode);
2534 }
2535 } else {
2536 inode->i_op = &ext3_special_inode_operations;
2537 if (raw_inode->i_block[0])
2538 init_special_inode(inode, inode->i_mode,
2539 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
2540 else
2541 init_special_inode(inode, inode->i_mode,
2542 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
2543 }
2544 brelse (iloc.bh);
2545 ext3_set_inode_flags(inode);
2546 return;
2547
2548 bad_inode:
2549 make_bad_inode(inode);
2550 return;
2551 }
2552
2553 /*
2554 * Post the struct inode info into an on-disk inode location in the
2555 * buffer-cache. This gobbles the caller's reference to the
2556 * buffer_head in the inode location struct.
2557 *
2558 * The caller must have write access to iloc->bh.
2559 */
2560 static int ext3_do_update_inode(handle_t *handle,
2561 struct inode *inode,
2562 struct ext3_iloc *iloc)
2563 {
2564 struct ext3_inode *raw_inode = ext3_raw_inode(iloc);
2565 struct ext3_inode_info *ei = EXT3_I(inode);
2566 struct buffer_head *bh = iloc->bh;
2567 int err = 0, rc, block;
2568
2569 /* For fields not not tracking in the in-memory inode,
2570 * initialise them to zero for new inodes. */
2571 if (ei->i_state & EXT3_STATE_NEW)
2572 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
2573
2574 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
2575 if(!(test_opt(inode->i_sb, NO_UID32))) {
2576 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
2577 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
2578 /*
2579 * Fix up interoperability with old kernels. Otherwise, old inodes get
2580 * re-used with the upper 16 bits of the uid/gid intact
2581 */
2582 if(!ei->i_dtime) {
2583 raw_inode->i_uid_high =
2584 cpu_to_le16(high_16_bits(inode->i_uid));
2585 raw_inode->i_gid_high =
2586 cpu_to_le16(high_16_bits(inode->i_gid));
2587 } else {
2588 raw_inode->i_uid_high = 0;
2589 raw_inode->i_gid_high = 0;
2590 }
2591 } else {
2592 raw_inode->i_uid_low =
2593 cpu_to_le16(fs_high2lowuid(inode->i_uid));
2594 raw_inode->i_gid_low =
2595 cpu_to_le16(fs_high2lowgid(inode->i_gid));
2596 raw_inode->i_uid_high = 0;
2597 raw_inode->i_gid_high = 0;
2598 }
2599 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
2600 raw_inode->i_size = cpu_to_le32(ei->i_disksize);
2601 raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
2602 raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
2603 raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
2604 raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
2605 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
2606 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
2607 #ifdef EXT3_FRAGMENTS
2608 raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
2609 raw_inode->i_frag = ei->i_frag_no;
2610 raw_inode->i_fsize = ei->i_frag_size;
2611 #endif
2612 raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
2613 if (!S_ISREG(inode->i_mode)) {
2614 raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
2615 } else {
2616 raw_inode->i_size_high =
2617 cpu_to_le32(ei->i_disksize >> 32);
2618 if (ei->i_disksize > 0x7fffffffULL) {
2619 struct super_block *sb = inode->i_sb;
2620 if (!EXT3_HAS_RO_COMPAT_FEATURE(sb,
2621 EXT3_FEATURE_RO_COMPAT_LARGE_FILE) ||
2622 EXT3_SB(sb)->s_es->s_rev_level ==
2623 cpu_to_le32(EXT3_GOOD_OLD_REV)) {
2624 /* If this is the first large file
2625 * created, add a flag to the superblock.
2626 */
2627 err = ext3_journal_get_write_access(handle,
2628 EXT3_SB(sb)->s_sbh);
2629 if (err)
2630 goto out_brelse;
2631 ext3_update_dynamic_rev(sb);
2632 EXT3_SET_RO_COMPAT_FEATURE(sb,
2633 EXT3_FEATURE_RO_COMPAT_LARGE_FILE);
2634 sb->s_dirt = 1;
2635 handle->h_sync = 1;
2636 err = ext3_journal_dirty_metadata(handle,
2637 EXT3_SB(sb)->s_sbh);
2638 }
2639 }
2640 }
2641 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
2642 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
2643 if (old_valid_dev(inode->i_rdev)) {
2644 raw_inode->i_block[0] =
2645 cpu_to_le32(old_encode_dev(inode->i_rdev));
2646 raw_inode->i_block[1] = 0;
2647 } else {
2648 raw_inode->i_block[0] = 0;
2649 raw_inode->i_block[1] =
2650 cpu_to_le32(new_encode_dev(inode->i_rdev));
2651 raw_inode->i_block[2] = 0;
2652 }
2653 } else for (block = 0; block < EXT3_N_BLOCKS; block++)
2654 raw_inode->i_block[block] = ei->i_data[block];
2655
2656 if (EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE)
2657 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
2658
2659 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
2660 rc = ext3_journal_dirty_metadata(handle, bh);
2661 if (!err)
2662 err = rc;
2663 ei->i_state &= ~EXT3_STATE_NEW;
2664
2665 out_brelse:
2666 brelse (bh);
2667 ext3_std_error(inode->i_sb, err);
2668 return err;
2669 }
2670
2671 /*
2672 * ext3_write_inode()
2673 *
2674 * We are called from a few places:
2675 *
2676 * - Within generic_file_write() for O_SYNC files.
2677 * Here, there will be no transaction running. We wait for any running
2678 * trasnaction to commit.
2679 *
2680 * - Within sys_sync(), kupdate and such.
2681 * We wait on commit, if tol to.
2682 *
2683 * - Within prune_icache() (PF_MEMALLOC == true)
2684 * Here we simply return. We can't afford to block kswapd on the
2685 * journal commit.
2686 *
2687 * In all cases it is actually safe for us to return without doing anything,
2688 * because the inode has been copied into a raw inode buffer in
2689 * ext3_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
2690 * knfsd.
2691 *
2692 * Note that we are absolutely dependent upon all inode dirtiers doing the
2693 * right thing: they *must* call mark_inode_dirty() after dirtying info in
2694 * which we are interested.
2695 *
2696 * It would be a bug for them to not do this. The code:
2697 *
2698 * mark_inode_dirty(inode)
2699 * stuff();
2700 * inode->i_size = expr;
2701 *
2702 * is in error because a kswapd-driven write_inode() could occur while
2703 * `stuff()' is running, and the new i_size will be lost. Plus the inode
2704 * will no longer be on the superblock's dirty inode list.
2705 */
2706 int ext3_write_inode(struct inode *inode, int wait)
2707 {
2708 if (current->flags & PF_MEMALLOC)
2709 return 0;
2710
2711 if (ext3_journal_current_handle()) {
2712 jbd_debug(0, "called recursively, non-PF_MEMALLOC!\n");
2713 dump_stack();
2714 return -EIO;
2715 }
2716
2717 if (!wait)
2718 return 0;
2719
2720 return ext3_force_commit(inode->i_sb);
2721 }
2722
2723 /*
2724 * ext3_setattr()
2725 *
2726 * Called from notify_change.
2727 *
2728 * We want to trap VFS attempts to truncate the file as soon as
2729 * possible. In particular, we want to make sure that when the VFS
2730 * shrinks i_size, we put the inode on the orphan list and modify
2731 * i_disksize immediately, so that during the subsequent flushing of
2732 * dirty pages and freeing of disk blocks, we can guarantee that any
2733 * commit will leave the blocks being flushed in an unused state on
2734 * disk. (On recovery, the inode will get truncated and the blocks will
2735 * be freed, so we have a strong guarantee that no future commit will
2736 * leave these blocks visible to the user.)
2737 *
2738 * Called with inode->sem down.
2739 */
2740 int ext3_setattr(struct dentry *dentry, struct iattr *attr)
2741 {
2742 struct inode *inode = dentry->d_inode;
2743 int error, rc = 0;
2744 const unsigned int ia_valid = attr->ia_valid;
2745
2746 error = inode_change_ok(inode, attr);
2747 if (error)
2748 return error;
2749
2750 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
2751 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
2752 handle_t *handle;
2753
2754 /* (user+group)*(old+new) structure, inode write (sb,
2755 * inode block, ? - but truncate inode update has it) */
2756 handle = ext3_journal_start(inode, 4*EXT3_QUOTA_INIT_BLOCKS+3);
2757 if (IS_ERR(handle)) {
2758 error = PTR_ERR(handle);
2759 goto err_out;
2760 }
2761 error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
2762 if (error) {
2763 ext3_journal_stop(handle);
2764 return error;
2765 }
2766 /* Update corresponding info in inode so that everything is in
2767 * one transaction */
2768 if (attr->ia_valid & ATTR_UID)
2769 inode->i_uid = attr->ia_uid;
2770 if (attr->ia_valid & ATTR_GID)
2771 inode->i_gid = attr->ia_gid;
2772 error = ext3_mark_inode_dirty(handle, inode);
2773 ext3_journal_stop(handle);
2774 }
2775
2776 if (S_ISREG(inode->i_mode) &&
2777 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
2778 handle_t *handle;
2779
2780 handle = ext3_journal_start(inode, 3);
2781 if (IS_ERR(handle)) {
2782 error = PTR_ERR(handle);
2783 goto err_out;
2784 }
2785
2786 error = ext3_orphan_add(handle, inode);
2787 EXT3_I(inode)->i_disksize = attr->ia_size;
2788 rc = ext3_mark_inode_dirty(handle, inode);
2789 if (!error)
2790 error = rc;
2791 ext3_journal_stop(handle);
2792 }
2793
2794 rc = inode_setattr(inode, attr);
2795
2796 /* If inode_setattr's call to ext3_truncate failed to get a
2797 * transaction handle at all, we need to clean up the in-core
2798 * orphan list manually. */
2799 if (inode->i_nlink)
2800 ext3_orphan_del(NULL, inode);
2801
2802 if (!rc && (ia_valid & ATTR_MODE))
2803 rc = ext3_acl_chmod(inode);
2804
2805 err_out:
2806 ext3_std_error(inode->i_sb, error);
2807 if (!error)
2808 error = rc;
2809 return error;
2810 }
2811
2812
2813 /*
2814 * akpm: how many blocks doth make a writepage()?
2815 *
2816 * With N blocks per page, it may be:
2817 * N data blocks
2818 * 2 indirect block
2819 * 2 dindirect
2820 * 1 tindirect
2821 * N+5 bitmap blocks (from the above)
2822 * N+5 group descriptor summary blocks
2823 * 1 inode block
2824 * 1 superblock.
2825 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quote files
2826 *
2827 * 3 * (N + 5) + 2 + 2 * EXT3_SINGLEDATA_TRANS_BLOCKS
2828 *
2829 * With ordered or writeback data it's the same, less the N data blocks.
2830 *
2831 * If the inode's direct blocks can hold an integral number of pages then a
2832 * page cannot straddle two indirect blocks, and we can only touch one indirect
2833 * and dindirect block, and the "5" above becomes "3".
2834 *
2835 * This still overestimates under most circumstances. If we were to pass the
2836 * start and end offsets in here as well we could do block_to_path() on each
2837 * block and work out the exact number of indirects which are touched. Pah.
2838 */
2839
2840 static int ext3_writepage_trans_blocks(struct inode *inode)
2841 {
2842 int bpp = ext3_journal_blocks_per_page(inode);
2843 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2844 int ret;
2845
2846 if (ext3_should_journal_data(inode))
2847 ret = 3 * (bpp + indirects) + 2;
2848 else
2849 ret = 2 * (bpp + indirects) + 2;
2850
2851 #ifdef CONFIG_QUOTA
2852 /* We know that structure was already allocated during DQUOT_INIT so
2853 * we will be updating only the data blocks + inodes */
2854 ret += 2*EXT3_QUOTA_TRANS_BLOCKS;
2855 #endif
2856
2857 return ret;
2858 }
2859
2860 /*
2861 * The caller must have previously called ext3_reserve_inode_write().
2862 * Give this, we know that the caller already has write access to iloc->bh.
2863 */
2864 int ext3_mark_iloc_dirty(handle_t *handle,
2865 struct inode *inode, struct ext3_iloc *iloc)
2866 {
2867 int err = 0;
2868
2869 /* the do_update_inode consumes one bh->b_count */
2870 get_bh(iloc->bh);
2871
2872 /* ext3_do_update_inode() does journal_dirty_metadata */
2873 err = ext3_do_update_inode(handle, inode, iloc);
2874 put_bh(iloc->bh);
2875 return err;
2876 }
2877
2878 /*
2879 * On success, We end up with an outstanding reference count against
2880 * iloc->bh. This _must_ be cleaned up later.
2881 */
2882
2883 int
2884 ext3_reserve_inode_write(handle_t *handle, struct inode *inode,
2885 struct ext3_iloc *iloc)
2886 {
2887 int err = 0;
2888 if (handle) {
2889 err = ext3_get_inode_loc(inode, iloc);
2890 if (!err) {
2891 BUFFER_TRACE(iloc->bh, "get_write_access");
2892 err = ext3_journal_get_write_access(handle, iloc->bh);
2893 if (err) {
2894 brelse(iloc->bh);
2895 iloc->bh = NULL;
2896 }
2897 }
2898 }
2899 ext3_std_error(inode->i_sb, err);
2900 return err;
2901 }
2902
2903 /*
2904 * akpm: What we do here is to mark the in-core inode as clean
2905 * with respect to inode dirtiness (it may still be data-dirty).
2906 * This means that the in-core inode may be reaped by prune_icache
2907 * without having to perform any I/O. This is a very good thing,
2908 * because *any* task may call prune_icache - even ones which
2909 * have a transaction open against a different journal.
2910 *
2911 * Is this cheating? Not really. Sure, we haven't written the
2912 * inode out, but prune_icache isn't a user-visible syncing function.
2913 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
2914 * we start and wait on commits.
2915 *
2916 * Is this efficient/effective? Well, we're being nice to the system
2917 * by cleaning up our inodes proactively so they can be reaped
2918 * without I/O. But we are potentially leaving up to five seconds'
2919 * worth of inodes floating about which prune_icache wants us to
2920 * write out. One way to fix that would be to get prune_icache()
2921 * to do a write_super() to free up some memory. It has the desired
2922 * effect.
2923 */
2924 int ext3_mark_inode_dirty(handle_t *handle, struct inode *inode)
2925 {
2926 struct ext3_iloc iloc;
2927 int err;
2928
2929 might_sleep();
2930 err = ext3_reserve_inode_write(handle, inode, &iloc);
2931 if (!err)
2932 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2933 return err;
2934 }
2935
2936 /*
2937 * akpm: ext3_dirty_inode() is called from __mark_inode_dirty()
2938 *
2939 * We're really interested in the case where a file is being extended.
2940 * i_size has been changed by generic_commit_write() and we thus need
2941 * to include the updated inode in the current transaction.
2942 *
2943 * Also, DQUOT_ALLOC_SPACE() will always dirty the inode when blocks
2944 * are allocated to the file.
2945 *
2946 * If the inode is marked synchronous, we don't honour that here - doing
2947 * so would cause a commit on atime updates, which we don't bother doing.
2948 * We handle synchronous inodes at the highest possible level.
2949 */
2950 void ext3_dirty_inode(struct inode *inode)
2951 {
2952 handle_t *current_handle = ext3_journal_current_handle();
2953 handle_t *handle;
2954
2955 handle = ext3_journal_start(inode, 2);
2956 if (IS_ERR(handle))
2957 goto out;
2958 if (current_handle &&
2959 current_handle->h_transaction != handle->h_transaction) {
2960 /* This task has a transaction open against a different fs */
2961 printk(KERN_EMERG "%s: transactions do not match!\n",
2962 __FUNCTION__);
2963 } else {
2964 jbd_debug(5, "marking dirty. outer handle=%p\n",
2965 current_handle);
2966 ext3_mark_inode_dirty(handle, inode);
2967 }
2968 ext3_journal_stop(handle);
2969 out:
2970 return;
2971 }
2972
2973 #ifdef AKPM
2974 /*
2975 * Bind an inode's backing buffer_head into this transaction, to prevent
2976 * it from being flushed to disk early. Unlike
2977 * ext3_reserve_inode_write, this leaves behind no bh reference and
2978 * returns no iloc structure, so the caller needs to repeat the iloc
2979 * lookup to mark the inode dirty later.
2980 */
2981 static inline int
2982 ext3_pin_inode(handle_t *handle, struct inode *inode)
2983 {
2984 struct ext3_iloc iloc;
2985
2986 int err = 0;
2987 if (handle) {
2988 err = ext3_get_inode_loc(inode, &iloc);
2989 if (!err) {
2990 BUFFER_TRACE(iloc.bh, "get_write_access");
2991 err = journal_get_write_access(handle, iloc.bh);
2992 if (!err)
2993 err = ext3_journal_dirty_metadata(handle,
2994 iloc.bh);
2995 brelse(iloc.bh);
2996 }
2997 }
2998 ext3_std_error(inode->i_sb, err);
2999 return err;
3000 }
3001 #endif
3002
3003 int ext3_change_inode_journal_flag(struct inode *inode, int val)
3004 {
3005 journal_t *journal;
3006 handle_t *handle;
3007 int err;
3008
3009 /*
3010 * We have to be very careful here: changing a data block's
3011 * journaling status dynamically is dangerous. If we write a
3012 * data block to the journal, change the status and then delete
3013 * that block, we risk forgetting to revoke the old log record
3014 * from the journal and so a subsequent replay can corrupt data.
3015 * So, first we make sure that the journal is empty and that
3016 * nobody is changing anything.
3017 */
3018
3019 journal = EXT3_JOURNAL(inode);
3020 if (is_journal_aborted(journal) || IS_RDONLY(inode))
3021 return -EROFS;
3022
3023 journal_lock_updates(journal);
3024 journal_flush(journal);
3025
3026 /*
3027 * OK, there are no updates running now, and all cached data is
3028 * synced to disk. We are now in a completely consistent state
3029 * which doesn't have anything in the journal, and we know that
3030 * no filesystem updates are running, so it is safe to modify
3031 * the inode's in-core data-journaling state flag now.
3032 */
3033
3034 if (val)
3035 EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
3036 else
3037 EXT3_I(inode)->i_flags &= ~EXT3_JOURNAL_DATA_FL;
3038 ext3_set_aops(inode);
3039
3040 journal_unlock_updates(journal);
3041
3042 /* Finally we can mark the inode as dirty. */
3043
3044 handle = ext3_journal_start(inode, 1);
3045 if (IS_ERR(handle))
3046 return PTR_ERR(handle);
3047
3048 err = ext3_mark_inode_dirty(handle, inode);
3049 handle->h_sync = 1;
3050 ext3_journal_stop(handle);
3051 ext3_std_error(inode->i_sb, err);
3052
3053 return err;
3054 }
3055
|
This page was automatically generated by the
LXR engine.
|