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