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