blob: 98fa07c2b710ee6db00c5a1942638550d430474d [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050016#include <linux/crc32.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "lm_interface.h"
20#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "bmap.h"
22#include "glock.h"
23#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "meta_io.h"
25#include "page.h"
26#include "quota.h"
27#include "rgrp.h"
28#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000029#include "dir.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050030#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000031
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
50/**
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 * 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;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000067 int isdir = gfs2_is_dir(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +000068 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
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000080 if (isdir) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000081 block = gfs2_alloc_meta(ip);
82
Steven Whitehouse61e085a2006-04-24 10:07:13 -040083 error = gfs2_dir_get_new_buffer(ip, block, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +000084 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
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000101 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000102
103 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
104
105 if (ip->i_di.di_size) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500106 *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) =
107 cpu_to_be64(block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400139 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 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
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000146 if (gfs2_is_dir(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 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 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166 *
167 * Returns: errno
168 */
169
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400170static int build_height(struct inode *inode, unsigned height)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400172 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400173 unsigned new_height = height - ip->i_di.di_height;
174 struct buffer_head *dibh;
175 struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 int error;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400177 u64 *bp;
178 u64 bn;
179 unsigned n;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400181 if (height <= ip->i_di.di_height)
182 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400184 error = gfs2_meta_inode_buffer(ip, &dibh);
185 if (error)
186 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000187
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400188 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);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000192 }
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400193
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,
199 GFS2_FORMAT_IN);
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;
207 }
208 }
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;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222}
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
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500283static void find_metapath(struct gfs2_inode *ip, uint64_t block,
284 struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400286 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287 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
Steven Whitehousefd88de562006-05-05 16:59:11 -0400306static inline u64 *metapointer(struct buffer_head *bh, int *boundary,
307 unsigned int height, const struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308{
309 unsigned int head_size = (height > 0) ?
310 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400311 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;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317}
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
Steven Whitehousefd88de562006-05-05 16:59:11 -0400335static 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)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400339 int boundary;
340 uint64_t *ptr = metapointer(bh, &boundary, height, mp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341
342 if (*ptr) {
343 *block = be64_to_cpu(*ptr);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400344 return boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345 }
346
347 *block = 0;
348
349 if (!create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400350 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351
Steven Whitehousefd88de562006-05-05 16:59:11 -0400352 if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 *block = gfs2_alloc_data(ip);
354 else
355 *block = gfs2_alloc_meta(ip);
356
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000357 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358
359 *ptr = cpu_to_be64(*block);
360 ip->i_di.di_blocks++;
361
362 *new = 1;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400363 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000364}
365
366/**
Steven Whitehousefd88de562006-05-05 16:59:11 -0400367 * gfs2_block_pointers - Map a block from an inode to a disk block
368 * @inode: The inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 * @lblock: The logical block number
370 * @new: Value/Result argument (1 = may create/did create new blocks)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400371 * @boundary: gets set if we've hit a block boundary
372 * @mp: metapath to use
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373 *
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
Steven Whitehousefd88de562006-05-05 16:59:11 -0400380static struct buffer_head *gfs2_block_pointers(struct inode *inode, u64 lblock,
381 int *new, u64 *dblock,
382 int *boundary,
383 struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400385 struct gfs2_inode *ip = GFS2_I(inode);
386 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 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;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397
398 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
399 goto out;
400
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000401 bsize = (gfs2_is_dir(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402
403 height = calc_tree_height(ip, (lblock + 1) * bsize);
404 if (ip->i_di.di_height < height) {
405 if (!create)
406 goto out;
407
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400408 error = build_height(inode, height);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409 if (error)
410 goto out;
411 }
412
Steven Whitehousefd88de562006-05-05 16:59:11 -0400413 find_metapath(ip, lblock, mp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 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++) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400421 lookup_block(ip, bh, x, mp, create, new, dblock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 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
Steven Whitehousefd88de562006-05-05 16:59:11 -0400431 *boundary = lookup_block(ip, bh, end_of_metadata, mp, create, new, dblock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432 if (*new) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400433 struct buffer_head *dibh;
434 error = gfs2_meta_inode_buffer(ip, &dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435 if (!error) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400436 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
437 gfs2_dinode_out(&ip->i_di, dibh->b_data);
438 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439 }
440 }
Steven Whitehousefd88de562006-05-05 16:59:11 -0400441 return bh;
442out:
443 return ERR_PTR(error);
444}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445
Steven Whitehousefd88de562006-05-05 16:59:11 -0400446
447static inline void bmap_lock(struct inode *inode, int create)
448{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400449 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400450 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400458 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 if (create)
460 up_write(&ip->i_rw_mutex);
461 else
462 up_read(&ip->i_rw_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400463}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464
Steven Whitehousefd88de562006-05-05 16:59:11 -0400465int 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400484 struct gfs2_inode *ip = GFS2_I(inode);
485 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400486 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;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520}
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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400544 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400612 struct strip_mine *sm = data;
613 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000614 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
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000635 metadata = (height != ip->i_di.di_height - 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636 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;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500673 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674 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
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000689 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
690 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691
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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400759 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 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);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400798 error = build_height(&ip->i_inode, h);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 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
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000812 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813 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
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000831static int trunc_start(struct gfs2_inode *ip, uint64_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400833 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 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();
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000850 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851 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 {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000856 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400857 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858
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;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000863 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 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;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000885 else
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400886 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887
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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400914 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000915 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
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000938 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000939 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
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000961static int do_shrink(struct gfs2_inode *ip, uint64_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000962{
963 int error;
964
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000965 error = trunc_start(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 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/**
Steven Whitehouse666a2c52006-01-18 10:29:04 +0000979 * gfs2_truncatei - make a file a given size
David Teiglandb3b94fa2006-01-16 16:50:04 +0000980 * @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
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000989int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990{
991 int error;
992
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400993 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_di.di_mode)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000994 return -EINVAL;
995
996 if (size > ip->i_di.di_size)
997 error = do_grow(ip, size);
998 else
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000999 error = do_shrink(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001000
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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001030 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031 unsigned int tmp;
1032
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001033 if (gfs2_is_dir(ip)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001034 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035 *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;) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001042 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001043 *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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001060 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 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
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001078 if (gfs2_is_dir(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079 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) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001091 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001092 if (error)
1093 return error;
1094
1095 if (!dblock) {
1096 *alloc_required = 1;
1097 return 0;
1098 }
1099 }
1100
1101 return 0;
1102}
1103