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