blob: b75a2f93dadab05187dad73077512c15db6e1643 [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
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "quota.h"
26#include "rgrp.h"
27#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000028#include "dir.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050029#include "util.h"
Steven Whitehouseba7f7292006-07-26 11:27:10 -040030#include "ops_address.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/**
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040051 * 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,
61 uint64_t block, struct page *page)
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);
Steven Whitehouse75d3b812006-09-04 11:41:31 -040097 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040098 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
109/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110 * 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
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400121int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122{
123 struct buffer_head *bh, *dibh;
124 uint64_t block = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000125 int isdir = gfs2_is_dir(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126 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
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000138 if (isdir) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139 block = gfs2_alloc_meta(ip);
140
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400141 error = gfs2_dir_get_new_buffer(ip, block, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000142 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
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400151 error = gfs2_unstuffer_page(ip, dibh, block, page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 if (error)
153 goto out_brelse;
154 }
155 }
156
157 /* Set up the pointer to the new block */
158
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000159 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160
161 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
162
163 if (ip->i_di.di_size) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500164 *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) =
165 cpu_to_be64(block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166 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
173 out_brelse:
174 brelse(dibh);
175
176 out:
177 up_write(&ip->i_rw_mutex);
178
179 return error;
180}
181
182/**
183 * calc_tree_height - Calculate the height of a metadata tree
184 * @ip: The GFS2 inode
185 * @size: The proposed size of the file
186 *
187 * Work out how tall a metadata tree needs to be in order to accommodate a
188 * file of a particular size. If size is less than the current size of
189 * the inode, then the current size of the inode is used instead of the
190 * supplied one.
191 *
192 * Returns: the height the tree should be
193 */
194
195static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
196{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400197 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198 uint64_t *arr;
199 unsigned int max, height;
200
201 if (ip->i_di.di_size > size)
202 size = ip->i_di.di_size;
203
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000204 if (gfs2_is_dir(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000205 arr = sdp->sd_jheightsize;
206 max = sdp->sd_max_jheight;
207 } else {
208 arr = sdp->sd_heightsize;
209 max = sdp->sd_max_height;
210 }
211
212 for (height = 0; height < max; height++)
213 if (arr[height] >= size)
214 break;
215
216 return height;
217}
218
219/**
220 * build_height - Build a metadata tree of the requested height
221 * @ip: The GFS2 inode
222 * @height: The height to build to
223 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224 *
225 * Returns: errno
226 */
227
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400228static int build_height(struct inode *inode, unsigned height)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400230 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400231 unsigned new_height = height - ip->i_di.di_height;
232 struct buffer_head *dibh;
233 struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234 int error;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400235 u64 *bp;
236 u64 bn;
237 unsigned n;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400239 if (height <= ip->i_di.di_height)
240 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400242 error = gfs2_meta_inode_buffer(ip, &dibh);
243 if (error)
244 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400246 for(n = 0; n < new_height; n++) {
247 bn = gfs2_alloc_meta(ip);
248 blocks[n] = gfs2_meta_new(ip->i_gl, bn);
249 gfs2_trans_add_bh(ip->i_gl, blocks[n], 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 }
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400251
252 n = 0;
253 bn = blocks[0]->b_blocknr;
254 if (new_height > 1) {
255 for(; n < new_height-1; n++) {
256 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
257 GFS2_FORMAT_IN);
258 gfs2_buffer_clear_tail(blocks[n],
259 sizeof(struct gfs2_meta_header));
260 bp = (u64 *)(blocks[n]->b_data +
261 sizeof(struct gfs2_meta_header));
262 *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
263 brelse(blocks[n]);
264 blocks[n] = NULL;
265 }
266 }
267 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
268 gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
269 dibh, sizeof(struct gfs2_dinode));
270 brelse(blocks[n]);
271 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
272 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
273 bp = (u64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
274 *bp = cpu_to_be64(bn);
275 ip->i_di.di_height += new_height;
276 ip->i_di.di_blocks += new_height;
277 gfs2_dinode_out(&ip->i_di, dibh->b_data);
278 brelse(dibh);
279 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280}
281
282/**
283 * find_metapath - Find path through the metadata tree
284 * @ip: The inode pointer
285 * @mp: The metapath to return the result in
286 * @block: The disk block to look up
287 *
288 * This routine returns a struct metapath structure that defines a path
289 * through the metadata of inode "ip" to get to block "block".
290 *
291 * Example:
292 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
293 * filesystem with a blocksize of 4096.
294 *
295 * find_metapath() would return a struct metapath structure set to:
296 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
297 * and mp_list[2] = 165.
298 *
299 * That means that in order to get to the block containing the byte at
300 * offset 101342453, we would load the indirect block pointed to by pointer
301 * 0 in the dinode. We would then load the indirect block pointed to by
302 * pointer 48 in that indirect block. We would then load the data block
303 * pointed to by pointer 165 in that indirect block.
304 *
305 * ----------------------------------------
306 * | Dinode | |
307 * | | 4|
308 * | |0 1 2 3 4 5 9|
309 * | | 6|
310 * ----------------------------------------
311 * |
312 * |
313 * V
314 * ----------------------------------------
315 * | Indirect Block |
316 * | 5|
317 * | 4 4 4 4 4 5 5 1|
318 * |0 5 6 7 8 9 0 1 2|
319 * ----------------------------------------
320 * |
321 * |
322 * V
323 * ----------------------------------------
324 * | Indirect Block |
325 * | 1 1 1 1 1 5|
326 * | 6 6 6 6 6 1|
327 * |0 3 4 5 6 7 2|
328 * ----------------------------------------
329 * |
330 * |
331 * V
332 * ----------------------------------------
333 * | Data block containing offset |
334 * | 101342453 |
335 * | |
336 * | |
337 * ----------------------------------------
338 *
339 */
340
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500341static void find_metapath(struct gfs2_inode *ip, uint64_t block,
342 struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400344 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345 uint64_t b = block;
346 unsigned int i;
347
348 for (i = ip->i_di.di_height; i--;)
349 mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
350
351}
352
353/**
354 * metapointer - Return pointer to start of metadata in a buffer
355 * @bh: The buffer
356 * @height: The metadata height (0 = dinode)
357 * @mp: The metapath
358 *
359 * Return a pointer to the block number of the next height of the metadata
360 * tree given a buffer containing the pointer to the current height of the
361 * metadata tree.
362 */
363
Steven Whitehousefd88de562006-05-05 16:59:11 -0400364static inline u64 *metapointer(struct buffer_head *bh, int *boundary,
365 unsigned int height, const struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366{
367 unsigned int head_size = (height > 0) ?
368 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400369 u64 *ptr;
370 *boundary = 0;
371 ptr = ((u64 *)(bh->b_data + head_size)) + mp->mp_list[height];
Steven Whitehouse75d3b812006-09-04 11:41:31 -0400372 if (ptr + 1 == (u64 *)(bh->b_data + bh->b_size))
Steven Whitehousefd88de562006-05-05 16:59:11 -0400373 *boundary = 1;
374 return ptr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000375}
376
377/**
378 * lookup_block - Get the next metadata block in metadata tree
379 * @ip: The GFS2 inode
380 * @bh: Buffer containing the pointers to metadata blocks
381 * @height: The height of the tree (0 = dinode)
382 * @mp: The metapath
383 * @create: Non-zero if we may create a new meatdata block
384 * @new: Used to indicate if we did create a new metadata block
385 * @block: the returned disk block number
386 *
387 * Given a metatree, complete to a particular height, checks to see if the next
388 * height of the tree exists. If not the next height of the tree is created.
389 * The block number of the next height of the metadata tree is returned.
390 *
391 */
392
Steven Whitehousefd88de562006-05-05 16:59:11 -0400393static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
394 unsigned int height, struct metapath *mp, int create,
395 int *new, uint64_t *block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400397 int boundary;
398 uint64_t *ptr = metapointer(bh, &boundary, height, mp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000399
400 if (*ptr) {
401 *block = be64_to_cpu(*ptr);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400402 return boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000403 }
404
405 *block = 0;
406
407 if (!create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400408 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409
Steven Whitehousefd88de562006-05-05 16:59:11 -0400410 if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411 *block = gfs2_alloc_data(ip);
412 else
413 *block = gfs2_alloc_meta(ip);
414
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000415 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416
417 *ptr = cpu_to_be64(*block);
418 ip->i_di.di_blocks++;
419
420 *new = 1;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400421 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422}
423
424/**
Steven Whitehousefd88de562006-05-05 16:59:11 -0400425 * gfs2_block_pointers - Map a block from an inode to a disk block
426 * @inode: The inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427 * @lblock: The logical block number
428 * @new: Value/Result argument (1 = may create/did create new blocks)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400429 * @boundary: gets set if we've hit a block boundary
430 * @mp: metapath to use
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431 *
432 * Find the block number on the current device which corresponds to an
433 * inode's block. If the block had to be created, "new" will be set.
434 *
435 * Returns: errno
436 */
437
Steven Whitehousefd88de562006-05-05 16:59:11 -0400438static struct buffer_head *gfs2_block_pointers(struct inode *inode, u64 lblock,
439 int *new, u64 *dblock,
440 int *boundary,
441 struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400443 struct gfs2_inode *ip = GFS2_I(inode);
444 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446 int create = *new;
447 unsigned int bsize;
448 unsigned int height;
449 unsigned int end_of_metadata;
450 unsigned int x;
451 int error = 0;
452
453 *new = 0;
454 *dblock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455
456 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
457 goto out;
458
Steven Whitehouse75d3b812006-09-04 11:41:31 -0400459 bsize = gfs2_is_dir(ip) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460
461 height = calc_tree_height(ip, (lblock + 1) * bsize);
462 if (ip->i_di.di_height < height) {
463 if (!create)
464 goto out;
465
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400466 error = build_height(inode, height);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467 if (error)
468 goto out;
469 }
470
Steven Whitehousefd88de562006-05-05 16:59:11 -0400471 find_metapath(ip, lblock, mp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000472 end_of_metadata = ip->i_di.di_height - 1;
473
474 error = gfs2_meta_inode_buffer(ip, &bh);
475 if (error)
476 goto out;
477
478 for (x = 0; x < end_of_metadata; x++) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400479 lookup_block(ip, bh, x, mp, create, new, dblock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 brelse(bh);
481 if (!*dblock)
482 goto out;
483
484 error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
485 if (error)
486 goto out;
487 }
488
Steven Whitehousefd88de562006-05-05 16:59:11 -0400489 *boundary = lookup_block(ip, bh, end_of_metadata, mp, create, new, dblock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490 if (*new) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400491 struct buffer_head *dibh;
492 error = gfs2_meta_inode_buffer(ip, &dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493 if (!error) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400494 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
495 gfs2_dinode_out(&ip->i_di, dibh->b_data);
496 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000497 }
498 }
Steven Whitehousefd88de562006-05-05 16:59:11 -0400499 return bh;
500out:
501 return ERR_PTR(error);
502}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503
Steven Whitehousefd88de562006-05-05 16:59:11 -0400504
505static inline void bmap_lock(struct inode *inode, int create)
506{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400507 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400508 if (create)
509 down_write(&ip->i_rw_mutex);
510 else
511 down_read(&ip->i_rw_mutex);
512}
513
514static inline void bmap_unlock(struct inode *inode, int create)
515{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400516 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000517 if (create)
518 up_write(&ip->i_rw_mutex);
519 else
520 up_read(&ip->i_rw_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400521}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522
Steven Whitehousefd88de562006-05-05 16:59:11 -0400523int gfs2_block_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, int *boundary)
524{
525 struct metapath mp;
526 struct buffer_head *bh;
527 int create = *new;
528
529 bmap_lock(inode, create);
530 bh = gfs2_block_pointers(inode, lblock, new, dblock, boundary, &mp);
531 bmap_unlock(inode, create);
532 if (!bh)
533 return 0;
534 if (IS_ERR(bh))
535 return PTR_ERR(bh);
536 brelse(bh);
537 return 0;
538}
539
540int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
541{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400542 struct gfs2_inode *ip = GFS2_I(inode);
543 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400544 struct metapath mp;
545 struct buffer_head *bh;
546 int boundary;
547 int create = *new;
548
549 BUG_ON(!extlen);
550 BUG_ON(!dblock);
551 BUG_ON(!new);
552
553 bmap_lock(inode, create);
554 bh = gfs2_block_pointers(inode, lblock, new, dblock, &boundary, &mp);
555 *extlen = 1;
556
Steven Whitehouse75d3b812006-09-04 11:41:31 -0400557 if (bh != NULL && !IS_ERR(bh) && *dblock != 0 && *new == 0) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400558 u64 tmp_dblock;
559 int tmp_new;
560 unsigned int nptrs;
561 unsigned end_of_metadata = ip->i_di.di_height - 1;
562
563 nptrs = (end_of_metadata) ? sdp->sd_inptrs : sdp->sd_diptrs;
564 while (++mp.mp_list[end_of_metadata] < nptrs) {
565 lookup_block(ip, bh, end_of_metadata, &mp, 0, &tmp_new, &tmp_dblock);
566 if (*dblock + *extlen != tmp_dblock)
567 break;
Steven Whitehouse75d3b812006-09-04 11:41:31 -0400568 ++*extlen;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400569 }
570 }
571 bmap_unlock(inode, create);
572 if (!bh)
573 return 0;
574 if (IS_ERR(bh))
575 return PTR_ERR(bh);
576 brelse(bh);
577 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578}
579
580/**
581 * recursive_scan - recursively scan through the end of a file
582 * @ip: the inode
583 * @dibh: the dinode buffer
584 * @mp: the path through the metadata to the point to start
585 * @height: the height the recursion is at
586 * @block: the indirect block to look at
587 * @first: 1 if this is the first block
588 * @bc: the call to make for each piece of metadata
589 * @data: data opaque to this function to pass to @bc
590 *
591 * When this is first called @height and @block should be zero and
592 * @first should be 1.
593 *
594 * Returns: errno
595 */
596
597static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
598 struct metapath *mp, unsigned int height,
599 uint64_t block, int first, block_call_t bc,
600 void *data)
601{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400602 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603 struct buffer_head *bh = NULL;
604 uint64_t *top, *bottom;
605 uint64_t bn;
606 int error;
607 int mh_size = sizeof(struct gfs2_meta_header);
608
609 if (!height) {
610 error = gfs2_meta_inode_buffer(ip, &bh);
611 if (error)
612 return error;
613 dibh = bh;
614
Steven Whitehouse75d3b812006-09-04 11:41:31 -0400615 top = (u64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
616 bottom = (u64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617 } else {
618 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
619 if (error)
620 return error;
621
622 top = (uint64_t *)(bh->b_data + mh_size) +
623 ((first) ? mp->mp_list[height] : 0);
624
625 bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
626 }
627
628 error = bc(ip, dibh, bh, top, bottom, height, data);
629 if (error)
630 goto out;
631
632 if (height < ip->i_di.di_height - 1)
633 for (; top < bottom; top++, first = 0) {
634 if (!*top)
635 continue;
636
637 bn = be64_to_cpu(*top);
638
639 error = recursive_scan(ip, dibh, mp, height + 1, bn,
640 first, bc, data);
641 if (error)
642 break;
643 }
644
645 out:
646 brelse(bh);
647
648 return error;
649}
650
651/**
652 * do_strip - Look for a layer a particular layer of the file and strip it off
653 * @ip: the inode
654 * @dibh: the dinode buffer
655 * @bh: A buffer of pointers
656 * @top: The first pointer in the buffer
657 * @bottom: One more than the last pointer
658 * @height: the height this buffer is at
659 * @data: a pointer to a struct strip_mine
660 *
661 * Returns: errno
662 */
663
664static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
665 struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
666 unsigned int height, void *data)
667{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400668 struct strip_mine *sm = data;
669 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 struct gfs2_rgrp_list rlist;
671 uint64_t bn, bstart;
672 uint32_t blen;
673 uint64_t *p;
674 unsigned int rg_blocks = 0;
675 int metadata;
676 unsigned int revokes = 0;
677 int x;
678 int error;
679
680 if (!*top)
681 sm->sm_first = 0;
682
683 if (height != sm->sm_height)
684 return 0;
685
686 if (sm->sm_first) {
687 top++;
688 sm->sm_first = 0;
689 }
690
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000691 metadata = (height != ip->i_di.di_height - 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000692 if (metadata)
693 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
694
695 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
696 if (error)
697 return error;
698
699 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
700 bstart = 0;
701 blen = 0;
702
703 for (p = top; p < bottom; p++) {
704 if (!*p)
705 continue;
706
707 bn = be64_to_cpu(*p);
708
709 if (bstart + blen == bn)
710 blen++;
711 else {
712 if (bstart)
713 gfs2_rlist_add(sdp, &rlist, bstart);
714
715 bstart = bn;
716 blen = 1;
717 }
718 }
719
720 if (bstart)
721 gfs2_rlist_add(sdp, &rlist, bstart);
722 else
723 goto out; /* Nothing to do */
724
725 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
726
727 for (x = 0; x < rlist.rl_rgrps; x++) {
728 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500729 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 rg_blocks += rgd->rd_ri.ri_length;
731 }
732
733 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
734 if (error)
735 goto out_rlist;
736
737 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
738 RES_INDIRECT + RES_STATFS + RES_QUOTA,
739 revokes);
740 if (error)
741 goto out_rg_gunlock;
742
743 down_write(&ip->i_rw_mutex);
744
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000745 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
746 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747
748 bstart = 0;
749 blen = 0;
750
751 for (p = top; p < bottom; p++) {
752 if (!*p)
753 continue;
754
755 bn = be64_to_cpu(*p);
756
757 if (bstart + blen == bn)
758 blen++;
759 else {
760 if (bstart) {
761 if (metadata)
762 gfs2_free_meta(ip, bstart, blen);
763 else
764 gfs2_free_data(ip, bstart, blen);
765 }
766
767 bstart = bn;
768 blen = 1;
769 }
770
771 *p = 0;
772 if (!ip->i_di.di_blocks)
773 gfs2_consist_inode(ip);
774 ip->i_di.di_blocks--;
775 }
776 if (bstart) {
777 if (metadata)
778 gfs2_free_meta(ip, bstart, blen);
779 else
780 gfs2_free_data(ip, bstart, blen);
781 }
782
783 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
784
785 gfs2_dinode_out(&ip->i_di, dibh->b_data);
786
787 up_write(&ip->i_rw_mutex);
788
789 gfs2_trans_end(sdp);
790
791 out_rg_gunlock:
792 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
793
794 out_rlist:
795 gfs2_rlist_free(&rlist);
796
797 out:
798 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
799
800 return error;
801}
802
803/**
804 * do_grow - Make a file look bigger than it is
805 * @ip: the inode
806 * @size: the size to set the file to
807 *
808 * Called with an exclusive lock on @ip.
809 *
810 * Returns: errno
811 */
812
813static int do_grow(struct gfs2_inode *ip, uint64_t size)
814{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400815 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000816 struct gfs2_alloc *al;
817 struct buffer_head *dibh;
818 unsigned int h;
819 int error;
820
821 al = gfs2_alloc_get(ip);
822
823 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
824 if (error)
825 goto out;
826
827 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
828 if (error)
829 goto out_gunlock_q;
830
831 al->al_requested = sdp->sd_max_height + RES_DATA;
832
833 error = gfs2_inplace_reserve(ip);
834 if (error)
835 goto out_gunlock_q;
836
837 error = gfs2_trans_begin(sdp,
838 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
839 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
840 if (error)
841 goto out_ipres;
842
843 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
844 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400845 error = gfs2_unstuff_dinode(ip, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846 if (error)
847 goto out_end_trans;
848 }
849
850 h = calc_tree_height(ip, size);
851 if (ip->i_di.di_height < h) {
852 down_write(&ip->i_rw_mutex);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400853 error = build_height(&ip->i_inode, h);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854 up_write(&ip->i_rw_mutex);
855 if (error)
856 goto out_end_trans;
857 }
858 }
859
860 ip->i_di.di_size = size;
861 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
862
863 error = gfs2_meta_inode_buffer(ip, &dibh);
864 if (error)
865 goto out_end_trans;
866
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000867 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868 gfs2_dinode_out(&ip->i_di, dibh->b_data);
869 brelse(dibh);
870
871 out_end_trans:
872 gfs2_trans_end(sdp);
873
874 out_ipres:
875 gfs2_inplace_release(ip);
876
877 out_gunlock_q:
878 gfs2_quota_unlock(ip);
879
880 out:
881 gfs2_alloc_put(ip);
882
883 return error;
884}
885
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400886
887/**
888 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
889 *
890 * This is partly borrowed from ext3.
891 */
892static int gfs2_block_truncate_page(struct address_space *mapping)
893{
894 struct inode *inode = mapping->host;
895 struct gfs2_inode *ip = GFS2_I(inode);
896 struct gfs2_sbd *sdp = GFS2_SB(inode);
897 loff_t from = inode->i_size;
898 unsigned long index = from >> PAGE_CACHE_SHIFT;
899 unsigned offset = from & (PAGE_CACHE_SIZE-1);
900 unsigned blocksize, iblock, length, pos;
901 struct buffer_head *bh;
902 struct page *page;
903 void *kaddr;
904 int err;
905
906 page = grab_cache_page(mapping, index);
907 if (!page)
908 return 0;
909
910 blocksize = inode->i_sb->s_blocksize;
911 length = blocksize - (offset & (blocksize - 1));
912 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
913
914 if (!page_has_buffers(page))
915 create_empty_buffers(page, blocksize, 0);
916
917 /* Find the buffer that contains "offset" */
918 bh = page_buffers(page);
919 pos = blocksize;
920 while (offset >= pos) {
921 bh = bh->b_this_page;
922 iblock++;
923 pos += blocksize;
924 }
925
926 err = 0;
927
928 if (!buffer_mapped(bh)) {
929 gfs2_get_block(inode, iblock, bh, 0);
930 /* unmapped? It's a hole - nothing to do */
931 if (!buffer_mapped(bh))
932 goto unlock;
933 }
934
935 /* Ok, it's mapped. Make sure it's up-to-date */
936 if (PageUptodate(page))
937 set_buffer_uptodate(bh);
938
939 if (!buffer_uptodate(bh)) {
940 err = -EIO;
941 ll_rw_block(READ, 1, &bh);
942 wait_on_buffer(bh);
943 /* Uhhuh. Read error. Complain and punt. */
944 if (!buffer_uptodate(bh))
945 goto unlock;
946 }
947
948 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
949 gfs2_trans_add_bh(ip->i_gl, bh, 0);
950
951 kaddr = kmap_atomic(page, KM_USER0);
952 memset(kaddr + offset, 0, length);
953 flush_dcache_page(page);
954 kunmap_atomic(kaddr, KM_USER0);
955
956unlock:
957 unlock_page(page);
958 page_cache_release(page);
959 return err;
960}
961
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000962static int trunc_start(struct gfs2_inode *ip, uint64_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400964 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965 struct buffer_head *dibh;
966 int journaled = gfs2_is_jdata(ip);
967 int error;
968
969 error = gfs2_trans_begin(sdp,
970 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
971 if (error)
972 return error;
973
974 error = gfs2_meta_inode_buffer(ip, &dibh);
975 if (error)
976 goto out;
977
978 if (gfs2_is_stuffed(ip)) {
979 ip->i_di.di_size = size;
980 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000981 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982 gfs2_dinode_out(&ip->i_di, dibh->b_data);
983 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
984 error = 1;
985
986 } else {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000987 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400988 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989
990 if (!error) {
991 ip->i_di.di_size = size;
992 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
993 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000994 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000995 gfs2_dinode_out(&ip->i_di, dibh->b_data);
996 }
997 }
998
999 brelse(dibh);
1000
1001 out:
1002 gfs2_trans_end(sdp);
1003
1004 return error;
1005}
1006
1007static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
1008{
1009 unsigned int height = ip->i_di.di_height;
1010 uint64_t lblock;
1011 struct metapath mp;
1012 int error;
1013
1014 if (!size)
1015 lblock = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001016 else
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001017 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001018
1019 find_metapath(ip, lblock, &mp);
1020 gfs2_alloc_get(ip);
1021
1022 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1023 if (error)
1024 goto out;
1025
1026 while (height--) {
1027 struct strip_mine sm;
1028 sm.sm_first = !!size;
1029 sm.sm_height = height;
1030
1031 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1032 if (error)
1033 break;
1034 }
1035
1036 gfs2_quota_unhold(ip);
1037
1038 out:
1039 gfs2_alloc_put(ip);
1040 return error;
1041}
1042
1043static int trunc_end(struct gfs2_inode *ip)
1044{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001045 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046 struct buffer_head *dibh;
1047 int error;
1048
1049 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1050 if (error)
1051 return error;
1052
1053 down_write(&ip->i_rw_mutex);
1054
1055 error = gfs2_meta_inode_buffer(ip, &dibh);
1056 if (error)
1057 goto out;
1058
1059 if (!ip->i_di.di_size) {
1060 ip->i_di.di_height = 0;
1061 ip->i_di.di_goal_meta =
1062 ip->i_di.di_goal_data =
1063 ip->i_num.no_addr;
1064 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1065 }
1066 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1067 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1068
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001069 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1071 brelse(dibh);
1072
1073 out:
1074 up_write(&ip->i_rw_mutex);
1075
1076 gfs2_trans_end(sdp);
1077
1078 return error;
1079}
1080
1081/**
1082 * do_shrink - make a file smaller
1083 * @ip: the inode
1084 * @size: the size to make the file
1085 * @truncator: function to truncate the last partial block
1086 *
1087 * Called with an exclusive lock on @ip.
1088 *
1089 * Returns: errno
1090 */
1091
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001092static int do_shrink(struct gfs2_inode *ip, uint64_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093{
1094 int error;
1095
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001096 error = trunc_start(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 if (error < 0)
1098 return error;
1099 if (error > 0)
1100 return 0;
1101
1102 error = trunc_dealloc(ip, size);
1103 if (!error)
1104 error = trunc_end(ip);
1105
1106 return error;
1107}
1108
1109/**
Steven Whitehouse666a2c52006-01-18 10:29:04 +00001110 * gfs2_truncatei - make a file a given size
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111 * @ip: the inode
1112 * @size: the size to make the file
1113 * @truncator: function to truncate the last partial block
1114 *
1115 * The file size can grow, shrink, or stay the same size.
1116 *
1117 * Returns: errno
1118 */
1119
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001120int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001121{
1122 int error;
1123
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001124 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_di.di_mode)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125 return -EINVAL;
1126
1127 if (size > ip->i_di.di_size)
1128 error = do_grow(ip, size);
1129 else
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001130 error = do_shrink(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001131
1132 return error;
1133}
1134
1135int gfs2_truncatei_resume(struct gfs2_inode *ip)
1136{
1137 int error;
1138 error = trunc_dealloc(ip, ip->i_di.di_size);
1139 if (!error)
1140 error = trunc_end(ip);
1141 return error;
1142}
1143
1144int gfs2_file_dealloc(struct gfs2_inode *ip)
1145{
1146 return trunc_dealloc(ip, 0);
1147}
1148
1149/**
1150 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1151 * @ip: the file
1152 * @len: the number of bytes to be written to the file
1153 * @data_blocks: returns the number of data blocks required
1154 * @ind_blocks: returns the number of indirect blocks required
1155 *
1156 */
1157
1158void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1159 unsigned int *data_blocks, unsigned int *ind_blocks)
1160{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001161 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162 unsigned int tmp;
1163
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001164 if (gfs2_is_dir(ip)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001165 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1167 } else {
1168 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1169 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1170 }
1171
1172 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001173 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001174 *ind_blocks += tmp;
1175 }
1176}
1177
1178/**
1179 * gfs2_write_alloc_required - figure out if a write will require an allocation
1180 * @ip: the file being written to
1181 * @offset: the offset to write to
1182 * @len: the number of bytes being written
1183 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1184 *
1185 * Returns: errno
1186 */
1187
1188int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
1189 unsigned int len, int *alloc_required)
1190{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001191 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001192 uint64_t lblock, lblock_stop, dblock;
1193 uint32_t extlen;
1194 int new = 0;
1195 int error = 0;
1196
1197 *alloc_required = 0;
1198
1199 if (!len)
1200 return 0;
1201
1202 if (gfs2_is_stuffed(ip)) {
1203 if (offset + len >
1204 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1205 *alloc_required = 1;
1206 return 0;
1207 }
1208
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001209 if (gfs2_is_dir(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001210 unsigned int bsize = sdp->sd_jbsize;
1211 lblock = offset;
1212 do_div(lblock, bsize);
1213 lblock_stop = offset + len + bsize - 1;
1214 do_div(lblock_stop, bsize);
1215 } else {
1216 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1217 lblock = offset >> shift;
1218 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1219 }
1220
1221 for (; lblock < lblock_stop; lblock += extlen) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001222 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001223 if (error)
1224 return error;
1225
1226 if (!dblock) {
1227 *alloc_required = 1;
1228 return 0;
1229 }
1230 }
1231
1232 return 0;
1233}
1234