blob: 92eef825167d5bd5b4b4464c844b9d17c23ad463 [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>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020017#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000018
19#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#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,
Steven Whitehousecd915492006-09-04 12:49:07 -040041 struct buffer_head *bh, u64 *top,
42 u64 *bottom, unsigned int height,
David Teiglandb3b94fa2006-01-16 16:50:04 +000043 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,
Steven Whitehousecd915492006-09-04 12:49:07 -040061 u64 block, struct page *page)
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040062{
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;
Steven Whitehousecd915492006-09-04 12:49:07 -0400124 u64 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;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400133
David Teiglandb3b94fa2006-01-16 16:50:04 +0000134 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 Whitehousecd915492006-09-04 12:49:07 -0400164 *(u64 *)(dibh->b_data + sizeof(struct gfs2_dinode)) =
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500165 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
Steven Whitehousea91ea692006-09-04 12:04:26 -0400173out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400175out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 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
Steven Whitehousecd915492006-09-04 12:49:07 -0400193static unsigned int calc_tree_height(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400195 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400196 u64 *arr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197 unsigned int max, height;
198
199 if (ip->i_di.di_size > size)
200 size = ip->i_di.di_size;
201
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000202 if (gfs2_is_dir(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203 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 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222 *
223 * Returns: errno
224 */
225
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400226static int build_height(struct inode *inode, unsigned height)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400228 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400229 unsigned new_height = height - ip->i_di.di_height;
230 struct buffer_head *dibh;
231 struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232 int error;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400233 u64 *bp;
234 u64 bn;
235 unsigned n;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400237 if (height <= ip->i_di.di_height)
238 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400240 error = gfs2_meta_inode_buffer(ip, &dibh);
241 if (error)
242 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400244 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);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000248 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400249
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400250 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,
255 GFS2_FORMAT_IN);
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;
263 }
264 }
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;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278}
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
Steven Whitehousecd915492006-09-04 12:49:07 -0400339static void find_metapath(struct gfs2_inode *ip, u64 block,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500340 struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400342 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400343 u64 b = block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344 unsigned int i;
345
346 for (i = ip->i_di.di_height; i--;)
Steven Whitehousec2668712006-09-04 13:55:48 -0400347 mp->mp_list[i] = do_div(b, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348
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
Steven Whitehousefd88de562006-05-05 16:59:11 -0400362static inline u64 *metapointer(struct buffer_head *bh, int *boundary,
363 unsigned int height, const struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000364{
365 unsigned int head_size = (height > 0) ?
366 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400367 u64 *ptr;
368 *boundary = 0;
369 ptr = ((u64 *)(bh->b_data + head_size)) + mp->mp_list[height];
Steven Whitehouse75d3b812006-09-04 11:41:31 -0400370 if (ptr + 1 == (u64 *)(bh->b_data + bh->b_size))
Steven Whitehousefd88de562006-05-05 16:59:11 -0400371 *boundary = 1;
372 return ptr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373}
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
Steven Whitehousefd88de562006-05-05 16:59:11 -0400391static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
392 unsigned int height, struct metapath *mp, int create,
Steven Whitehousecd915492006-09-04 12:49:07 -0400393 int *new, u64 *block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400395 int boundary;
Steven Whitehousecd915492006-09-04 12:49:07 -0400396 u64 *ptr = metapointer(bh, &boundary, height, mp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397
398 if (*ptr) {
399 *block = be64_to_cpu(*ptr);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400400 return boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000401 }
402
403 *block = 0;
404
405 if (!create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400406 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407
Steven Whitehousefd88de562006-05-05 16:59:11 -0400408 if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409 *block = gfs2_alloc_data(ip);
410 else
411 *block = gfs2_alloc_meta(ip);
412
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000413 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414
415 *ptr = cpu_to_be64(*block);
416 ip->i_di.di_blocks++;
417
418 *new = 1;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400419 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420}
421
422/**
Steven Whitehousefd88de562006-05-05 16:59:11 -0400423 * gfs2_block_pointers - Map a block from an inode to a disk block
424 * @inode: The inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 * @lblock: The logical block number
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400426 * @map_bh: The bh to be mapped
Steven Whitehousefd88de562006-05-05 16:59:11 -0400427 * @mp: metapath to use
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428 *
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
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400435static int gfs2_block_pointers(struct inode *inode, u64 lblock, int create,
436 struct buffer_head *bh_map, struct metapath *mp,
437 unsigned int maxlen)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400439 struct gfs2_inode *ip = GFS2_I(inode);
440 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 unsigned int bsize;
443 unsigned int height;
444 unsigned int end_of_metadata;
445 unsigned int x;
446 int error = 0;
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400447 int new = 0;
448 u64 dblock = 0;
449 int boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000450
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400451 BUG_ON(maxlen == 0);
452
David Teiglandb3b94fa2006-01-16 16:50:04 +0000453 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400454 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455
Steven Whitehouse75d3b812006-09-04 11:41:31 -0400456 bsize = gfs2_is_dir(ip) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457
458 height = calc_tree_height(ip, (lblock + 1) * bsize);
459 if (ip->i_di.di_height < height) {
460 if (!create)
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400461 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400463 error = build_height(inode, height);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464 if (error)
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400465 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000466 }
467
Steven Whitehousefd88de562006-05-05 16:59:11 -0400468 find_metapath(ip, lblock, mp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 end_of_metadata = ip->i_di.di_height - 1;
470
471 error = gfs2_meta_inode_buffer(ip, &bh);
472 if (error)
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400473 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474
475 for (x = 0; x < end_of_metadata; x++) {
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400476 lookup_block(ip, bh, x, mp, create, &new, &dblock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477 brelse(bh);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400478 if (!dblock)
479 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400481 error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482 if (error)
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400483 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484 }
485
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400486 boundary = lookup_block(ip, bh, end_of_metadata, mp, create, &new, &dblock);
487 clear_buffer_mapped(bh_map);
488 clear_buffer_new(bh_map);
489 clear_buffer_boundary(bh_map);
490
491 if (dblock) {
492 map_bh(bh_map, inode->i_sb, dblock);
493 if (boundary)
494 set_buffer_boundary(bh);
495 if (new) {
496 struct buffer_head *dibh;
497 error = gfs2_meta_inode_buffer(ip, &dibh);
498 if (!error) {
499 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
500 gfs2_dinode_out(&ip->i_di, dibh->b_data);
501 brelse(dibh);
502 }
503 set_buffer_new(bh_map);
504 goto out_brelse;
505 }
506 while(--maxlen && !buffer_boundary(bh_map)) {
507 u64 eblock;
508
509 mp->mp_list[end_of_metadata]++;
510 boundary = lookup_block(ip, bh, end_of_metadata, mp, 0, &new, &eblock);
511 if (eblock != ++dblock)
512 break;
akpm@osdl.orgf3b30912006-09-18 22:16:03 -0700513 bh_map->b_size += (1 << inode->i_blkbits);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400514 if (boundary)
515 set_buffer_boundary(bh_map);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 }
517 }
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400518out_brelse:
519 brelse(bh);
520 return 0;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400521}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522
Steven Whitehousefd88de562006-05-05 16:59:11 -0400523
524static inline void bmap_lock(struct inode *inode, int create)
525{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400526 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400527 if (create)
528 down_write(&ip->i_rw_mutex);
529 else
530 down_read(&ip->i_rw_mutex);
531}
532
533static inline void bmap_unlock(struct inode *inode, int create)
534{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400535 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 if (create)
537 up_write(&ip->i_rw_mutex);
538 else
539 up_read(&ip->i_rw_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400540}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400542int gfs2_block_map(struct inode *inode, u64 lblock, int create,
543 struct buffer_head *bh, unsigned int maxlen)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400544{
545 struct metapath mp;
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400546 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400547
548 bmap_lock(inode, create);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400549 ret = gfs2_block_pointers(inode, lblock, create, bh, &mp, maxlen);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400550 bmap_unlock(inode, create);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400551 return ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400552}
553
554int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
555{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400556 struct metapath mp;
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400557 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0, .b_size = 0 };
558 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400559 int create = *new;
560
561 BUG_ON(!extlen);
562 BUG_ON(!dblock);
563 BUG_ON(!new);
564
565 bmap_lock(inode, create);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400566 ret = gfs2_block_pointers(inode, lblock, create, &bh, &mp, 32);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400567 bmap_unlock(inode, create);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400568 *extlen = bh.b_size >> inode->i_blkbits;
569 *dblock = bh.b_blocknr;
570 if (buffer_new(&bh))
571 *new = 1;
572 else
573 *new = 0;
574 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575}
576
577/**
578 * recursive_scan - recursively scan through the end of a file
579 * @ip: the inode
580 * @dibh: the dinode buffer
581 * @mp: the path through the metadata to the point to start
582 * @height: the height the recursion is at
583 * @block: the indirect block to look at
584 * @first: 1 if this is the first block
585 * @bc: the call to make for each piece of metadata
586 * @data: data opaque to this function to pass to @bc
587 *
588 * When this is first called @height and @block should be zero and
589 * @first should be 1.
590 *
591 * Returns: errno
592 */
593
594static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
595 struct metapath *mp, unsigned int height,
Steven Whitehousecd915492006-09-04 12:49:07 -0400596 u64 block, int first, block_call_t bc,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 void *data)
598{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400599 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600 struct buffer_head *bh = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -0400601 u64 *top, *bottom;
602 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603 int error;
604 int mh_size = sizeof(struct gfs2_meta_header);
605
606 if (!height) {
607 error = gfs2_meta_inode_buffer(ip, &bh);
608 if (error)
609 return error;
610 dibh = bh;
611
Steven Whitehouse75d3b812006-09-04 11:41:31 -0400612 top = (u64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
613 bottom = (u64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000614 } else {
615 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
616 if (error)
617 return error;
618
Steven Whitehousecd915492006-09-04 12:49:07 -0400619 top = (u64 *)(bh->b_data + mh_size) +
Jan Engelhardtc53921242006-09-05 14:30:40 +0200620 (first ? mp->mp_list[height] : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621
Steven Whitehousecd915492006-09-04 12:49:07 -0400622 bottom = (u64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623 }
624
625 error = bc(ip, dibh, bh, top, bottom, height, data);
626 if (error)
627 goto out;
628
629 if (height < ip->i_di.di_height - 1)
630 for (; top < bottom; top++, first = 0) {
631 if (!*top)
632 continue;
633
634 bn = be64_to_cpu(*top);
635
636 error = recursive_scan(ip, dibh, mp, height + 1, bn,
637 first, bc, data);
638 if (error)
639 break;
640 }
641
Steven Whitehousea91ea692006-09-04 12:04:26 -0400642out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644 return error;
645}
646
647/**
648 * do_strip - Look for a layer a particular layer of the file and strip it off
649 * @ip: the inode
650 * @dibh: the dinode buffer
651 * @bh: A buffer of pointers
652 * @top: The first pointer in the buffer
653 * @bottom: One more than the last pointer
654 * @height: the height this buffer is at
655 * @data: a pointer to a struct strip_mine
656 *
657 * Returns: errno
658 */
659
660static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
Steven Whitehousecd915492006-09-04 12:49:07 -0400661 struct buffer_head *bh, u64 *top, u64 *bottom,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662 unsigned int height, void *data)
663{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400664 struct strip_mine *sm = data;
665 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666 struct gfs2_rgrp_list rlist;
Steven Whitehousecd915492006-09-04 12:49:07 -0400667 u64 bn, bstart;
668 u32 blen;
669 u64 *p;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 unsigned int rg_blocks = 0;
671 int metadata;
672 unsigned int revokes = 0;
673 int x;
674 int error;
675
676 if (!*top)
677 sm->sm_first = 0;
678
679 if (height != sm->sm_height)
680 return 0;
681
682 if (sm->sm_first) {
683 top++;
684 sm->sm_first = 0;
685 }
686
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000687 metadata = (height != ip->i_di.di_height - 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688 if (metadata)
689 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
690
691 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
692 if (error)
693 return error;
694
695 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
696 bstart = 0;
697 blen = 0;
698
699 for (p = top; p < bottom; p++) {
700 if (!*p)
701 continue;
702
703 bn = be64_to_cpu(*p);
704
705 if (bstart + blen == bn)
706 blen++;
707 else {
708 if (bstart)
709 gfs2_rlist_add(sdp, &rlist, bstart);
710
711 bstart = bn;
712 blen = 1;
713 }
714 }
715
716 if (bstart)
717 gfs2_rlist_add(sdp, &rlist, bstart);
718 else
719 goto out; /* Nothing to do */
720
721 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
722
723 for (x = 0; x < rlist.rl_rgrps; x++) {
724 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500725 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 rg_blocks += rgd->rd_ri.ri_length;
727 }
728
729 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
730 if (error)
731 goto out_rlist;
732
733 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
734 RES_INDIRECT + RES_STATFS + RES_QUOTA,
735 revokes);
736 if (error)
737 goto out_rg_gunlock;
738
739 down_write(&ip->i_rw_mutex);
740
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000741 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
742 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743
744 bstart = 0;
745 blen = 0;
746
747 for (p = top; p < bottom; p++) {
748 if (!*p)
749 continue;
750
751 bn = be64_to_cpu(*p);
752
753 if (bstart + blen == bn)
754 blen++;
755 else {
756 if (bstart) {
757 if (metadata)
758 gfs2_free_meta(ip, bstart, blen);
759 else
760 gfs2_free_data(ip, bstart, blen);
761 }
762
763 bstart = bn;
764 blen = 1;
765 }
766
767 *p = 0;
768 if (!ip->i_di.di_blocks)
769 gfs2_consist_inode(ip);
770 ip->i_di.di_blocks--;
771 }
772 if (bstart) {
773 if (metadata)
774 gfs2_free_meta(ip, bstart, blen);
775 else
776 gfs2_free_data(ip, bstart, blen);
777 }
778
779 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
780
781 gfs2_dinode_out(&ip->i_di, dibh->b_data);
782
783 up_write(&ip->i_rw_mutex);
784
785 gfs2_trans_end(sdp);
786
Steven Whitehousea91ea692006-09-04 12:04:26 -0400787out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400789out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 gfs2_rlist_free(&rlist);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400791out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 return error;
794}
795
796/**
797 * do_grow - Make a file look bigger than it is
798 * @ip: the inode
799 * @size: the size to set the file to
800 *
801 * Called with an exclusive lock on @ip.
802 *
803 * Returns: errno
804 */
805
Steven Whitehousecd915492006-09-04 12:49:07 -0400806static int do_grow(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400808 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 struct gfs2_alloc *al;
810 struct buffer_head *dibh;
811 unsigned int h;
812 int error;
813
814 al = gfs2_alloc_get(ip);
815
816 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
817 if (error)
818 goto out;
819
820 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
821 if (error)
822 goto out_gunlock_q;
823
824 al->al_requested = sdp->sd_max_height + RES_DATA;
825
826 error = gfs2_inplace_reserve(ip);
827 if (error)
828 goto out_gunlock_q;
829
830 error = gfs2_trans_begin(sdp,
831 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
832 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
833 if (error)
834 goto out_ipres;
835
836 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
837 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400838 error = gfs2_unstuff_dinode(ip, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839 if (error)
840 goto out_end_trans;
841 }
842
843 h = calc_tree_height(ip, size);
844 if (ip->i_di.di_height < h) {
845 down_write(&ip->i_rw_mutex);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400846 error = build_height(&ip->i_inode, h);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000847 up_write(&ip->i_rw_mutex);
848 if (error)
849 goto out_end_trans;
850 }
851 }
852
853 ip->i_di.di_size = size;
854 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
855
856 error = gfs2_meta_inode_buffer(ip, &dibh);
857 if (error)
858 goto out_end_trans;
859
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000860 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 gfs2_dinode_out(&ip->i_di, dibh->b_data);
862 brelse(dibh);
863
Steven Whitehousea91ea692006-09-04 12:04:26 -0400864out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000865 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400866out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400868out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400870out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 return error;
873}
874
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400875
876/**
877 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
878 *
879 * This is partly borrowed from ext3.
880 */
881static int gfs2_block_truncate_page(struct address_space *mapping)
882{
883 struct inode *inode = mapping->host;
884 struct gfs2_inode *ip = GFS2_I(inode);
885 struct gfs2_sbd *sdp = GFS2_SB(inode);
886 loff_t from = inode->i_size;
887 unsigned long index = from >> PAGE_CACHE_SHIFT;
888 unsigned offset = from & (PAGE_CACHE_SIZE-1);
889 unsigned blocksize, iblock, length, pos;
890 struct buffer_head *bh;
891 struct page *page;
892 void *kaddr;
893 int err;
894
895 page = grab_cache_page(mapping, index);
896 if (!page)
897 return 0;
898
899 blocksize = inode->i_sb->s_blocksize;
900 length = blocksize - (offset & (blocksize - 1));
901 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
902
903 if (!page_has_buffers(page))
904 create_empty_buffers(page, blocksize, 0);
905
906 /* Find the buffer that contains "offset" */
907 bh = page_buffers(page);
908 pos = blocksize;
909 while (offset >= pos) {
910 bh = bh->b_this_page;
911 iblock++;
912 pos += blocksize;
913 }
914
915 err = 0;
916
917 if (!buffer_mapped(bh)) {
918 gfs2_get_block(inode, iblock, bh, 0);
919 /* unmapped? It's a hole - nothing to do */
920 if (!buffer_mapped(bh))
921 goto unlock;
922 }
923
924 /* Ok, it's mapped. Make sure it's up-to-date */
925 if (PageUptodate(page))
926 set_buffer_uptodate(bh);
927
928 if (!buffer_uptodate(bh)) {
929 err = -EIO;
930 ll_rw_block(READ, 1, &bh);
931 wait_on_buffer(bh);
932 /* Uhhuh. Read error. Complain and punt. */
933 if (!buffer_uptodate(bh))
934 goto unlock;
935 }
936
937 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
938 gfs2_trans_add_bh(ip->i_gl, bh, 0);
939
940 kaddr = kmap_atomic(page, KM_USER0);
941 memset(kaddr + offset, 0, length);
942 flush_dcache_page(page);
943 kunmap_atomic(kaddr, KM_USER0);
944
945unlock:
946 unlock_page(page);
947 page_cache_release(page);
948 return err;
949}
950
Steven Whitehousecd915492006-09-04 12:49:07 -0400951static int trunc_start(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400953 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 struct buffer_head *dibh;
955 int journaled = gfs2_is_jdata(ip);
956 int error;
957
958 error = gfs2_trans_begin(sdp,
Jan Engelhardtc53921242006-09-05 14:30:40 +0200959 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 if (error)
961 return error;
962
963 error = gfs2_meta_inode_buffer(ip, &dibh);
964 if (error)
965 goto out;
966
967 if (gfs2_is_stuffed(ip)) {
968 ip->i_di.di_size = size;
969 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000970 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 gfs2_dinode_out(&ip->i_di, dibh->b_data);
972 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
973 error = 1;
974
975 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -0400976 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400977 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978
979 if (!error) {
980 ip->i_di.di_size = size;
981 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
982 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000983 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984 gfs2_dinode_out(&ip->i_di, dibh->b_data);
985 }
986 }
987
988 brelse(dibh);
989
Steven Whitehousea91ea692006-09-04 12:04:26 -0400990out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000991 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992 return error;
993}
994
Steven Whitehousecd915492006-09-04 12:49:07 -0400995static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996{
997 unsigned int height = ip->i_di.di_height;
Steven Whitehousecd915492006-09-04 12:49:07 -0400998 u64 lblock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 struct metapath mp;
1000 int error;
1001
1002 if (!size)
1003 lblock = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001004 else
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001005 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001006
1007 find_metapath(ip, lblock, &mp);
1008 gfs2_alloc_get(ip);
1009
1010 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1011 if (error)
1012 goto out;
1013
1014 while (height--) {
1015 struct strip_mine sm;
1016 sm.sm_first = !!size;
1017 sm.sm_height = height;
1018
1019 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1020 if (error)
1021 break;
1022 }
1023
1024 gfs2_quota_unhold(ip);
1025
Steven Whitehousea91ea692006-09-04 12:04:26 -04001026out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 gfs2_alloc_put(ip);
1028 return error;
1029}
1030
1031static int trunc_end(struct gfs2_inode *ip)
1032{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001033 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034 struct buffer_head *dibh;
1035 int error;
1036
1037 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1038 if (error)
1039 return error;
1040
1041 down_write(&ip->i_rw_mutex);
1042
1043 error = gfs2_meta_inode_buffer(ip, &dibh);
1044 if (error)
1045 goto out;
1046
1047 if (!ip->i_di.di_size) {
1048 ip->i_di.di_height = 0;
1049 ip->i_di.di_goal_meta =
1050 ip->i_di.di_goal_data =
1051 ip->i_num.no_addr;
1052 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1053 }
1054 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1055 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1056
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001057 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1059 brelse(dibh);
1060
Steven Whitehousea91ea692006-09-04 12:04:26 -04001061out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001064 return error;
1065}
1066
1067/**
1068 * do_shrink - make a file smaller
1069 * @ip: the inode
1070 * @size: the size to make the file
1071 * @truncator: function to truncate the last partial block
1072 *
1073 * Called with an exclusive lock on @ip.
1074 *
1075 * Returns: errno
1076 */
1077
Steven Whitehousecd915492006-09-04 12:49:07 -04001078static int do_shrink(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079{
1080 int error;
1081
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001082 error = trunc_start(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001083 if (error < 0)
1084 return error;
1085 if (error > 0)
1086 return 0;
1087
1088 error = trunc_dealloc(ip, size);
1089 if (!error)
1090 error = trunc_end(ip);
1091
1092 return error;
1093}
1094
1095/**
Steven Whitehouse666a2c52006-01-18 10:29:04 +00001096 * gfs2_truncatei - make a file a given size
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 * @ip: the inode
1098 * @size: the size to make the file
1099 * @truncator: function to truncate the last partial block
1100 *
1101 * The file size can grow, shrink, or stay the same size.
1102 *
1103 * Returns: errno
1104 */
1105
Steven Whitehousecd915492006-09-04 12:49:07 -04001106int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107{
1108 int error;
1109
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001110 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_di.di_mode)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111 return -EINVAL;
1112
1113 if (size > ip->i_di.di_size)
1114 error = do_grow(ip, size);
1115 else
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001116 error = do_shrink(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001117
1118 return error;
1119}
1120
1121int gfs2_truncatei_resume(struct gfs2_inode *ip)
1122{
1123 int error;
1124 error = trunc_dealloc(ip, ip->i_di.di_size);
1125 if (!error)
1126 error = trunc_end(ip);
1127 return error;
1128}
1129
1130int gfs2_file_dealloc(struct gfs2_inode *ip)
1131{
1132 return trunc_dealloc(ip, 0);
1133}
1134
1135/**
1136 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1137 * @ip: the file
1138 * @len: the number of bytes to be written to the file
1139 * @data_blocks: returns the number of data blocks required
1140 * @ind_blocks: returns the number of indirect blocks required
1141 *
1142 */
1143
1144void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1145 unsigned int *data_blocks, unsigned int *ind_blocks)
1146{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001147 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001148 unsigned int tmp;
1149
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001150 if (gfs2_is_dir(ip)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001151 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1153 } else {
1154 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1155 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1156 }
1157
1158 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001159 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160 *ind_blocks += tmp;
1161 }
1162}
1163
1164/**
1165 * gfs2_write_alloc_required - figure out if a write will require an allocation
1166 * @ip: the file being written to
1167 * @offset: the offset to write to
1168 * @len: the number of bytes being written
1169 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1170 *
1171 * Returns: errno
1172 */
1173
Steven Whitehousecd915492006-09-04 12:49:07 -04001174int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001175 unsigned int len, int *alloc_required)
1176{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001177 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001178 u64 lblock, lblock_stop, dblock;
1179 u32 extlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180 int new = 0;
1181 int error = 0;
1182
1183 *alloc_required = 0;
1184
1185 if (!len)
1186 return 0;
1187
1188 if (gfs2_is_stuffed(ip)) {
1189 if (offset + len >
1190 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1191 *alloc_required = 1;
1192 return 0;
1193 }
1194
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001195 if (gfs2_is_dir(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001196 unsigned int bsize = sdp->sd_jbsize;
1197 lblock = offset;
1198 do_div(lblock, bsize);
1199 lblock_stop = offset + len + bsize - 1;
1200 do_div(lblock_stop, bsize);
1201 } else {
1202 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1203 lblock = offset >> shift;
1204 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1205 }
1206
1207 for (; lblock < lblock_stop; lblock += extlen) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001208 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 if (error)
1210 return error;
1211
1212 if (!dblock) {
1213 *alloc_required = 1;
1214 return 0;
1215 }
1216 }
1217
1218 return 0;
1219}
1220