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