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