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