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