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