blob: 93fa427bb5f501cee6da6ab88afbfd921f584295 [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
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050014#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050015#include <linux/crc32.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020016#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000020#include "bmap.h"
21#include "glock.h"
22#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "quota.h"
25#include "rgrp.h"
26#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000027#include "dir.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
Steven Whitehouseba7f7292006-07-26 11:27:10 -040029#include "ops_address.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030
31/* This doesn't need to be that large as max 64 bit pointers in a 4k
32 * block is 512, so __u16 is fine for that. It saves stack space to
33 * keep it small.
34 */
35struct metapath {
36 __u16 mp_list[GFS2_MAX_META_HEIGHT];
37};
38
39typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
Al Virob44b84d2006-10-14 10:46:30 -040040 struct buffer_head *bh, __be64 *top,
41 __be64 *bottom, unsigned int height,
David Teiglandb3b94fa2006-01-16 16:50:04 +000042 void *data);
43
44struct strip_mine {
45 int sm_first;
46 unsigned int sm_height;
47};
48
49/**
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040050 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
51 * @ip: the inode
52 * @dibh: the dinode buffer
53 * @block: the block number that was allocated
54 * @private: any locked page held by the caller process
55 *
56 * Returns: errno
57 */
58
59static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
Steven Whitehousecd915492006-09-04 12:49:07 -040060 u64 block, struct page *page)
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040061{
62 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
63 struct inode *inode = &ip->i_inode;
64 struct buffer_head *bh;
65 int release = 0;
66
67 if (!page || page->index) {
68 page = grab_cache_page(inode->i_mapping, 0);
69 if (!page)
70 return -ENOMEM;
71 release = 1;
72 }
73
74 if (!PageUptodate(page)) {
75 void *kaddr = kmap(page);
76
77 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
78 ip->i_di.di_size);
79 memset(kaddr + ip->i_di.di_size, 0,
80 PAGE_CACHE_SIZE - ip->i_di.di_size);
81 kunmap(page);
82
83 SetPageUptodate(page);
84 }
85
86 if (!page_has_buffers(page))
87 create_empty_buffers(page, 1 << inode->i_blkbits,
88 (1 << BH_Uptodate));
89
90 bh = page_buffers(page);
91
92 if (!buffer_mapped(bh))
93 map_bh(bh, inode->i_sb, block);
94
95 set_buffer_uptodate(bh);
Steven Whitehouseeaf96522007-08-27 09:49:37 +010096 if (!gfs2_is_jdata(ip))
97 mark_buffer_dirty(bh);
Bob Peterson84754872007-09-02 10:55:29 +010098 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
99 gfs2_trans_add_bh(ip->i_gl, bh, 0);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400100
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 Whitehouse48516ce2006-10-02 12:39:19 -0400124 struct gfs2_dinode *di;
Steven Whitehousecd915492006-09-04 12:49:07 -0400125 u64 block = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000126 int isdir = gfs2_is_dir(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 int error;
128
129 down_write(&ip->i_rw_mutex);
130
131 error = gfs2_meta_inode_buffer(ip, &dibh);
132 if (error)
133 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400134
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135 if (ip->i_di.di_size) {
136 /* Get a free block, fill it with the stuffed data,
137 and write it out to disk */
138
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000139 if (isdir) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 block = gfs2_alloc_meta(ip);
141
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400142 error = gfs2_dir_get_new_buffer(ip, block, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000143 if (error)
144 goto out_brelse;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400145 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 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);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400160 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
162
163 if (ip->i_di.di_size) {
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400164 *(__be64 *)(di + 1) = cpu_to_be64(block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000165 ip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500166 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400167 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168 }
169
170 ip->i_di.di_height = 1;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400171 di->di_height = cpu_to_be16(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172
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];
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400232 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233 int error;
Al Virob44b84d2006-10-14 10:46:30 -0400234 __be64 *bp;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400235 u64 bn;
236 unsigned n;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000237
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400238 if (height <= ip->i_di.di_height)
239 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400241 error = gfs2_meta_inode_buffer(ip, &dibh);
242 if (error)
243 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400245 for(n = 0; n < new_height; n++) {
246 bn = gfs2_alloc_meta(ip);
247 blocks[n] = gfs2_meta_new(ip->i_gl, bn);
248 gfs2_trans_add_bh(ip->i_gl, blocks[n], 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400250
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400251 n = 0;
252 bn = blocks[0]->b_blocknr;
253 if (new_height > 1) {
254 for(; n < new_height-1; n++) {
255 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
256 GFS2_FORMAT_IN);
257 gfs2_buffer_clear_tail(blocks[n],
258 sizeof(struct gfs2_meta_header));
Al Virob44b84d2006-10-14 10:46:30 -0400259 bp = (__be64 *)(blocks[n]->b_data +
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400260 sizeof(struct gfs2_meta_header));
261 *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
262 brelse(blocks[n]);
263 blocks[n] = NULL;
264 }
265 }
266 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
267 gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
268 dibh, sizeof(struct gfs2_dinode));
269 brelse(blocks[n]);
270 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400271 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400272 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400273 *(__be64 *)(di + 1) = cpu_to_be64(bn);
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400274 ip->i_di.di_height += new_height;
275 ip->i_di.di_blocks += new_height;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500276 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400277 di->di_height = cpu_to_be16(ip->i_di.di_height);
278 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400279 brelse(dibh);
280 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281}
282
283/**
284 * find_metapath - Find path through the metadata tree
285 * @ip: The inode pointer
286 * @mp: The metapath to return the result in
287 * @block: The disk block to look up
288 *
289 * This routine returns a struct metapath structure that defines a path
290 * through the metadata of inode "ip" to get to block "block".
291 *
292 * Example:
293 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
294 * filesystem with a blocksize of 4096.
295 *
296 * find_metapath() would return a struct metapath structure set to:
297 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
298 * and mp_list[2] = 165.
299 *
300 * That means that in order to get to the block containing the byte at
301 * offset 101342453, we would load the indirect block pointed to by pointer
302 * 0 in the dinode. We would then load the indirect block pointed to by
303 * pointer 48 in that indirect block. We would then load the data block
304 * pointed to by pointer 165 in that indirect block.
305 *
306 * ----------------------------------------
307 * | Dinode | |
308 * | | 4|
309 * | |0 1 2 3 4 5 9|
310 * | | 6|
311 * ----------------------------------------
312 * |
313 * |
314 * V
315 * ----------------------------------------
316 * | Indirect Block |
317 * | 5|
318 * | 4 4 4 4 4 5 5 1|
319 * |0 5 6 7 8 9 0 1 2|
320 * ----------------------------------------
321 * |
322 * |
323 * V
324 * ----------------------------------------
325 * | Indirect Block |
326 * | 1 1 1 1 1 5|
327 * | 6 6 6 6 6 1|
328 * |0 3 4 5 6 7 2|
329 * ----------------------------------------
330 * |
331 * |
332 * V
333 * ----------------------------------------
334 * | Data block containing offset |
335 * | 101342453 |
336 * | |
337 * | |
338 * ----------------------------------------
339 *
340 */
341
Steven Whitehousecd915492006-09-04 12:49:07 -0400342static void find_metapath(struct gfs2_inode *ip, u64 block,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500343 struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400345 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400346 u64 b = block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347 unsigned int i;
348
349 for (i = ip->i_di.di_height; i--;)
Steven Whitehousec2668712006-09-04 13:55:48 -0400350 mp->mp_list[i] = do_div(b, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351
352}
353
354/**
355 * metapointer - Return pointer to start of metadata in a buffer
356 * @bh: The buffer
357 * @height: The metadata height (0 = dinode)
358 * @mp: The metapath
359 *
360 * Return a pointer to the block number of the next height of the metadata
361 * tree given a buffer containing the pointer to the current height of the
362 * metadata tree.
363 */
364
Al Virob44b84d2006-10-14 10:46:30 -0400365static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400366 unsigned int height, const struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367{
368 unsigned int head_size = (height > 0) ?
369 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
Al Virob44b84d2006-10-14 10:46:30 -0400370 __be64 *ptr;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400371 *boundary = 0;
Al Virob44b84d2006-10-14 10:46:30 -0400372 ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
373 if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
Steven Whitehousefd88de562006-05-05 16:59:11 -0400374 *boundary = 1;
375 return ptr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000376}
377
378/**
379 * lookup_block - Get the next metadata block in metadata tree
380 * @ip: The GFS2 inode
381 * @bh: Buffer containing the pointers to metadata blocks
382 * @height: The height of the tree (0 = dinode)
383 * @mp: The metapath
384 * @create: Non-zero if we may create a new meatdata block
385 * @new: Used to indicate if we did create a new metadata block
386 * @block: the returned disk block number
387 *
388 * Given a metatree, complete to a particular height, checks to see if the next
389 * height of the tree exists. If not the next height of the tree is created.
390 * The block number of the next height of the metadata tree is returned.
391 *
392 */
393
Steven Whitehousefd88de562006-05-05 16:59:11 -0400394static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
395 unsigned int height, struct metapath *mp, int create,
Steven Whitehousecd915492006-09-04 12:49:07 -0400396 int *new, u64 *block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400398 int boundary;
Al Virob44b84d2006-10-14 10:46:30 -0400399 __be64 *ptr = metapointer(bh, &boundary, height, mp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400
401 if (*ptr) {
402 *block = be64_to_cpu(*ptr);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400403 return boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404 }
405
406 *block = 0;
407
408 if (!create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400409 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000410
Steven Whitehousefd88de562006-05-05 16:59:11 -0400411 if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412 *block = gfs2_alloc_data(ip);
413 else
414 *block = gfs2_alloc_meta(ip);
415
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000416 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417
418 *ptr = cpu_to_be64(*block);
419 ip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500420 gfs2_set_inode_blocks(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421
422 *new = 1;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400423 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424}
425
Steven Whitehousefd88de562006-05-05 16:59:11 -0400426static inline void bmap_lock(struct inode *inode, int create)
427{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400428 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400429 if (create)
430 down_write(&ip->i_rw_mutex);
431 else
432 down_read(&ip->i_rw_mutex);
433}
434
435static inline void bmap_unlock(struct inode *inode, int create)
436{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400437 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438 if (create)
439 up_write(&ip->i_rw_mutex);
440 else
441 up_read(&ip->i_rw_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400442}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500444/**
445 * gfs2_block_map - Map a block from an inode to a disk block
446 * @inode: The inode
447 * @lblock: The logical block number
448 * @bh_map: The bh to be mapped
449 *
450 * Find the block number on the current device which corresponds to an
451 * inode's block. If the block had to be created, "new" will be set.
452 *
453 * Returns: errno
454 */
455
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400456int gfs2_block_map(struct inode *inode, u64 lblock, int create,
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500457 struct buffer_head *bh_map)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400458{
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500459 struct gfs2_inode *ip = GFS2_I(inode);
460 struct gfs2_sbd *sdp = GFS2_SB(inode);
461 struct buffer_head *bh;
462 unsigned int bsize;
463 unsigned int height;
464 unsigned int end_of_metadata;
465 unsigned int x;
466 int error = 0;
467 int new = 0;
468 u64 dblock = 0;
469 int boundary;
470 unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400471 struct metapath mp;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500472 u64 size;
473
474 BUG_ON(maxlen == 0);
475
476 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
477 return 0;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400478
479 bmap_lock(inode, create);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500480 clear_buffer_mapped(bh_map);
481 clear_buffer_new(bh_map);
482 clear_buffer_boundary(bh_map);
483 bsize = gfs2_is_dir(ip) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
484 size = (lblock + 1) * bsize;
485
486 if (size > ip->i_di.di_size) {
487 height = calc_tree_height(ip, size);
488 if (ip->i_di.di_height < height) {
489 if (!create)
490 goto out_ok;
491
492 error = build_height(inode, height);
493 if (error)
494 goto out_fail;
495 }
496 }
497
498 find_metapath(ip, lblock, &mp);
499 end_of_metadata = ip->i_di.di_height - 1;
500 error = gfs2_meta_inode_buffer(ip, &bh);
501 if (error)
502 goto out_fail;
503
504 for (x = 0; x < end_of_metadata; x++) {
505 lookup_block(ip, bh, x, &mp, create, &new, &dblock);
506 brelse(bh);
507 if (!dblock)
508 goto out_ok;
509
510 error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
511 if (error)
512 goto out_fail;
513 }
514
515 boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock);
516 if (dblock) {
517 map_bh(bh_map, inode->i_sb, dblock);
518 if (boundary)
519 set_buffer_boundary(bh_map);
520 if (new) {
521 struct buffer_head *dibh;
522 error = gfs2_meta_inode_buffer(ip, &dibh);
523 if (!error) {
524 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
525 gfs2_dinode_out(ip, dibh->b_data);
526 brelse(dibh);
527 }
528 set_buffer_new(bh_map);
529 goto out_brelse;
530 }
531 while(--maxlen && !buffer_boundary(bh_map)) {
532 u64 eblock;
533
534 mp.mp_list[end_of_metadata]++;
535 boundary = lookup_block(ip, bh, end_of_metadata, &mp, 0, &new, &eblock);
536 if (eblock != ++dblock)
537 break;
538 bh_map->b_size += (1 << inode->i_blkbits);
539 if (boundary)
540 set_buffer_boundary(bh_map);
541 }
542 }
543out_brelse:
544 brelse(bh);
545out_ok:
546 error = 0;
547out_fail:
Steven Whitehousefd88de562006-05-05 16:59:11 -0400548 bmap_unlock(inode, create);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500549 return error;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400550}
551
552int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
553{
Steven Whitehouse23591252006-10-13 17:25:45 -0400554 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400555 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400556 int create = *new;
557
558 BUG_ON(!extlen);
559 BUG_ON(!dblock);
560 BUG_ON(!new);
561
Steven Whitehouse23591252006-10-13 17:25:45 -0400562 bh.b_size = 1 << (inode->i_blkbits + 5);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500563 ret = gfs2_block_map(inode, lblock, create, &bh);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400564 *extlen = bh.b_size >> inode->i_blkbits;
565 *dblock = bh.b_blocknr;
566 if (buffer_new(&bh))
567 *new = 1;
568 else
569 *new = 0;
570 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571}
572
573/**
574 * recursive_scan - recursively scan through the end of a file
575 * @ip: the inode
576 * @dibh: the dinode buffer
577 * @mp: the path through the metadata to the point to start
578 * @height: the height the recursion is at
579 * @block: the indirect block to look at
580 * @first: 1 if this is the first block
581 * @bc: the call to make for each piece of metadata
582 * @data: data opaque to this function to pass to @bc
583 *
584 * When this is first called @height and @block should be zero and
585 * @first should be 1.
586 *
587 * Returns: errno
588 */
589
590static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
591 struct metapath *mp, unsigned int height,
Steven Whitehousecd915492006-09-04 12:49:07 -0400592 u64 block, int first, block_call_t bc,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593 void *data)
594{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400595 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 struct buffer_head *bh = NULL;
Al Virob44b84d2006-10-14 10:46:30 -0400597 __be64 *top, *bottom;
Steven Whitehousecd915492006-09-04 12:49:07 -0400598 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599 int error;
600 int mh_size = sizeof(struct gfs2_meta_header);
601
602 if (!height) {
603 error = gfs2_meta_inode_buffer(ip, &bh);
604 if (error)
605 return error;
606 dibh = bh;
607
Al Virob44b84d2006-10-14 10:46:30 -0400608 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
609 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 } else {
611 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
612 if (error)
613 return error;
614
Al Virob44b84d2006-10-14 10:46:30 -0400615 top = (__be64 *)(bh->b_data + mh_size) +
Jan Engelhardtc5392122006-09-05 14:30:40 +0200616 (first ? mp->mp_list[height] : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617
Al Virob44b84d2006-10-14 10:46:30 -0400618 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619 }
620
621 error = bc(ip, dibh, bh, top, bottom, height, data);
622 if (error)
623 goto out;
624
625 if (height < ip->i_di.di_height - 1)
626 for (; top < bottom; top++, first = 0) {
627 if (!*top)
628 continue;
629
630 bn = be64_to_cpu(*top);
631
632 error = recursive_scan(ip, dibh, mp, height + 1, bn,
633 first, bc, data);
634 if (error)
635 break;
636 }
637
Steven Whitehousea91ea692006-09-04 12:04:26 -0400638out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640 return error;
641}
642
643/**
644 * do_strip - Look for a layer a particular layer of the file and strip it off
645 * @ip: the inode
646 * @dibh: the dinode buffer
647 * @bh: A buffer of pointers
648 * @top: The first pointer in the buffer
649 * @bottom: One more than the last pointer
650 * @height: the height this buffer is at
651 * @data: a pointer to a struct strip_mine
652 *
653 * Returns: errno
654 */
655
656static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
Al Virob44b84d2006-10-14 10:46:30 -0400657 struct buffer_head *bh, __be64 *top, __be64 *bottom,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 unsigned int height, void *data)
659{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400660 struct strip_mine *sm = data;
661 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662 struct gfs2_rgrp_list rlist;
Steven Whitehousecd915492006-09-04 12:49:07 -0400663 u64 bn, bstart;
664 u32 blen;
Al Virob44b84d2006-10-14 10:46:30 -0400665 __be64 *p;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666 unsigned int rg_blocks = 0;
667 int metadata;
668 unsigned int revokes = 0;
669 int x;
670 int error;
671
672 if (!*top)
673 sm->sm_first = 0;
674
675 if (height != sm->sm_height)
676 return 0;
677
678 if (sm->sm_first) {
679 top++;
680 sm->sm_first = 0;
681 }
682
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000683 metadata = (height != ip->i_di.di_height - 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000684 if (metadata)
685 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
686
687 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
688 if (error)
689 return error;
690
691 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
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 gfs2_rlist_add(sdp, &rlist, bstart);
706
707 bstart = bn;
708 blen = 1;
709 }
710 }
711
712 if (bstart)
713 gfs2_rlist_add(sdp, &rlist, bstart);
714 else
715 goto out; /* Nothing to do */
716
717 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
718
719 for (x = 0; x < rlist.rl_rgrps; x++) {
720 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500721 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100722 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 }
724
725 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
726 if (error)
727 goto out_rlist;
728
729 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
730 RES_INDIRECT + RES_STATFS + RES_QUOTA,
731 revokes);
732 if (error)
733 goto out_rg_gunlock;
734
735 down_write(&ip->i_rw_mutex);
736
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000737 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
738 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739
740 bstart = 0;
741 blen = 0;
742
743 for (p = top; p < bottom; p++) {
744 if (!*p)
745 continue;
746
747 bn = be64_to_cpu(*p);
748
749 if (bstart + blen == bn)
750 blen++;
751 else {
752 if (bstart) {
753 if (metadata)
754 gfs2_free_meta(ip, bstart, blen);
755 else
756 gfs2_free_data(ip, bstart, blen);
757 }
758
759 bstart = bn;
760 blen = 1;
761 }
762
763 *p = 0;
764 if (!ip->i_di.di_blocks)
765 gfs2_consist_inode(ip);
766 ip->i_di.di_blocks--;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500767 gfs2_set_inode_blocks(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 }
769 if (bstart) {
770 if (metadata)
771 gfs2_free_meta(ip, bstart, blen);
772 else
773 gfs2_free_data(ip, bstart, blen);
774 }
775
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100776 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500778 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779
780 up_write(&ip->i_rw_mutex);
781
782 gfs2_trans_end(sdp);
783
Steven Whitehousea91ea692006-09-04 12:04:26 -0400784out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400786out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 gfs2_rlist_free(&rlist);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400788out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 return error;
791}
792
793/**
794 * do_grow - Make a file look bigger than it is
795 * @ip: the inode
796 * @size: the size to set the file to
797 *
798 * Called with an exclusive lock on @ip.
799 *
800 * Returns: errno
801 */
802
Steven Whitehousecd915492006-09-04 12:49:07 -0400803static int do_grow(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400805 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806 struct gfs2_alloc *al;
807 struct buffer_head *dibh;
808 unsigned int h;
809 int error;
810
811 al = gfs2_alloc_get(ip);
812
813 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
814 if (error)
815 goto out;
816
Steven Whitehouse2933f922006-11-01 13:23:29 -0500817 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000818 if (error)
819 goto out_gunlock_q;
820
821 al->al_requested = sdp->sd_max_height + RES_DATA;
822
823 error = gfs2_inplace_reserve(ip);
824 if (error)
825 goto out_gunlock_q;
826
827 error = gfs2_trans_begin(sdp,
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100828 sdp->sd_max_height + al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
830 if (error)
831 goto out_ipres;
832
833 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
834 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400835 error = gfs2_unstuff_dinode(ip, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836 if (error)
837 goto out_end_trans;
838 }
839
840 h = calc_tree_height(ip, size);
841 if (ip->i_di.di_height < h) {
842 down_write(&ip->i_rw_mutex);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400843 error = build_height(&ip->i_inode, h);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 up_write(&ip->i_rw_mutex);
845 if (error)
846 goto out_end_trans;
847 }
848 }
849
850 ip->i_di.di_size = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100851 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852
853 error = gfs2_meta_inode_buffer(ip, &dibh);
854 if (error)
855 goto out_end_trans;
856
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000857 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500858 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 brelse(dibh);
860
Steven Whitehousea91ea692006-09-04 12:04:26 -0400861out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400863out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400865out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400867out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 return error;
870}
871
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400872
873/**
874 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
875 *
876 * This is partly borrowed from ext3.
877 */
878static int gfs2_block_truncate_page(struct address_space *mapping)
879{
880 struct inode *inode = mapping->host;
881 struct gfs2_inode *ip = GFS2_I(inode);
882 struct gfs2_sbd *sdp = GFS2_SB(inode);
883 loff_t from = inode->i_size;
884 unsigned long index = from >> PAGE_CACHE_SHIFT;
885 unsigned offset = from & (PAGE_CACHE_SIZE-1);
886 unsigned blocksize, iblock, length, pos;
887 struct buffer_head *bh;
888 struct page *page;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400889 int err;
890
891 page = grab_cache_page(mapping, index);
892 if (!page)
893 return 0;
894
895 blocksize = inode->i_sb->s_blocksize;
896 length = blocksize - (offset & (blocksize - 1));
897 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
898
899 if (!page_has_buffers(page))
900 create_empty_buffers(page, blocksize, 0);
901
902 /* Find the buffer that contains "offset" */
903 bh = page_buffers(page);
904 pos = blocksize;
905 while (offset >= pos) {
906 bh = bh->b_this_page;
907 iblock++;
908 pos += blocksize;
909 }
910
911 err = 0;
912
913 if (!buffer_mapped(bh)) {
914 gfs2_get_block(inode, iblock, bh, 0);
915 /* unmapped? It's a hole - nothing to do */
916 if (!buffer_mapped(bh))
917 goto unlock;
918 }
919
920 /* Ok, it's mapped. Make sure it's up-to-date */
921 if (PageUptodate(page))
922 set_buffer_uptodate(bh);
923
924 if (!buffer_uptodate(bh)) {
925 err = -EIO;
926 ll_rw_block(READ, 1, &bh);
927 wait_on_buffer(bh);
928 /* Uhhuh. Read error. Complain and punt. */
929 if (!buffer_uptodate(bh))
930 goto unlock;
S. Wendy Cheng1875f2f2007-06-25 21:14:31 -0400931 err = 0;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400932 }
933
934 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
935 gfs2_trans_add_bh(ip->i_gl, bh, 0);
936
Nate Diller0507ecf2007-05-10 22:41:28 -0700937 zero_user_page(page, offset, length, KM_USER0);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400938
939unlock:
940 unlock_page(page);
941 page_cache_release(page);
942 return err;
943}
944
Steven Whitehousecd915492006-09-04 12:49:07 -0400945static int trunc_start(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400947 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000948 struct buffer_head *dibh;
949 int journaled = gfs2_is_jdata(ip);
950 int error;
951
952 error = gfs2_trans_begin(sdp,
Jan Engelhardtc5392122006-09-05 14:30:40 +0200953 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 if (error)
955 return error;
956
957 error = gfs2_meta_inode_buffer(ip, &dibh);
958 if (error)
959 goto out;
960
961 if (gfs2_is_stuffed(ip)) {
962 ip->i_di.di_size = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100963 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000964 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500965 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
967 error = 1;
968
969 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -0400970 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400971 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972
973 if (!error) {
974 ip->i_di.di_size = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100975 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000977 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500978 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 }
980 }
981
982 brelse(dibh);
983
Steven Whitehousea91ea692006-09-04 12:04:26 -0400984out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 return error;
987}
988
Steven Whitehousecd915492006-09-04 12:49:07 -0400989static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990{
991 unsigned int height = ip->i_di.di_height;
Steven Whitehousecd915492006-09-04 12:49:07 -0400992 u64 lblock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993 struct metapath mp;
994 int error;
995
996 if (!size)
997 lblock = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000998 else
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400999 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001000
1001 find_metapath(ip, lblock, &mp);
1002 gfs2_alloc_get(ip);
1003
1004 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1005 if (error)
1006 goto out;
1007
1008 while (height--) {
1009 struct strip_mine sm;
1010 sm.sm_first = !!size;
1011 sm.sm_height = height;
1012
1013 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1014 if (error)
1015 break;
1016 }
1017
1018 gfs2_quota_unhold(ip);
1019
Steven Whitehousea91ea692006-09-04 12:04:26 -04001020out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001021 gfs2_alloc_put(ip);
1022 return error;
1023}
1024
1025static int trunc_end(struct gfs2_inode *ip)
1026{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001027 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028 struct buffer_head *dibh;
1029 int error;
1030
1031 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1032 if (error)
1033 return error;
1034
1035 down_write(&ip->i_rw_mutex);
1036
1037 error = gfs2_meta_inode_buffer(ip, &dibh);
1038 if (error)
1039 goto out;
1040
1041 if (!ip->i_di.di_size) {
1042 ip->i_di.di_height = 0;
1043 ip->i_di.di_goal_meta =
1044 ip->i_di.di_goal_data =
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001045 ip->i_no_addr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1047 }
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001048 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1050
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001051 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001052 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 brelse(dibh);
1054
Steven Whitehousea91ea692006-09-04 12:04:26 -04001055out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001057 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058 return error;
1059}
1060
1061/**
1062 * do_shrink - make a file smaller
1063 * @ip: the inode
1064 * @size: the size to make the file
1065 * @truncator: function to truncate the last partial block
1066 *
1067 * Called with an exclusive lock on @ip.
1068 *
1069 * Returns: errno
1070 */
1071
Steven Whitehousecd915492006-09-04 12:49:07 -04001072static int do_shrink(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001073{
1074 int error;
1075
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001076 error = trunc_start(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077 if (error < 0)
1078 return error;
1079 if (error > 0)
1080 return 0;
1081
1082 error = trunc_dealloc(ip, size);
1083 if (!error)
1084 error = trunc_end(ip);
1085
1086 return error;
1087}
1088
Wendy Chenga13b8c52007-08-20 09:29:53 -04001089static int do_touch(struct gfs2_inode *ip, u64 size)
1090{
1091 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1092 struct buffer_head *dibh;
1093 int error;
1094
1095 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1096 if (error)
1097 return error;
1098
1099 down_write(&ip->i_rw_mutex);
1100
1101 error = gfs2_meta_inode_buffer(ip, &dibh);
1102 if (error)
1103 goto do_touch_out;
1104
1105 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1106 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1107 gfs2_dinode_out(ip, dibh->b_data);
1108 brelse(dibh);
1109
1110do_touch_out:
1111 up_write(&ip->i_rw_mutex);
1112 gfs2_trans_end(sdp);
1113 return error;
1114}
1115
David Teiglandb3b94fa2006-01-16 16:50:04 +00001116/**
Steven Whitehouse666a2c52006-01-18 10:29:04 +00001117 * gfs2_truncatei - make a file a given size
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 * @ip: the inode
1119 * @size: the size to make the file
1120 * @truncator: function to truncate the last partial block
1121 *
1122 * The file size can grow, shrink, or stay the same size.
1123 *
1124 * Returns: errno
1125 */
1126
Steven Whitehousecd915492006-09-04 12:49:07 -04001127int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128{
1129 int error;
1130
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001131 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132 return -EINVAL;
1133
1134 if (size > ip->i_di.di_size)
1135 error = do_grow(ip, size);
Wendy Chenga13b8c52007-08-20 09:29:53 -04001136 else if (size < ip->i_di.di_size)
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001137 error = do_shrink(ip, size);
Wendy Chenga13b8c52007-08-20 09:29:53 -04001138 else
1139 /* update time stamps */
1140 error = do_touch(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141
1142 return error;
1143}
1144
1145int gfs2_truncatei_resume(struct gfs2_inode *ip)
1146{
1147 int error;
1148 error = trunc_dealloc(ip, ip->i_di.di_size);
1149 if (!error)
1150 error = trunc_end(ip);
1151 return error;
1152}
1153
1154int gfs2_file_dealloc(struct gfs2_inode *ip)
1155{
1156 return trunc_dealloc(ip, 0);
1157}
1158
1159/**
1160 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1161 * @ip: the file
1162 * @len: the number of bytes to be written to the file
1163 * @data_blocks: returns the number of data blocks required
1164 * @ind_blocks: returns the number of indirect blocks required
1165 *
1166 */
1167
1168void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1169 unsigned int *data_blocks, unsigned int *ind_blocks)
1170{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001171 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172 unsigned int tmp;
1173
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001174 if (gfs2_is_dir(ip)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001175 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001176 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1177 } else {
1178 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1179 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1180 }
1181
1182 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001183 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001184 *ind_blocks += tmp;
1185 }
1186}
1187
1188/**
1189 * gfs2_write_alloc_required - figure out if a write will require an allocation
1190 * @ip: the file being written to
1191 * @offset: the offset to write to
1192 * @len: the number of bytes being written
1193 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1194 *
1195 * Returns: errno
1196 */
1197
Steven Whitehousecd915492006-09-04 12:49:07 -04001198int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199 unsigned int len, int *alloc_required)
1200{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001201 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001202 u64 lblock, lblock_stop, dblock;
1203 u32 extlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204 int new = 0;
1205 int error = 0;
1206
1207 *alloc_required = 0;
1208
1209 if (!len)
1210 return 0;
1211
1212 if (gfs2_is_stuffed(ip)) {
1213 if (offset + len >
1214 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1215 *alloc_required = 1;
1216 return 0;
1217 }
1218
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001219 if (gfs2_is_dir(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001220 unsigned int bsize = sdp->sd_jbsize;
1221 lblock = offset;
1222 do_div(lblock, bsize);
1223 lblock_stop = offset + len + bsize - 1;
1224 do_div(lblock_stop, bsize);
1225 } else {
1226 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1227 lblock = offset >> shift;
1228 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1229 }
1230
1231 for (; lblock < lblock_stop; lblock += extlen) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001232 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001233 if (error)
1234 return error;
1235
1236 if (!dblock) {
1237 *alloc_required = 1;
1238 return 0;
1239 }
1240 }
1241
1242 return 0;
1243}
1244