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