[GFS2] Get rid of unneeded parameter in gfs2_rlist_alloc
[pandora-kernel.git] / fs / gfs2 / bmap.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License version 2.
8  */
9
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/gfs2_ondisk.h>
15 #include <linux/crc32.h>
16 #include <linux/lm_interface.h>
17
18 #include "gfs2.h"
19 #include "incore.h"
20 #include "bmap.h"
21 #include "glock.h"
22 #include "inode.h"
23 #include "meta_io.h"
24 #include "quota.h"
25 #include "rgrp.h"
26 #include "trans.h"
27 #include "dir.h"
28 #include "util.h"
29 #include "ops_address.h"
30
31 /* This doesn't need to be that large as max 64 bit pointers in a 4k
32  * block is 512, so __u16 is fine for that. It saves stack space to
33  * keep it small.
34  */
35 struct metapath {
36         __u16 mp_list[GFS2_MAX_META_HEIGHT];
37 };
38
39 typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
40                              struct buffer_head *bh, __be64 *top,
41                              __be64 *bottom, unsigned int height,
42                              void *data);
43
44 struct strip_mine {
45         int sm_first;
46         unsigned int sm_height;
47 };
48
49 /**
50  * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
51  * @ip: the inode
52  * @dibh: the dinode buffer
53  * @block: the block number that was allocated
54  * @private: any locked page held by the caller process
55  *
56  * Returns: errno
57  */
58
59 static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
60                                u64 block, struct page *page)
61 {
62         struct inode *inode = &ip->i_inode;
63         struct buffer_head *bh;
64         int release = 0;
65
66         if (!page || page->index) {
67                 page = grab_cache_page(inode->i_mapping, 0);
68                 if (!page)
69                         return -ENOMEM;
70                 release = 1;
71         }
72
73         if (!PageUptodate(page)) {
74                 void *kaddr = kmap(page);
75
76                 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
77                        ip->i_di.di_size);
78                 memset(kaddr + ip->i_di.di_size, 0,
79                        PAGE_CACHE_SIZE - ip->i_di.di_size);
80                 kunmap(page);
81
82                 SetPageUptodate(page);
83         }
84
85         if (!page_has_buffers(page))
86                 create_empty_buffers(page, 1 << inode->i_blkbits,
87                                      (1 << BH_Uptodate));
88
89         bh = page_buffers(page);
90
91         if (!buffer_mapped(bh))
92                 map_bh(bh, inode->i_sb, block);
93
94         set_buffer_uptodate(bh);
95         if (!gfs2_is_jdata(ip))
96                 mark_buffer_dirty(bh);
97         if (!gfs2_is_writeback(ip))
98                 gfs2_trans_add_bh(ip->i_gl, bh, 0);
99
100         if (release) {
101                 unlock_page(page);
102                 page_cache_release(page);
103         }
104
105         return 0;
106 }
107
108 /**
109  * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
110  * @ip: The GFS2 inode to unstuff
111  * @unstuffer: the routine that handles unstuffing a non-zero length file
112  * @private: private data for the unstuffer
113  *
114  * This routine unstuffs a dinode and returns it to a "normal" state such
115  * that the height can be grown in the traditional way.
116  *
117  * Returns: errno
118  */
119
120 int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
121 {
122         struct buffer_head *bh, *dibh;
123         struct gfs2_dinode *di;
124         u64 block = 0;
125         int isdir = gfs2_is_dir(ip);
126         int error;
127
128         down_write(&ip->i_rw_mutex);
129
130         error = gfs2_meta_inode_buffer(ip, &dibh);
131         if (error)
132                 goto out;
133
134         if (ip->i_di.di_size) {
135                 /* Get a free block, fill it with the stuffed data,
136                    and write it out to disk */
137
138                 if (isdir) {
139                         block = gfs2_alloc_meta(ip);
140
141                         error = gfs2_dir_get_new_buffer(ip, block, &bh);
142                         if (error)
143                                 goto out_brelse;
144                         gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
145                                               dibh, sizeof(struct gfs2_dinode));
146                         brelse(bh);
147                 } else {
148                         block = gfs2_alloc_data(ip);
149
150                         error = gfs2_unstuffer_page(ip, dibh, block, page);
151                         if (error)
152                                 goto out_brelse;
153                 }
154         }
155
156         /*  Set up the pointer to the new block  */
157
158         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
159         di = (struct gfs2_dinode *)dibh->b_data;
160         gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
161
162         if (ip->i_di.di_size) {
163                 *(__be64 *)(di + 1) = cpu_to_be64(block);
164                 ip->i_di.di_blocks++;
165                 gfs2_set_inode_blocks(&ip->i_inode);
166                 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
167         }
168
169         ip->i_height = 1;
170         di->di_height = cpu_to_be16(1);
171
172 out_brelse:
173         brelse(dibh);
174 out:
175         up_write(&ip->i_rw_mutex);
176         return error;
177 }
178
179 /**
180  * build_height - Build a metadata tree of the requested height
181  * @ip: The GFS2 inode
182  * @height: The height to build to
183  *
184  *
185  * Returns: errno
186  */
187
188 static int build_height(struct inode *inode, unsigned height)
189 {
190         struct gfs2_inode *ip = GFS2_I(inode);
191         unsigned new_height = height - ip->i_height;
192         struct buffer_head *dibh;
193         struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
194         struct gfs2_dinode *di;
195         int error;
196         __be64 *bp;
197         u64 bn;
198         unsigned n;
199
200         if (height <= ip->i_height)
201                 return 0;
202
203         error = gfs2_meta_inode_buffer(ip, &dibh);
204         if (error)
205                 return error;
206
207         for(n = 0; n < new_height; n++) {
208                 bn = gfs2_alloc_meta(ip);
209                 blocks[n] = gfs2_meta_new(ip->i_gl, bn);
210                 gfs2_trans_add_bh(ip->i_gl, blocks[n], 1);
211         }
212
213         n = 0;
214         bn = blocks[0]->b_blocknr;
215         if (new_height > 1) {
216                 for(; n < new_height-1; n++) {
217                         gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
218                                           GFS2_FORMAT_IN);
219                         gfs2_buffer_clear_tail(blocks[n],
220                                                sizeof(struct gfs2_meta_header));
221                         bp = (__be64 *)(blocks[n]->b_data +
222                                      sizeof(struct gfs2_meta_header));
223                         *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
224                         brelse(blocks[n]);
225                         blocks[n] = NULL;
226                 }
227         }
228         gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
229         gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
230                               dibh, sizeof(struct gfs2_dinode));
231         brelse(blocks[n]);
232         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
233         di = (struct gfs2_dinode *)dibh->b_data;
234         gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
235         *(__be64 *)(di + 1) = cpu_to_be64(bn);
236         ip->i_height += new_height;
237         ip->i_di.di_blocks += new_height;
238         gfs2_set_inode_blocks(&ip->i_inode);
239         di->di_height = cpu_to_be16(ip->i_height);
240         di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
241         brelse(dibh);
242         return error;
243 }
244
245 /**
246  * find_metapath - Find path through the metadata tree
247  * @ip: The inode pointer
248  * @mp: The metapath to return the result in
249  * @block: The disk block to look up
250  *
251  *   This routine returns a struct metapath structure that defines a path
252  *   through the metadata of inode "ip" to get to block "block".
253  *
254  *   Example:
255  *   Given:  "ip" is a height 3 file, "offset" is 101342453, and this is a
256  *   filesystem with a blocksize of 4096.
257  *
258  *   find_metapath() would return a struct metapath structure set to:
259  *   mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
260  *   and mp_list[2] = 165.
261  *
262  *   That means that in order to get to the block containing the byte at
263  *   offset 101342453, we would load the indirect block pointed to by pointer
264  *   0 in the dinode.  We would then load the indirect block pointed to by
265  *   pointer 48 in that indirect block.  We would then load the data block
266  *   pointed to by pointer 165 in that indirect block.
267  *
268  *             ----------------------------------------
269  *             | Dinode |                             |
270  *             |        |                            4|
271  *             |        |0 1 2 3 4 5                 9|
272  *             |        |                            6|
273  *             ----------------------------------------
274  *                       |
275  *                       |
276  *                       V
277  *             ----------------------------------------
278  *             | Indirect Block                       |
279  *             |                                     5|
280  *             |            4 4 4 4 4 5 5            1|
281  *             |0           5 6 7 8 9 0 1            2|
282  *             ----------------------------------------
283  *                                |
284  *                                |
285  *                                V
286  *             ----------------------------------------
287  *             | Indirect Block                       |
288  *             |                         1 1 1 1 1   5|
289  *             |                         6 6 6 6 6   1|
290  *             |0                        3 4 5 6 7   2|
291  *             ----------------------------------------
292  *                                           |
293  *                                           |
294  *                                           V
295  *             ----------------------------------------
296  *             | Data block containing offset         |
297  *             |            101342453                 |
298  *             |                                      |
299  *             |                                      |
300  *             ----------------------------------------
301  *
302  */
303
304 static void find_metapath(struct gfs2_inode *ip, u64 block,
305                           struct metapath *mp)
306 {
307         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
308         u64 b = block;
309         unsigned int i;
310
311         for (i = ip->i_height; i--;)
312                 mp->mp_list[i] = do_div(b, sdp->sd_inptrs);
313
314 }
315
316 /**
317  * metapointer - Return pointer to start of metadata in a buffer
318  * @bh: The buffer
319  * @height: The metadata height (0 = dinode)
320  * @mp: The metapath
321  *
322  * Return a pointer to the block number of the next height of the metadata
323  * tree given a buffer containing the pointer to the current height of the
324  * metadata tree.
325  */
326
327 static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
328                                unsigned int height, const struct metapath *mp)
329 {
330         unsigned int head_size = (height > 0) ?
331                 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
332         __be64 *ptr;
333         *boundary = 0;
334         ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
335         if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
336                 *boundary = 1;
337         return ptr;
338 }
339
340 /**
341  * lookup_block - Get the next metadata block in metadata tree
342  * @ip: The GFS2 inode
343  * @bh: Buffer containing the pointers to metadata blocks
344  * @height: The height of the tree (0 = dinode)
345  * @mp: The metapath
346  * @create: Non-zero if we may create a new meatdata block
347  * @new: Used to indicate if we did create a new metadata block
348  * @block: the returned disk block number
349  *
350  * Given a metatree, complete to a particular height, checks to see if the next
351  * height of the tree exists. If not the next height of the tree is created.
352  * The block number of the next height of the metadata tree is returned.
353  *
354  */
355
356 static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
357                         unsigned int height, struct metapath *mp, int create,
358                         int *new, u64 *block)
359 {
360         int boundary;
361         __be64 *ptr = metapointer(bh, &boundary, height, mp);
362
363         if (*ptr) {
364                 *block = be64_to_cpu(*ptr);
365                 return boundary;
366         }
367
368         *block = 0;
369
370         if (!create)
371                 return 0;
372
373         if (height == ip->i_height - 1 && !gfs2_is_dir(ip))
374                 *block = gfs2_alloc_data(ip);
375         else
376                 *block = gfs2_alloc_meta(ip);
377
378         gfs2_trans_add_bh(ip->i_gl, bh, 1);
379
380         *ptr = cpu_to_be64(*block);
381         ip->i_di.di_blocks++;
382         gfs2_set_inode_blocks(&ip->i_inode);
383
384         *new = 1;
385         return 0;
386 }
387
388 static inline void bmap_lock(struct inode *inode, int create)
389 {
390         struct gfs2_inode *ip = GFS2_I(inode);
391         if (create)
392                 down_write(&ip->i_rw_mutex);
393         else
394                 down_read(&ip->i_rw_mutex);
395 }
396
397 static inline void bmap_unlock(struct inode *inode, int create)
398 {
399         struct gfs2_inode *ip = GFS2_I(inode);
400         if (create)
401                 up_write(&ip->i_rw_mutex);
402         else
403                 up_read(&ip->i_rw_mutex);
404 }
405
406 /**
407  * gfs2_block_map - Map a block from an inode to a disk block
408  * @inode: The inode
409  * @lblock: The logical block number
410  * @bh_map: The bh to be mapped
411  *
412  * Find the block number on the current device which corresponds to an
413  * inode's block. If the block had to be created, "new" will be set.
414  *
415  * Returns: errno
416  */
417
418 int gfs2_block_map(struct inode *inode, sector_t lblock,
419                    struct buffer_head *bh_map, int create)
420 {
421         struct gfs2_inode *ip = GFS2_I(inode);
422         struct gfs2_sbd *sdp = GFS2_SB(inode);
423         struct buffer_head *bh;
424         unsigned int bsize = sdp->sd_sb.sb_bsize;
425         unsigned int end_of_metadata;
426         unsigned int x;
427         int error = 0;
428         int new = 0;
429         u64 dblock = 0;
430         int boundary;
431         unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
432         struct metapath mp;
433         u64 size;
434         struct buffer_head *dibh = NULL;
435         const u64 *arr = sdp->sd_heightsize;
436         BUG_ON(maxlen == 0);
437
438         if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
439                 return 0;
440
441         bmap_lock(inode, create);
442         clear_buffer_mapped(bh_map);
443         clear_buffer_new(bh_map);
444         clear_buffer_boundary(bh_map);
445         if (gfs2_is_dir(ip)) {
446                 bsize = sdp->sd_jbsize;
447                 arr = sdp->sd_jheightsize;
448         }
449         size = (lblock + 1) * bsize;
450
451         if (size > arr[ip->i_height]) {
452                 u8 height = ip->i_height;
453                 if (!create)
454                         goto out_ok;
455                 while (size > arr[height])
456                         height++;
457                 error = build_height(inode, height);
458                 if (error)
459                         goto out_fail;
460         }
461
462         find_metapath(ip, lblock, &mp);
463         end_of_metadata = ip->i_height - 1;
464         error = gfs2_meta_inode_buffer(ip, &bh);
465         if (error)
466                 goto out_fail;
467         dibh = bh;
468         get_bh(dibh);
469
470         for (x = 0; x < end_of_metadata; x++) {
471                 lookup_block(ip, bh, x, &mp, create, &new, &dblock);
472                 brelse(bh);
473                 if (!dblock)
474                         goto out_ok;
475
476                 error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
477                 if (error)
478                         goto out_fail;
479         }
480
481         boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock);
482         if (dblock) {
483                 map_bh(bh_map, inode->i_sb, dblock);
484                 if (boundary)
485                         set_buffer_boundary(bh_map);
486                 if (new) {
487                         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
488                         gfs2_dinode_out(ip, dibh->b_data);
489                         set_buffer_new(bh_map);
490                         goto out_brelse;
491                 }
492                 while(--maxlen && !buffer_boundary(bh_map)) {
493                         u64 eblock;
494
495                         mp.mp_list[end_of_metadata]++;
496                         boundary = lookup_block(ip, bh, end_of_metadata, &mp, 0, &new, &eblock);
497                         if (eblock != ++dblock)
498                                 break;
499                         bh_map->b_size += (1 << inode->i_blkbits);
500                         if (boundary)
501                                 set_buffer_boundary(bh_map);
502                 }
503         }
504 out_brelse:
505         brelse(bh);
506 out_ok:
507         error = 0;
508 out_fail:
509         if (dibh)
510                 brelse(dibh);
511         bmap_unlock(inode, create);
512         return error;
513 }
514
515 /*
516  * Deprecated: do not use in new code
517  */
518 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
519 {
520         struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
521         int ret;
522         int create = *new;
523
524         BUG_ON(!extlen);
525         BUG_ON(!dblock);
526         BUG_ON(!new);
527
528         bh.b_size = 1 << (inode->i_blkbits + 5);
529         ret = gfs2_block_map(inode, lblock, &bh, create);
530         *extlen = bh.b_size >> inode->i_blkbits;
531         *dblock = bh.b_blocknr;
532         if (buffer_new(&bh))
533                 *new = 1;
534         else
535                 *new = 0;
536         return ret;
537 }
538
539 /**
540  * recursive_scan - recursively scan through the end of a file
541  * @ip: the inode
542  * @dibh: the dinode buffer
543  * @mp: the path through the metadata to the point to start
544  * @height: the height the recursion is at
545  * @block: the indirect block to look at
546  * @first: 1 if this is the first block
547  * @bc: the call to make for each piece of metadata
548  * @data: data opaque to this function to pass to @bc
549  *
550  * When this is first called @height and @block should be zero and
551  * @first should be 1.
552  *
553  * Returns: errno
554  */
555
556 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
557                           struct metapath *mp, unsigned int height,
558                           u64 block, int first, block_call_t bc,
559                           void *data)
560 {
561         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
562         struct buffer_head *bh = NULL;
563         __be64 *top, *bottom;
564         u64 bn;
565         int error;
566         int mh_size = sizeof(struct gfs2_meta_header);
567
568         if (!height) {
569                 error = gfs2_meta_inode_buffer(ip, &bh);
570                 if (error)
571                         return error;
572                 dibh = bh;
573
574                 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
575                 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
576         } else {
577                 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
578                 if (error)
579                         return error;
580
581                 top = (__be64 *)(bh->b_data + mh_size) +
582                                   (first ? mp->mp_list[height] : 0);
583
584                 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
585         }
586
587         error = bc(ip, dibh, bh, top, bottom, height, data);
588         if (error)
589                 goto out;
590
591         if (height < ip->i_height - 1)
592                 for (; top < bottom; top++, first = 0) {
593                         if (!*top)
594                                 continue;
595
596                         bn = be64_to_cpu(*top);
597
598                         error = recursive_scan(ip, dibh, mp, height + 1, bn,
599                                                first, bc, data);
600                         if (error)
601                                 break;
602                 }
603
604 out:
605         brelse(bh);
606         return error;
607 }
608
609 /**
610  * do_strip - Look for a layer a particular layer of the file and strip it off
611  * @ip: the inode
612  * @dibh: the dinode buffer
613  * @bh: A buffer of pointers
614  * @top: The first pointer in the buffer
615  * @bottom: One more than the last pointer
616  * @height: the height this buffer is at
617  * @data: a pointer to a struct strip_mine
618  *
619  * Returns: errno
620  */
621
622 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
623                     struct buffer_head *bh, __be64 *top, __be64 *bottom,
624                     unsigned int height, void *data)
625 {
626         struct strip_mine *sm = data;
627         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
628         struct gfs2_rgrp_list rlist;
629         u64 bn, bstart;
630         u32 blen;
631         __be64 *p;
632         unsigned int rg_blocks = 0;
633         int metadata;
634         unsigned int revokes = 0;
635         int x;
636         int error;
637
638         if (!*top)
639                 sm->sm_first = 0;
640
641         if (height != sm->sm_height)
642                 return 0;
643
644         if (sm->sm_first) {
645                 top++;
646                 sm->sm_first = 0;
647         }
648
649         metadata = (height != ip->i_height - 1);
650         if (metadata)
651                 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
652
653         error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
654         if (error)
655                 return error;
656
657         memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
658         bstart = 0;
659         blen = 0;
660
661         for (p = top; p < bottom; p++) {
662                 if (!*p)
663                         continue;
664
665                 bn = be64_to_cpu(*p);
666
667                 if (bstart + blen == bn)
668                         blen++;
669                 else {
670                         if (bstart)
671                                 gfs2_rlist_add(sdp, &rlist, bstart);
672
673                         bstart = bn;
674                         blen = 1;
675                 }
676         }
677
678         if (bstart)
679                 gfs2_rlist_add(sdp, &rlist, bstart);
680         else
681                 goto out; /* Nothing to do */
682
683         gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
684
685         for (x = 0; x < rlist.rl_rgrps; x++) {
686                 struct gfs2_rgrpd *rgd;
687                 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
688                 rg_blocks += rgd->rd_length;
689         }
690
691         error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
692         if (error)
693                 goto out_rlist;
694
695         error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
696                                  RES_INDIRECT + RES_STATFS + RES_QUOTA,
697                                  revokes);
698         if (error)
699                 goto out_rg_gunlock;
700
701         down_write(&ip->i_rw_mutex);
702
703         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
704         gfs2_trans_add_bh(ip->i_gl, bh, 1);
705
706         bstart = 0;
707         blen = 0;
708
709         for (p = top; p < bottom; p++) {
710                 if (!*p)
711                         continue;
712
713                 bn = be64_to_cpu(*p);
714
715                 if (bstart + blen == bn)
716                         blen++;
717                 else {
718                         if (bstart) {
719                                 if (metadata)
720                                         gfs2_free_meta(ip, bstart, blen);
721                                 else
722                                         gfs2_free_data(ip, bstart, blen);
723                         }
724
725                         bstart = bn;
726                         blen = 1;
727                 }
728
729                 *p = 0;
730                 if (!ip->i_di.di_blocks)
731                         gfs2_consist_inode(ip);
732                 ip->i_di.di_blocks--;
733                 gfs2_set_inode_blocks(&ip->i_inode);
734         }
735         if (bstart) {
736                 if (metadata)
737                         gfs2_free_meta(ip, bstart, blen);
738                 else
739                         gfs2_free_data(ip, bstart, blen);
740         }
741
742         ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
743
744         gfs2_dinode_out(ip, dibh->b_data);
745
746         up_write(&ip->i_rw_mutex);
747
748         gfs2_trans_end(sdp);
749
750 out_rg_gunlock:
751         gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
752 out_rlist:
753         gfs2_rlist_free(&rlist);
754 out:
755         gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
756         return error;
757 }
758
759 /**
760  * do_grow - Make a file look bigger than it is
761  * @ip: the inode
762  * @size: the size to set the file to
763  *
764  * Called with an exclusive lock on @ip.
765  *
766  * Returns: errno
767  */
768
769 static int do_grow(struct gfs2_inode *ip, u64 size)
770 {
771         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
772         struct gfs2_alloc *al;
773         struct buffer_head *dibh;
774         int error;
775
776         al = gfs2_alloc_get(ip);
777
778         error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
779         if (error)
780                 goto out;
781
782         error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
783         if (error)
784                 goto out_gunlock_q;
785
786         al->al_requested = sdp->sd_max_height + RES_DATA;
787
788         error = gfs2_inplace_reserve(ip);
789         if (error)
790                 goto out_gunlock_q;
791
792         error = gfs2_trans_begin(sdp,
793                         sdp->sd_max_height + al->al_rgd->rd_length +
794                         RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
795         if (error)
796                 goto out_ipres;
797
798         if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
799                 const u64 *arr = sdp->sd_heightsize;
800                 if (gfs2_is_stuffed(ip)) {
801                         error = gfs2_unstuff_dinode(ip, NULL);
802                         if (error)
803                                 goto out_end_trans;
804                 }
805
806                 down_write(&ip->i_rw_mutex);
807                 if (size > arr[ip->i_height]) {
808                         u8 height = ip->i_height;
809                         while(size > arr[height])
810                                 height++;
811                         error = build_height(&ip->i_inode, height);
812                 }
813                 up_write(&ip->i_rw_mutex);
814                 if (error)
815                         goto out_end_trans;
816         }
817
818         ip->i_di.di_size = size;
819         ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
820
821         error = gfs2_meta_inode_buffer(ip, &dibh);
822         if (error)
823                 goto out_end_trans;
824
825         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
826         gfs2_dinode_out(ip, dibh->b_data);
827         brelse(dibh);
828
829 out_end_trans:
830         gfs2_trans_end(sdp);
831 out_ipres:
832         gfs2_inplace_release(ip);
833 out_gunlock_q:
834         gfs2_quota_unlock(ip);
835 out:
836         gfs2_alloc_put(ip);
837         return error;
838 }
839
840
841 /**
842  * gfs2_block_truncate_page - Deal with zeroing out data for truncate
843  *
844  * This is partly borrowed from ext3.
845  */
846 static int gfs2_block_truncate_page(struct address_space *mapping)
847 {
848         struct inode *inode = mapping->host;
849         struct gfs2_inode *ip = GFS2_I(inode);
850         loff_t from = inode->i_size;
851         unsigned long index = from >> PAGE_CACHE_SHIFT;
852         unsigned offset = from & (PAGE_CACHE_SIZE-1);
853         unsigned blocksize, iblock, length, pos;
854         struct buffer_head *bh;
855         struct page *page;
856         int err;
857
858         page = grab_cache_page(mapping, index);
859         if (!page)
860                 return 0;
861
862         blocksize = inode->i_sb->s_blocksize;
863         length = blocksize - (offset & (blocksize - 1));
864         iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
865
866         if (!page_has_buffers(page))
867                 create_empty_buffers(page, blocksize, 0);
868
869         /* Find the buffer that contains "offset" */
870         bh = page_buffers(page);
871         pos = blocksize;
872         while (offset >= pos) {
873                 bh = bh->b_this_page;
874                 iblock++;
875                 pos += blocksize;
876         }
877
878         err = 0;
879
880         if (!buffer_mapped(bh)) {
881                 gfs2_block_map(inode, iblock, bh, 0);
882                 /* unmapped? It's a hole - nothing to do */
883                 if (!buffer_mapped(bh))
884                         goto unlock;
885         }
886
887         /* Ok, it's mapped. Make sure it's up-to-date */
888         if (PageUptodate(page))
889                 set_buffer_uptodate(bh);
890
891         if (!buffer_uptodate(bh)) {
892                 err = -EIO;
893                 ll_rw_block(READ, 1, &bh);
894                 wait_on_buffer(bh);
895                 /* Uhhuh. Read error. Complain and punt. */
896                 if (!buffer_uptodate(bh))
897                         goto unlock;
898                 err = 0;
899         }
900
901         if (!gfs2_is_writeback(ip))
902                 gfs2_trans_add_bh(ip->i_gl, bh, 0);
903
904         zero_user(page, offset, length);
905
906 unlock:
907         unlock_page(page);
908         page_cache_release(page);
909         return err;
910 }
911
912 static int trunc_start(struct gfs2_inode *ip, u64 size)
913 {
914         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
915         struct buffer_head *dibh;
916         int journaled = gfs2_is_jdata(ip);
917         int error;
918
919         error = gfs2_trans_begin(sdp,
920                                  RES_DINODE + (journaled ? RES_JDATA : 0), 0);
921         if (error)
922                 return error;
923
924         error = gfs2_meta_inode_buffer(ip, &dibh);
925         if (error)
926                 goto out;
927
928         if (gfs2_is_stuffed(ip)) {
929                 ip->i_di.di_size = size;
930                 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
931                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
932                 gfs2_dinode_out(ip, dibh->b_data);
933                 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
934                 error = 1;
935
936         } else {
937                 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
938                         error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
939
940                 if (!error) {
941                         ip->i_di.di_size = size;
942                         ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
943                         ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
944                         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
945                         gfs2_dinode_out(ip, dibh->b_data);
946                 }
947         }
948
949         brelse(dibh);
950
951 out:
952         gfs2_trans_end(sdp);
953         return error;
954 }
955
956 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
957 {
958         unsigned int height = ip->i_height;
959         u64 lblock;
960         struct metapath mp;
961         int error;
962
963         if (!size)
964                 lblock = 0;
965         else
966                 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
967
968         find_metapath(ip, lblock, &mp);
969         gfs2_alloc_get(ip);
970
971         error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
972         if (error)
973                 goto out;
974
975         while (height--) {
976                 struct strip_mine sm;
977                 sm.sm_first = !!size;
978                 sm.sm_height = height;
979
980                 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
981                 if (error)
982                         break;
983         }
984
985         gfs2_quota_unhold(ip);
986
987 out:
988         gfs2_alloc_put(ip);
989         return error;
990 }
991
992 static int trunc_end(struct gfs2_inode *ip)
993 {
994         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
995         struct buffer_head *dibh;
996         int error;
997
998         error = gfs2_trans_begin(sdp, RES_DINODE, 0);
999         if (error)
1000                 return error;
1001
1002         down_write(&ip->i_rw_mutex);
1003
1004         error = gfs2_meta_inode_buffer(ip, &dibh);
1005         if (error)
1006                 goto out;
1007
1008         if (!ip->i_di.di_size) {
1009                 ip->i_height = 0;
1010                 ip->i_di.di_goal_meta =
1011                         ip->i_di.di_goal_data =
1012                         ip->i_no_addr;
1013                 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1014         }
1015         ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1016         ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1017
1018         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1019         gfs2_dinode_out(ip, dibh->b_data);
1020         brelse(dibh);
1021
1022 out:
1023         up_write(&ip->i_rw_mutex);
1024         gfs2_trans_end(sdp);
1025         return error;
1026 }
1027
1028 /**
1029  * do_shrink - make a file smaller
1030  * @ip: the inode
1031  * @size: the size to make the file
1032  * @truncator: function to truncate the last partial block
1033  *
1034  * Called with an exclusive lock on @ip.
1035  *
1036  * Returns: errno
1037  */
1038
1039 static int do_shrink(struct gfs2_inode *ip, u64 size)
1040 {
1041         int error;
1042
1043         error = trunc_start(ip, size);
1044         if (error < 0)
1045                 return error;
1046         if (error > 0)
1047                 return 0;
1048
1049         error = trunc_dealloc(ip, size);
1050         if (!error)
1051                 error = trunc_end(ip);
1052
1053         return error;
1054 }
1055
1056 static int do_touch(struct gfs2_inode *ip, u64 size)
1057 {
1058         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1059         struct buffer_head *dibh;
1060         int error;
1061
1062         error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1063         if (error)
1064                 return error;
1065
1066         down_write(&ip->i_rw_mutex);
1067
1068         error = gfs2_meta_inode_buffer(ip, &dibh);
1069         if (error)
1070                 goto do_touch_out;
1071
1072         ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1073         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1074         gfs2_dinode_out(ip, dibh->b_data);
1075         brelse(dibh);
1076
1077 do_touch_out:
1078         up_write(&ip->i_rw_mutex);
1079         gfs2_trans_end(sdp);
1080         return error;
1081 }
1082
1083 /**
1084  * gfs2_truncatei - make a file a given size
1085  * @ip: the inode
1086  * @size: the size to make the file
1087  * @truncator: function to truncate the last partial block
1088  *
1089  * The file size can grow, shrink, or stay the same size.
1090  *
1091  * Returns: errno
1092  */
1093
1094 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1095 {
1096         int error;
1097
1098         if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1099                 return -EINVAL;
1100
1101         if (size > ip->i_di.di_size)
1102                 error = do_grow(ip, size);
1103         else if (size < ip->i_di.di_size)
1104                 error = do_shrink(ip, size);
1105         else
1106                 /* update time stamps */
1107                 error = do_touch(ip, size);
1108
1109         return error;
1110 }
1111
1112 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1113 {
1114         int error;
1115         error = trunc_dealloc(ip, ip->i_di.di_size);
1116         if (!error)
1117                 error = trunc_end(ip);
1118         return error;
1119 }
1120
1121 int gfs2_file_dealloc(struct gfs2_inode *ip)
1122 {
1123         return trunc_dealloc(ip, 0);
1124 }
1125
1126 /**
1127  * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1128  * @ip: the file
1129  * @len: the number of bytes to be written to the file
1130  * @data_blocks: returns the number of data blocks required
1131  * @ind_blocks: returns the number of indirect blocks required
1132  *
1133  */
1134
1135 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1136                             unsigned int *data_blocks, unsigned int *ind_blocks)
1137 {
1138         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1139         unsigned int tmp;
1140
1141         if (gfs2_is_dir(ip)) {
1142                 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1143                 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1144         } else {
1145                 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1146                 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1147         }
1148
1149         for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1150                 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1151                 *ind_blocks += tmp;
1152         }
1153 }
1154
1155 /**
1156  * gfs2_write_alloc_required - figure out if a write will require an allocation
1157  * @ip: the file being written to
1158  * @offset: the offset to write to
1159  * @len: the number of bytes being written
1160  * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1161  *
1162  * Returns: errno
1163  */
1164
1165 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1166                               unsigned int len, int *alloc_required)
1167 {
1168         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1169         struct buffer_head bh;
1170         unsigned int shift;
1171         u64 lblock, lblock_stop, size;
1172
1173         *alloc_required = 0;
1174
1175         if (!len)
1176                 return 0;
1177
1178         if (gfs2_is_stuffed(ip)) {
1179                 if (offset + len >
1180                     sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1181                         *alloc_required = 1;
1182                 return 0;
1183         }
1184
1185         *alloc_required = 1;
1186         shift = sdp->sd_sb.sb_bsize_shift;
1187         if (gfs2_is_dir(ip)) {
1188                 unsigned int bsize = sdp->sd_jbsize;
1189                 lblock = offset;
1190                 do_div(lblock, bsize);
1191                 lblock_stop = offset + len + bsize - 1;
1192                 do_div(lblock_stop, bsize);
1193         } else {
1194                 u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
1195                 lblock = offset >> shift;
1196                 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1197                 if (lblock_stop > end_of_file)
1198                         return 0;
1199         }
1200
1201         size = (lblock_stop - lblock) << shift;
1202         do {
1203                 bh.b_state = 0;
1204                 bh.b_size = size;
1205                 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1206                 if (!buffer_mapped(&bh))
1207                         return 0;
1208                 size -= bh.b_size;
1209                 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1210         } while(size > 0);
1211
1212         *alloc_required = 0;
1213         return 0;
1214 }
1215