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