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