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