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