Merge branch 'master'
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / 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 "meta_io.h"
22 #include "page.h"
23 #include "quota.h"
24 #include "rgrp.h"
25 #include "trans.h"
26 #include "dir.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 isdir = gfs2_is_dir(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 (isdir) {
107 block = gfs2_alloc_meta(ip);
108
109 error = gfs2_dir_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_dir(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_dir(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_dir(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);
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 trunc_start(struct gfs2_inode *ip, uint64_t size)
818 {
819 struct gfs2_sbd *sdp = ip->i_sbd;
820 struct buffer_head *dibh;
821 int journaled = gfs2_is_jdata(ip);
822 int error;
823
824 error = gfs2_trans_begin(sdp,
825 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
826 if (error)
827 return error;
828
829 error = gfs2_meta_inode_buffer(ip, &dibh);
830 if (error)
831 goto out;
832
833 if (gfs2_is_stuffed(ip)) {
834 ip->i_di.di_size = size;
835 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
836 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
837 gfs2_dinode_out(&ip->i_di, dibh->b_data);
838 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
839 error = 1;
840
841 } else {
842 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
843 error = gfs2_block_truncate_page(ip->i_vnode->i_mapping);
844
845 if (!error) {
846 ip->i_di.di_size = size;
847 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
848 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
849 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
850 gfs2_dinode_out(&ip->i_di, dibh->b_data);
851 }
852 }
853
854 brelse(dibh);
855
856 out:
857 gfs2_trans_end(sdp);
858
859 return error;
860 }
861
862 static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
863 {
864 unsigned int height = ip->i_di.di_height;
865 uint64_t lblock;
866 struct metapath mp;
867 int error;
868
869 if (!size)
870 lblock = 0;
871 else
872 lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
873
874 find_metapath(ip, lblock, &mp);
875 gfs2_alloc_get(ip);
876
877 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
878 if (error)
879 goto out;
880
881 while (height--) {
882 struct strip_mine sm;
883 sm.sm_first = !!size;
884 sm.sm_height = height;
885
886 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
887 if (error)
888 break;
889 }
890
891 gfs2_quota_unhold(ip);
892
893 out:
894 gfs2_alloc_put(ip);
895 return error;
896 }
897
898 static int trunc_end(struct gfs2_inode *ip)
899 {
900 struct gfs2_sbd *sdp = ip->i_sbd;
901 struct buffer_head *dibh;
902 int error;
903
904 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
905 if (error)
906 return error;
907
908 down_write(&ip->i_rw_mutex);
909
910 error = gfs2_meta_inode_buffer(ip, &dibh);
911 if (error)
912 goto out;
913
914 if (!ip->i_di.di_size) {
915 ip->i_di.di_height = 0;
916 ip->i_di.di_goal_meta =
917 ip->i_di.di_goal_data =
918 ip->i_num.no_addr;
919 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
920 }
921 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
922 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
923
924 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
925 gfs2_dinode_out(&ip->i_di, dibh->b_data);
926 brelse(dibh);
927
928 out:
929 up_write(&ip->i_rw_mutex);
930
931 gfs2_trans_end(sdp);
932
933 return error;
934 }
935
936 /**
937 * do_shrink - make a file smaller
938 * @ip: the inode
939 * @size: the size to make the file
940 * @truncator: function to truncate the last partial block
941 *
942 * Called with an exclusive lock on @ip.
943 *
944 * Returns: errno
945 */
946
947 static int do_shrink(struct gfs2_inode *ip, uint64_t size)
948 {
949 int error;
950
951 error = trunc_start(ip, size);
952 if (error < 0)
953 return error;
954 if (error > 0)
955 return 0;
956
957 error = trunc_dealloc(ip, size);
958 if (!error)
959 error = trunc_end(ip);
960
961 return error;
962 }
963
964 /**
965 * gfs2_truncatei - make a file a given size
966 * @ip: the inode
967 * @size: the size to make the file
968 * @truncator: function to truncate the last partial block
969 *
970 * The file size can grow, shrink, or stay the same size.
971 *
972 * Returns: errno
973 */
974
975 int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
976 {
977 int error;
978
979 if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
980 return -EINVAL;
981
982 if (size > ip->i_di.di_size)
983 error = do_grow(ip, size);
984 else
985 error = do_shrink(ip, size);
986
987 return error;
988 }
989
990 int gfs2_truncatei_resume(struct gfs2_inode *ip)
991 {
992 int error;
993 error = trunc_dealloc(ip, ip->i_di.di_size);
994 if (!error)
995 error = trunc_end(ip);
996 return error;
997 }
998
999 int gfs2_file_dealloc(struct gfs2_inode *ip)
1000 {
1001 return trunc_dealloc(ip, 0);
1002 }
1003
1004 /**
1005 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1006 * @ip: the file
1007 * @len: the number of bytes to be written to the file
1008 * @data_blocks: returns the number of data blocks required
1009 * @ind_blocks: returns the number of indirect blocks required
1010 *
1011 */
1012
1013 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1014 unsigned int *data_blocks, unsigned int *ind_blocks)
1015 {
1016 struct gfs2_sbd *sdp = ip->i_sbd;
1017 unsigned int tmp;
1018
1019 if (gfs2_is_dir(ip)) {
1020 *data_blocks = DIV_RU(len, sdp->sd_jbsize) + 2;
1021 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1022 } else {
1023 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1024 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1025 }
1026
1027 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1028 tmp = DIV_RU(tmp, sdp->sd_inptrs);
1029 *ind_blocks += tmp;
1030 }
1031 }
1032
1033 /**
1034 * gfs2_write_alloc_required - figure out if a write will require an allocation
1035 * @ip: the file being written to
1036 * @offset: the offset to write to
1037 * @len: the number of bytes being written
1038 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1039 *
1040 * Returns: errno
1041 */
1042
1043 int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
1044 unsigned int len, int *alloc_required)
1045 {
1046 struct gfs2_sbd *sdp = ip->i_sbd;
1047 uint64_t lblock, lblock_stop, dblock;
1048 uint32_t extlen;
1049 int new = 0;
1050 int error = 0;
1051
1052 *alloc_required = 0;
1053
1054 if (!len)
1055 return 0;
1056
1057 if (gfs2_is_stuffed(ip)) {
1058 if (offset + len >
1059 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1060 *alloc_required = 1;
1061 return 0;
1062 }
1063
1064 if (gfs2_is_dir(ip)) {
1065 unsigned int bsize = sdp->sd_jbsize;
1066 lblock = offset;
1067 do_div(lblock, bsize);
1068 lblock_stop = offset + len + bsize - 1;
1069 do_div(lblock_stop, bsize);
1070 } else {
1071 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1072 lblock = offset >> shift;
1073 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1074 }
1075
1076 for (; lblock < lblock_stop; lblock += extlen) {
1077 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
1078 if (error)
1079 return error;
1080
1081 if (!dblock) {
1082 *alloc_required = 1;
1083 return 0;
1084 }
1085 }
1086
1087 return 0;
1088 }
1089