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