]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/gfs2/bmap.c
[GFS2] Set d_ops for root inode
[mirror_ubuntu-jammy-kernel.git] / fs / gfs2 / bmap.c
CommitLineData
b3b94faa
DT
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>
5c676f6d 15#include <linux/gfs2_ondisk.h>
71b86f56 16#include <linux/crc32.h>
b3b94faa
DT
17#include <asm/semaphore.h>
18
19#include "gfs2.h"
5c676f6d
SW
20#include "lm_interface.h"
21#include "incore.h"
b3b94faa
DT
22#include "bmap.h"
23#include "glock.h"
24#include "inode.h"
b3b94faa
DT
25#include "meta_io.h"
26#include "page.h"
27#include "quota.h"
28#include "rgrp.h"
29#include "trans.h"
18ec7d5c 30#include "dir.h"
5c676f6d 31#include "util.h"
b3b94faa
DT
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 */
37struct metapath {
38 __u16 mp_list[GFS2_MAX_META_HEIGHT];
39};
40
41typedef 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
46struct strip_mine {
47 int sm_first;
48 unsigned int sm_height;
49};
50
b3b94faa
DT
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
63int 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;
18ec7d5c 68 int isdir = gfs2_is_dir(ip);
b3b94faa
DT
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
18ec7d5c 81 if (isdir) {
b3b94faa
DT
82 block = gfs2_alloc_meta(ip);
83
61e085a8 84 error = gfs2_dir_get_new_buffer(ip, block, &bh);
b3b94faa
DT
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
d4e9c4c3 102 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
103
104 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
105
106 if (ip->i_di.di_size) {
568f4c96
SW
107 *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) =
108 cpu_to_be64(block);
b3b94faa
DT
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
138static 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
18ec7d5c 147 if (gfs2_is_dir(ip)) {
b3b94faa
DT
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
173static 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);
d4e9c4c3 202 gfs2_trans_add_bh(ip->i_gl, bh, 1);
b3b94faa
DT
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
d4e9c4c3 215 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
216
217 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
218
219 if (new_block) {
568f4c96
SW
220 *(uint64_t *)(dibh->b_data +
221 sizeof(struct gfs2_dinode)) =
222 cpu_to_be64(block);
b3b94faa
DT
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
568f4c96
SW
294static void find_metapath(struct gfs2_inode *ip, uint64_t block,
295 struct metapath *mp)
b3b94faa
DT
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
317static inline uint64_t *metapointer(struct buffer_head *bh,
318 unsigned int height, struct metapath *mp)
319{
320 unsigned int head_size = (height > 0) ?
321 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
322
323 return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height];
324}
325
326/**
327 * lookup_block - Get the next metadata block in metadata tree
328 * @ip: The GFS2 inode
329 * @bh: Buffer containing the pointers to metadata blocks
330 * @height: The height of the tree (0 = dinode)
331 * @mp: The metapath
332 * @create: Non-zero if we may create a new meatdata block
333 * @new: Used to indicate if we did create a new metadata block
334 * @block: the returned disk block number
335 *
336 * Given a metatree, complete to a particular height, checks to see if the next
337 * height of the tree exists. If not the next height of the tree is created.
338 * The block number of the next height of the metadata tree is returned.
339 *
340 */
341
342static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
343 unsigned int height, struct metapath *mp, int create,
344 int *new, uint64_t *block)
345{
346 uint64_t *ptr = metapointer(bh, height, mp);
347
348 if (*ptr) {
349 *block = be64_to_cpu(*ptr);
350 return;
351 }
352
353 *block = 0;
354
355 if (!create)
356 return;
357
358 if (height == ip->i_di.di_height - 1 &&
18ec7d5c 359 !gfs2_is_dir(ip))
b3b94faa
DT
360 *block = gfs2_alloc_data(ip);
361 else
362 *block = gfs2_alloc_meta(ip);
363
d4e9c4c3 364 gfs2_trans_add_bh(ip->i_gl, bh, 1);
b3b94faa
DT
365
366 *ptr = cpu_to_be64(*block);
367 ip->i_di.di_blocks++;
368
369 *new = 1;
370}
371
372/**
373 * gfs2_block_map - Map a block from an inode to a disk block
374 * @ip: The GFS2 inode
375 * @lblock: The logical block number
376 * @new: Value/Result argument (1 = may create/did create new blocks)
377 * @dblock: the disk block number of the start of an extent
378 * @extlen: the size of the extent
379 *
380 * Find the block number on the current device which corresponds to an
381 * inode's block. If the block had to be created, "new" will be set.
382 *
383 * Returns: errno
384 */
385
386int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
387 uint64_t *dblock, uint32_t *extlen)
388{
389 struct gfs2_sbd *sdp = ip->i_sbd;
390 struct buffer_head *bh;
391 struct metapath mp;
392 int create = *new;
393 unsigned int bsize;
394 unsigned int height;
395 unsigned int end_of_metadata;
396 unsigned int x;
397 int error = 0;
398
399 *new = 0;
400 *dblock = 0;
401 if (extlen)
402 *extlen = 0;
403
404 if (create)
405 down_write(&ip->i_rw_mutex);
406 else
407 down_read(&ip->i_rw_mutex);
408
409 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
410 goto out;
411
18ec7d5c 412 bsize = (gfs2_is_dir(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
b3b94faa
DT
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 lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
443
444 if (extlen && *dblock) {
445 *extlen = 1;
446
447 if (!*new) {
448 uint64_t tmp_dblock;
449 int tmp_new;
450 unsigned int nptrs;
451
452 nptrs = (end_of_metadata) ? sdp->sd_inptrs :
453 sdp->sd_diptrs;
454
455 while (++mp.mp_list[end_of_metadata] < nptrs) {
456 lookup_block(ip, bh, end_of_metadata, &mp,
457 0, &tmp_new, &tmp_dblock);
458
459 if (*dblock + *extlen != tmp_dblock)
460 break;
461
462 (*extlen)++;
463 }
464 }
465 }
466
467 brelse(bh);
468
469 if (*new) {
470 error = gfs2_meta_inode_buffer(ip, &bh);
471 if (!error) {
d4e9c4c3 472 gfs2_trans_add_bh(ip->i_gl, bh, 1);
b3b94faa
DT
473 gfs2_dinode_out(&ip->i_di, bh->b_data);
474 brelse(bh);
475 }
476 }
477
478 out:
479 if (create)
480 up_write(&ip->i_rw_mutex);
481 else
482 up_read(&ip->i_rw_mutex);
483
484 return error;
485}
486
487/**
488 * recursive_scan - recursively scan through the end of a file
489 * @ip: the inode
490 * @dibh: the dinode buffer
491 * @mp: the path through the metadata to the point to start
492 * @height: the height the recursion is at
493 * @block: the indirect block to look at
494 * @first: 1 if this is the first block
495 * @bc: the call to make for each piece of metadata
496 * @data: data opaque to this function to pass to @bc
497 *
498 * When this is first called @height and @block should be zero and
499 * @first should be 1.
500 *
501 * Returns: errno
502 */
503
504static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
505 struct metapath *mp, unsigned int height,
506 uint64_t block, int first, block_call_t bc,
507 void *data)
508{
509 struct gfs2_sbd *sdp = ip->i_sbd;
510 struct buffer_head *bh = NULL;
511 uint64_t *top, *bottom;
512 uint64_t bn;
513 int error;
514 int mh_size = sizeof(struct gfs2_meta_header);
515
516 if (!height) {
517 error = gfs2_meta_inode_buffer(ip, &bh);
518 if (error)
519 return error;
520 dibh = bh;
521
522 top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
523 mp->mp_list[0];
524 bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
525 sdp->sd_diptrs;
526 } else {
527 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
528 if (error)
529 return error;
530
531 top = (uint64_t *)(bh->b_data + mh_size) +
532 ((first) ? mp->mp_list[height] : 0);
533
534 bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
535 }
536
537 error = bc(ip, dibh, bh, top, bottom, height, data);
538 if (error)
539 goto out;
540
541 if (height < ip->i_di.di_height - 1)
542 for (; top < bottom; top++, first = 0) {
543 if (!*top)
544 continue;
545
546 bn = be64_to_cpu(*top);
547
548 error = recursive_scan(ip, dibh, mp, height + 1, bn,
549 first, bc, data);
550 if (error)
551 break;
552 }
553
554 out:
555 brelse(bh);
556
557 return error;
558}
559
560/**
561 * do_strip - Look for a layer a particular layer of the file and strip it off
562 * @ip: the inode
563 * @dibh: the dinode buffer
564 * @bh: A buffer of pointers
565 * @top: The first pointer in the buffer
566 * @bottom: One more than the last pointer
567 * @height: the height this buffer is at
568 * @data: a pointer to a struct strip_mine
569 *
570 * Returns: errno
571 */
572
573static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
574 struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
575 unsigned int height, void *data)
576{
577 struct strip_mine *sm = (struct strip_mine *)data;
578 struct gfs2_sbd *sdp = ip->i_sbd;
579 struct gfs2_rgrp_list rlist;
580 uint64_t bn, bstart;
581 uint32_t blen;
582 uint64_t *p;
583 unsigned int rg_blocks = 0;
584 int metadata;
585 unsigned int revokes = 0;
586 int x;
587 int error;
588
589 if (!*top)
590 sm->sm_first = 0;
591
592 if (height != sm->sm_height)
593 return 0;
594
595 if (sm->sm_first) {
596 top++;
597 sm->sm_first = 0;
598 }
599
18ec7d5c 600 metadata = (height != ip->i_di.di_height - 1);
b3b94faa
DT
601 if (metadata)
602 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
603
604 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
605 if (error)
606 return error;
607
608 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
609 bstart = 0;
610 blen = 0;
611
612 for (p = top; p < bottom; p++) {
613 if (!*p)
614 continue;
615
616 bn = be64_to_cpu(*p);
617
618 if (bstart + blen == bn)
619 blen++;
620 else {
621 if (bstart)
622 gfs2_rlist_add(sdp, &rlist, bstart);
623
624 bstart = bn;
625 blen = 1;
626 }
627 }
628
629 if (bstart)
630 gfs2_rlist_add(sdp, &rlist, bstart);
631 else
632 goto out; /* Nothing to do */
633
634 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
635
636 for (x = 0; x < rlist.rl_rgrps; x++) {
637 struct gfs2_rgrpd *rgd;
5c676f6d 638 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
b3b94faa
DT
639 rg_blocks += rgd->rd_ri.ri_length;
640 }
641
642 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
643 if (error)
644 goto out_rlist;
645
646 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
647 RES_INDIRECT + RES_STATFS + RES_QUOTA,
648 revokes);
649 if (error)
650 goto out_rg_gunlock;
651
652 down_write(&ip->i_rw_mutex);
653
d4e9c4c3
SW
654 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
655 gfs2_trans_add_bh(ip->i_gl, bh, 1);
b3b94faa
DT
656
657 bstart = 0;
658 blen = 0;
659
660 for (p = top; p < bottom; p++) {
661 if (!*p)
662 continue;
663
664 bn = be64_to_cpu(*p);
665
666 if (bstart + blen == bn)
667 blen++;
668 else {
669 if (bstart) {
670 if (metadata)
671 gfs2_free_meta(ip, bstart, blen);
672 else
673 gfs2_free_data(ip, bstart, blen);
674 }
675
676 bstart = bn;
677 blen = 1;
678 }
679
680 *p = 0;
681 if (!ip->i_di.di_blocks)
682 gfs2_consist_inode(ip);
683 ip->i_di.di_blocks--;
684 }
685 if (bstart) {
686 if (metadata)
687 gfs2_free_meta(ip, bstart, blen);
688 else
689 gfs2_free_data(ip, bstart, blen);
690 }
691
692 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
693
694 gfs2_dinode_out(&ip->i_di, dibh->b_data);
695
696 up_write(&ip->i_rw_mutex);
697
698 gfs2_trans_end(sdp);
699
700 out_rg_gunlock:
701 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
702
703 out_rlist:
704 gfs2_rlist_free(&rlist);
705
706 out:
707 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
708
709 return error;
710}
711
712/**
713 * do_grow - Make a file look bigger than it is
714 * @ip: the inode
715 * @size: the size to set the file to
716 *
717 * Called with an exclusive lock on @ip.
718 *
719 * Returns: errno
720 */
721
722static int do_grow(struct gfs2_inode *ip, uint64_t size)
723{
724 struct gfs2_sbd *sdp = ip->i_sbd;
725 struct gfs2_alloc *al;
726 struct buffer_head *dibh;
727 unsigned int h;
728 int error;
729
730 al = gfs2_alloc_get(ip);
731
732 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
733 if (error)
734 goto out;
735
736 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
737 if (error)
738 goto out_gunlock_q;
739
740 al->al_requested = sdp->sd_max_height + RES_DATA;
741
742 error = gfs2_inplace_reserve(ip);
743 if (error)
744 goto out_gunlock_q;
745
746 error = gfs2_trans_begin(sdp,
747 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
748 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
749 if (error)
750 goto out_ipres;
751
752 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
753 if (gfs2_is_stuffed(ip)) {
754 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
755 NULL);
756 if (error)
757 goto out_end_trans;
758 }
759
760 h = calc_tree_height(ip, size);
761 if (ip->i_di.di_height < h) {
762 down_write(&ip->i_rw_mutex);
763 error = build_height(ip, h);
764 up_write(&ip->i_rw_mutex);
765 if (error)
766 goto out_end_trans;
767 }
768 }
769
770 ip->i_di.di_size = size;
771 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
772
773 error = gfs2_meta_inode_buffer(ip, &dibh);
774 if (error)
775 goto out_end_trans;
776
d4e9c4c3 777 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
778 gfs2_dinode_out(&ip->i_di, dibh->b_data);
779 brelse(dibh);
780
781 out_end_trans:
782 gfs2_trans_end(sdp);
783
784 out_ipres:
785 gfs2_inplace_release(ip);
786
787 out_gunlock_q:
788 gfs2_quota_unlock(ip);
789
790 out:
791 gfs2_alloc_put(ip);
792
793 return error;
794}
795
aa6a85a9 796static int trunc_start(struct gfs2_inode *ip, uint64_t size)
b3b94faa
DT
797{
798 struct gfs2_sbd *sdp = ip->i_sbd;
799 struct buffer_head *dibh;
800 int journaled = gfs2_is_jdata(ip);
801 int error;
802
803 error = gfs2_trans_begin(sdp,
804 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
805 if (error)
806 return error;
807
808 error = gfs2_meta_inode_buffer(ip, &dibh);
809 if (error)
810 goto out;
811
812 if (gfs2_is_stuffed(ip)) {
813 ip->i_di.di_size = size;
814 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
d4e9c4c3 815 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
816 gfs2_dinode_out(&ip->i_di, dibh->b_data);
817 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
818 error = 1;
819
820 } else {
18ec7d5c 821 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
257f9b4e 822 error = gfs2_block_truncate_page(ip->i_vnode->i_mapping);
b3b94faa
DT
823
824 if (!error) {
825 ip->i_di.di_size = size;
826 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
827 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
d4e9c4c3 828 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
829 gfs2_dinode_out(&ip->i_di, dibh->b_data);
830 }
831 }
832
833 brelse(dibh);
834
835 out:
836 gfs2_trans_end(sdp);
837
838 return error;
839}
840
841static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
842{
843 unsigned int height = ip->i_di.di_height;
844 uint64_t lblock;
845 struct metapath mp;
846 int error;
847
848 if (!size)
849 lblock = 0;
18ec7d5c 850 else
b3b94faa
DT
851 lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
852
853 find_metapath(ip, lblock, &mp);
854 gfs2_alloc_get(ip);
855
856 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
857 if (error)
858 goto out;
859
860 while (height--) {
861 struct strip_mine sm;
862 sm.sm_first = !!size;
863 sm.sm_height = height;
864
865 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
866 if (error)
867 break;
868 }
869
870 gfs2_quota_unhold(ip);
871
872 out:
873 gfs2_alloc_put(ip);
874 return error;
875}
876
877static int trunc_end(struct gfs2_inode *ip)
878{
879 struct gfs2_sbd *sdp = ip->i_sbd;
880 struct buffer_head *dibh;
881 int error;
882
883 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
884 if (error)
885 return error;
886
887 down_write(&ip->i_rw_mutex);
888
889 error = gfs2_meta_inode_buffer(ip, &dibh);
890 if (error)
891 goto out;
892
893 if (!ip->i_di.di_size) {
894 ip->i_di.di_height = 0;
895 ip->i_di.di_goal_meta =
896 ip->i_di.di_goal_data =
897 ip->i_num.no_addr;
898 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
899 }
900 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
901 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
902
d4e9c4c3 903 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
904 gfs2_dinode_out(&ip->i_di, dibh->b_data);
905 brelse(dibh);
906
907 out:
908 up_write(&ip->i_rw_mutex);
909
910 gfs2_trans_end(sdp);
911
912 return error;
913}
914
915/**
916 * do_shrink - make a file smaller
917 * @ip: the inode
918 * @size: the size to make the file
919 * @truncator: function to truncate the last partial block
920 *
921 * Called with an exclusive lock on @ip.
922 *
923 * Returns: errno
924 */
925
aa6a85a9 926static int do_shrink(struct gfs2_inode *ip, uint64_t size)
b3b94faa
DT
927{
928 int error;
929
aa6a85a9 930 error = trunc_start(ip, size);
b3b94faa
DT
931 if (error < 0)
932 return error;
933 if (error > 0)
934 return 0;
935
936 error = trunc_dealloc(ip, size);
937 if (!error)
938 error = trunc_end(ip);
939
940 return error;
941}
942
943/**
666a2c53 944 * gfs2_truncatei - make a file a given size
b3b94faa
DT
945 * @ip: the inode
946 * @size: the size to make the file
947 * @truncator: function to truncate the last partial block
948 *
949 * The file size can grow, shrink, or stay the same size.
950 *
951 * Returns: errno
952 */
953
aa6a85a9 954int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
b3b94faa
DT
955{
956 int error;
957
958 if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
959 return -EINVAL;
960
961 if (size > ip->i_di.di_size)
962 error = do_grow(ip, size);
963 else
aa6a85a9 964 error = do_shrink(ip, size);
b3b94faa
DT
965
966 return error;
967}
968
969int gfs2_truncatei_resume(struct gfs2_inode *ip)
970{
971 int error;
972 error = trunc_dealloc(ip, ip->i_di.di_size);
973 if (!error)
974 error = trunc_end(ip);
975 return error;
976}
977
978int gfs2_file_dealloc(struct gfs2_inode *ip)
979{
980 return trunc_dealloc(ip, 0);
981}
982
983/**
984 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
985 * @ip: the file
986 * @len: the number of bytes to be written to the file
987 * @data_blocks: returns the number of data blocks required
988 * @ind_blocks: returns the number of indirect blocks required
989 *
990 */
991
992void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
993 unsigned int *data_blocks, unsigned int *ind_blocks)
994{
995 struct gfs2_sbd *sdp = ip->i_sbd;
996 unsigned int tmp;
997
18ec7d5c 998 if (gfs2_is_dir(ip)) {
5c676f6d 999 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
b3b94faa
DT
1000 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1001 } else {
1002 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1003 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1004 }
1005
1006 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
5c676f6d 1007 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
b3b94faa
DT
1008 *ind_blocks += tmp;
1009 }
1010}
1011
1012/**
1013 * gfs2_write_alloc_required - figure out if a write will require an allocation
1014 * @ip: the file being written to
1015 * @offset: the offset to write to
1016 * @len: the number of bytes being written
1017 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1018 *
1019 * Returns: errno
1020 */
1021
1022int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
1023 unsigned int len, int *alloc_required)
1024{
1025 struct gfs2_sbd *sdp = ip->i_sbd;
1026 uint64_t lblock, lblock_stop, dblock;
1027 uint32_t extlen;
1028 int new = 0;
1029 int error = 0;
1030
1031 *alloc_required = 0;
1032
1033 if (!len)
1034 return 0;
1035
1036 if (gfs2_is_stuffed(ip)) {
1037 if (offset + len >
1038 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1039 *alloc_required = 1;
1040 return 0;
1041 }
1042
18ec7d5c 1043 if (gfs2_is_dir(ip)) {
b3b94faa
DT
1044 unsigned int bsize = sdp->sd_jbsize;
1045 lblock = offset;
1046 do_div(lblock, bsize);
1047 lblock_stop = offset + len + bsize - 1;
1048 do_div(lblock_stop, bsize);
1049 } else {
1050 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1051 lblock = offset >> shift;
1052 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1053 }
1054
1055 for (; lblock < lblock_stop; lblock += extlen) {
1056 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
1057 if (error)
1058 return error;
1059
1060 if (!dblock) {
1061 *alloc_required = 1;
1062 return 0;
1063 }
1064 }
1065
1066 return 0;
1067}
1068