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