blob: 5a3187049dd7d8ca9383a79ffc42e4366bb0644f [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{
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040062 struct inode *inode = &ip->i_inode;
63 struct buffer_head *bh;
64 int release = 0;
65
66 if (!page || page->index) {
67 page = grab_cache_page(inode->i_mapping, 0);
68 if (!page)
69 return -ENOMEM;
70 release = 1;
71 }
72
73 if (!PageUptodate(page)) {
74 void *kaddr = kmap(page);
75
76 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
77 ip->i_di.di_size);
78 memset(kaddr + ip->i_di.di_size, 0,
79 PAGE_CACHE_SIZE - ip->i_di.di_size);
80 kunmap(page);
81
82 SetPageUptodate(page);
83 }
84
85 if (!page_has_buffers(page))
86 create_empty_buffers(page, 1 << inode->i_blkbits,
87 (1 << BH_Uptodate));
88
89 bh = page_buffers(page);
90
91 if (!buffer_mapped(bh))
92 map_bh(bh, inode->i_sb, block);
93
94 set_buffer_uptodate(bh);
Steven Whitehouseeaf96522007-08-27 09:49:37 +010095 if (!gfs2_is_jdata(ip))
96 mark_buffer_dirty(bh);
Steven Whitehousebf36a712007-10-17 08:35:19 +010097 if (!gfs2_is_writeback(ip))
Bob Peterson84754872007-09-02 10:55:29 +010098 gfs2_trans_add_bh(ip->i_gl, bh, 0);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040099
100 if (release) {
101 unlock_page(page);
102 page_cache_release(page);
103 }
104
105 return 0;
106}
107
108/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
110 * @ip: The GFS2 inode to unstuff
111 * @unstuffer: the routine that handles unstuffing a non-zero length file
112 * @private: private data for the unstuffer
113 *
114 * This routine unstuffs a dinode and returns it to a "normal" state such
115 * that the height can be grown in the traditional way.
116 *
117 * Returns: errno
118 */
119
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400120int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121{
122 struct buffer_head *bh, *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400123 struct gfs2_dinode *di;
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;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400144 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 dibh, sizeof(struct gfs2_dinode));
146 brelse(bh);
147 } else {
148 block = gfs2_alloc_data(ip);
149
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400150 error = gfs2_unstuffer_page(ip, dibh, block, page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151 if (error)
152 goto out_brelse;
153 }
154 }
155
156 /* Set up the pointer to the new block */
157
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000158 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400159 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
161
162 if (ip->i_di.di_size) {
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400163 *(__be64 *)(di + 1) = cpu_to_be64(block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164 ip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500165 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400166 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000167 }
168
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000169 ip->i_height = 1;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400170 di->di_height = cpu_to_be16(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171
Steven Whitehousea91ea692006-09-04 12:04:26 -0400172out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400174out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 return error;
177}
178
179/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180 * build_height - Build a metadata tree of the requested height
181 * @ip: The GFS2 inode
182 * @height: The height to build to
183 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184 *
185 * Returns: errno
186 */
187
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400188static int build_height(struct inode *inode, unsigned height)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400190 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000191 unsigned new_height = height - ip->i_height;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400192 struct buffer_head *dibh;
193 struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400194 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195 int error;
Al Virob44b84d2006-10-14 10:46:30 -0400196 __be64 *bp;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400197 u64 bn;
198 unsigned n;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000200 if (height <= ip->i_height)
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400201 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400203 error = gfs2_meta_inode_buffer(ip, &dibh);
204 if (error)
205 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400207 for(n = 0; n < new_height; n++) {
208 bn = gfs2_alloc_meta(ip);
209 blocks[n] = gfs2_meta_new(ip->i_gl, bn);
210 gfs2_trans_add_bh(ip->i_gl, blocks[n], 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400212
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400213 n = 0;
214 bn = blocks[0]->b_blocknr;
215 if (new_height > 1) {
216 for(; n < new_height-1; n++) {
217 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
218 GFS2_FORMAT_IN);
219 gfs2_buffer_clear_tail(blocks[n],
220 sizeof(struct gfs2_meta_header));
Al Virob44b84d2006-10-14 10:46:30 -0400221 bp = (__be64 *)(blocks[n]->b_data +
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400222 sizeof(struct gfs2_meta_header));
223 *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
224 brelse(blocks[n]);
225 blocks[n] = NULL;
226 }
227 }
228 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
229 gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
230 dibh, sizeof(struct gfs2_dinode));
231 brelse(blocks[n]);
232 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400233 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400234 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400235 *(__be64 *)(di + 1) = cpu_to_be64(bn);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000236 ip->i_height += new_height;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400237 ip->i_di.di_blocks += new_height;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500238 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000239 di->di_height = cpu_to_be16(ip->i_height);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400240 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400241 brelse(dibh);
242 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243}
244
245/**
246 * find_metapath - Find path through the metadata tree
247 * @ip: The inode pointer
248 * @mp: The metapath to return the result in
249 * @block: The disk block to look up
250 *
251 * This routine returns a struct metapath structure that defines a path
252 * through the metadata of inode "ip" to get to block "block".
253 *
254 * Example:
255 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
256 * filesystem with a blocksize of 4096.
257 *
258 * find_metapath() would return a struct metapath structure set to:
259 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
260 * and mp_list[2] = 165.
261 *
262 * That means that in order to get to the block containing the byte at
263 * offset 101342453, we would load the indirect block pointed to by pointer
264 * 0 in the dinode. We would then load the indirect block pointed to by
265 * pointer 48 in that indirect block. We would then load the data block
266 * pointed to by pointer 165 in that indirect block.
267 *
268 * ----------------------------------------
269 * | Dinode | |
270 * | | 4|
271 * | |0 1 2 3 4 5 9|
272 * | | 6|
273 * ----------------------------------------
274 * |
275 * |
276 * V
277 * ----------------------------------------
278 * | Indirect Block |
279 * | 5|
280 * | 4 4 4 4 4 5 5 1|
281 * |0 5 6 7 8 9 0 1 2|
282 * ----------------------------------------
283 * |
284 * |
285 * V
286 * ----------------------------------------
287 * | Indirect Block |
288 * | 1 1 1 1 1 5|
289 * | 6 6 6 6 6 1|
290 * |0 3 4 5 6 7 2|
291 * ----------------------------------------
292 * |
293 * |
294 * V
295 * ----------------------------------------
296 * | Data block containing offset |
297 * | 101342453 |
298 * | |
299 * | |
300 * ----------------------------------------
301 *
302 */
303
Steven Whitehousecd915492006-09-04 12:49:07 -0400304static void find_metapath(struct gfs2_inode *ip, u64 block,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500305 struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400307 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400308 u64 b = block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309 unsigned int i;
310
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000311 for (i = ip->i_height; i--;)
Steven Whitehousec2668712006-09-04 13:55:48 -0400312 mp->mp_list[i] = do_div(b, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000313
314}
315
316/**
317 * metapointer - Return pointer to start of metadata in a buffer
318 * @bh: The buffer
319 * @height: The metadata height (0 = dinode)
320 * @mp: The metapath
321 *
322 * Return a pointer to the block number of the next height of the metadata
323 * tree given a buffer containing the pointer to the current height of the
324 * metadata tree.
325 */
326
Al Virob44b84d2006-10-14 10:46:30 -0400327static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400328 unsigned int height, const struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329{
330 unsigned int head_size = (height > 0) ?
331 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
Al Virob44b84d2006-10-14 10:46:30 -0400332 __be64 *ptr;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400333 *boundary = 0;
Al Virob44b84d2006-10-14 10:46:30 -0400334 ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
335 if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
Steven Whitehousefd88de562006-05-05 16:59:11 -0400336 *boundary = 1;
337 return ptr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338}
339
340/**
341 * lookup_block - Get the next metadata block in metadata tree
342 * @ip: The GFS2 inode
343 * @bh: Buffer containing the pointers to metadata blocks
344 * @height: The height of the tree (0 = dinode)
345 * @mp: The metapath
346 * @create: Non-zero if we may create a new meatdata block
347 * @new: Used to indicate if we did create a new metadata block
348 * @block: the returned disk block number
349 *
350 * Given a metatree, complete to a particular height, checks to see if the next
351 * height of the tree exists. If not the next height of the tree is created.
352 * The block number of the next height of the metadata tree is returned.
353 *
354 */
355
Steven Whitehousefd88de562006-05-05 16:59:11 -0400356static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
357 unsigned int height, struct metapath *mp, int create,
Steven Whitehousecd915492006-09-04 12:49:07 -0400358 int *new, u64 *block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000359{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400360 int boundary;
Al Virob44b84d2006-10-14 10:46:30 -0400361 __be64 *ptr = metapointer(bh, &boundary, height, mp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362
363 if (*ptr) {
364 *block = be64_to_cpu(*ptr);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400365 return boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366 }
367
368 *block = 0;
369
370 if (!create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400371 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000373 if (height == ip->i_height - 1 && !gfs2_is_dir(ip))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374 *block = gfs2_alloc_data(ip);
375 else
376 *block = gfs2_alloc_meta(ip);
377
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000378 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000379
380 *ptr = cpu_to_be64(*block);
381 ip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500382 gfs2_set_inode_blocks(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000383
384 *new = 1;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400385 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000386}
387
Steven Whitehousefd88de562006-05-05 16:59:11 -0400388static inline void bmap_lock(struct inode *inode, int create)
389{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400390 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400391 if (create)
392 down_write(&ip->i_rw_mutex);
393 else
394 down_read(&ip->i_rw_mutex);
395}
396
397static inline void bmap_unlock(struct inode *inode, int create)
398{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400399 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400 if (create)
401 up_write(&ip->i_rw_mutex);
402 else
403 up_read(&ip->i_rw_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400404}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500406/**
407 * gfs2_block_map - Map a block from an inode to a disk block
408 * @inode: The inode
409 * @lblock: The logical block number
410 * @bh_map: The bh to be mapped
411 *
412 * Find the block number on the current device which corresponds to an
413 * inode's block. If the block had to be created, "new" will be set.
414 *
415 * Returns: errno
416 */
417
Bob Petersone9e1ef22007-12-10 14:13:27 -0600418int gfs2_block_map(struct inode *inode, sector_t lblock,
419 struct buffer_head *bh_map, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400420{
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500421 struct gfs2_inode *ip = GFS2_I(inode);
422 struct gfs2_sbd *sdp = GFS2_SB(inode);
423 struct buffer_head *bh;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000424 unsigned int bsize = sdp->sd_sb.sb_bsize;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500425 unsigned int end_of_metadata;
426 unsigned int x;
427 int error = 0;
428 int new = 0;
429 u64 dblock = 0;
430 int boundary;
431 unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400432 struct metapath mp;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500433 u64 size;
Bob Petersonb0d5fd32007-12-11 19:16:09 -0600434 struct buffer_head *dibh = NULL;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000435 const u64 *arr = sdp->sd_heightsize;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500436 BUG_ON(maxlen == 0);
437
438 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
439 return 0;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400440
441 bmap_lock(inode, create);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500442 clear_buffer_mapped(bh_map);
443 clear_buffer_new(bh_map);
444 clear_buffer_boundary(bh_map);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000445 if (gfs2_is_dir(ip)) {
446 bsize = sdp->sd_jbsize;
447 arr = sdp->sd_jheightsize;
448 }
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500449 size = (lblock + 1) * bsize;
450
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000451 if (size > arr[ip->i_height]) {
452 u8 height = ip->i_height;
453 if (!create)
454 goto out_ok;
455 while (size > arr[height])
456 height++;
457 error = build_height(inode, height);
458 if (error)
459 goto out_fail;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500460 }
461
462 find_metapath(ip, lblock, &mp);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000463 end_of_metadata = ip->i_height - 1;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500464 error = gfs2_meta_inode_buffer(ip, &bh);
465 if (error)
466 goto out_fail;
Bob Petersonb0d5fd32007-12-11 19:16:09 -0600467 dibh = bh;
468 get_bh(dibh);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500469
470 for (x = 0; x < end_of_metadata; x++) {
471 lookup_block(ip, bh, x, &mp, create, &new, &dblock);
472 brelse(bh);
473 if (!dblock)
474 goto out_ok;
475
476 error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
477 if (error)
478 goto out_fail;
479 }
480
481 boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock);
482 if (dblock) {
483 map_bh(bh_map, inode->i_sb, dblock);
484 if (boundary)
485 set_buffer_boundary(bh_map);
486 if (new) {
Bob Petersonb0d5fd32007-12-11 19:16:09 -0600487 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
488 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500489 set_buffer_new(bh_map);
490 goto out_brelse;
491 }
492 while(--maxlen && !buffer_boundary(bh_map)) {
493 u64 eblock;
494
495 mp.mp_list[end_of_metadata]++;
496 boundary = lookup_block(ip, bh, end_of_metadata, &mp, 0, &new, &eblock);
497 if (eblock != ++dblock)
498 break;
499 bh_map->b_size += (1 << inode->i_blkbits);
500 if (boundary)
501 set_buffer_boundary(bh_map);
502 }
503 }
504out_brelse:
505 brelse(bh);
506out_ok:
507 error = 0;
508out_fail:
Bob Petersonb0d5fd32007-12-11 19:16:09 -0600509 if (dibh)
510 brelse(dibh);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400511 bmap_unlock(inode, create);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500512 return error;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400513}
514
Steven Whitehouse941e6d72008-01-28 08:47:38 +0000515/*
516 * Deprecated: do not use in new code
517 */
Steven Whitehousefd88de562006-05-05 16:59:11 -0400518int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
519{
Steven Whitehouse23591252006-10-13 17:25:45 -0400520 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400521 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400522 int create = *new;
523
524 BUG_ON(!extlen);
525 BUG_ON(!dblock);
526 BUG_ON(!new);
527
Steven Whitehouse23591252006-10-13 17:25:45 -0400528 bh.b_size = 1 << (inode->i_blkbits + 5);
Bob Petersone9e1ef22007-12-10 14:13:27 -0600529 ret = gfs2_block_map(inode, lblock, &bh, create);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400530 *extlen = bh.b_size >> inode->i_blkbits;
531 *dblock = bh.b_blocknr;
532 if (buffer_new(&bh))
533 *new = 1;
534 else
535 *new = 0;
536 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537}
538
539/**
540 * recursive_scan - recursively scan through the end of a file
541 * @ip: the inode
542 * @dibh: the dinode buffer
543 * @mp: the path through the metadata to the point to start
544 * @height: the height the recursion is at
545 * @block: the indirect block to look at
546 * @first: 1 if this is the first block
547 * @bc: the call to make for each piece of metadata
548 * @data: data opaque to this function to pass to @bc
549 *
550 * When this is first called @height and @block should be zero and
551 * @first should be 1.
552 *
553 * Returns: errno
554 */
555
556static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
557 struct metapath *mp, unsigned int height,
Steven Whitehousecd915492006-09-04 12:49:07 -0400558 u64 block, int first, block_call_t bc,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559 void *data)
560{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400561 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 struct buffer_head *bh = NULL;
Al Virob44b84d2006-10-14 10:46:30 -0400563 __be64 *top, *bottom;
Steven Whitehousecd915492006-09-04 12:49:07 -0400564 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 int error;
566 int mh_size = sizeof(struct gfs2_meta_header);
567
568 if (!height) {
569 error = gfs2_meta_inode_buffer(ip, &bh);
570 if (error)
571 return error;
572 dibh = bh;
573
Al Virob44b84d2006-10-14 10:46:30 -0400574 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
575 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 } else {
577 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
578 if (error)
579 return error;
580
Al Virob44b84d2006-10-14 10:46:30 -0400581 top = (__be64 *)(bh->b_data + mh_size) +
Jan Engelhardtc53921242006-09-05 14:30:40 +0200582 (first ? mp->mp_list[height] : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583
Al Virob44b84d2006-10-14 10:46:30 -0400584 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 }
586
587 error = bc(ip, dibh, bh, top, bottom, height, data);
588 if (error)
589 goto out;
590
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000591 if (height < ip->i_height - 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592 for (; top < bottom; top++, first = 0) {
593 if (!*top)
594 continue;
595
596 bn = be64_to_cpu(*top);
597
598 error = recursive_scan(ip, dibh, mp, height + 1, bn,
599 first, bc, data);
600 if (error)
601 break;
602 }
603
Steven Whitehousea91ea692006-09-04 12:04:26 -0400604out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606 return error;
607}
608
609/**
610 * do_strip - Look for a layer a particular layer of the file and strip it off
611 * @ip: the inode
612 * @dibh: the dinode buffer
613 * @bh: A buffer of pointers
614 * @top: The first pointer in the buffer
615 * @bottom: One more than the last pointer
616 * @height: the height this buffer is at
617 * @data: a pointer to a struct strip_mine
618 *
619 * Returns: errno
620 */
621
622static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
Al Virob44b84d2006-10-14 10:46:30 -0400623 struct buffer_head *bh, __be64 *top, __be64 *bottom,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624 unsigned int height, void *data)
625{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400626 struct strip_mine *sm = data;
627 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628 struct gfs2_rgrp_list rlist;
Steven Whitehousecd915492006-09-04 12:49:07 -0400629 u64 bn, bstart;
630 u32 blen;
Al Virob44b84d2006-10-14 10:46:30 -0400631 __be64 *p;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632 unsigned int rg_blocks = 0;
633 int metadata;
634 unsigned int revokes = 0;
635 int x;
636 int error;
637
638 if (!*top)
639 sm->sm_first = 0;
640
641 if (height != sm->sm_height)
642 return 0;
643
644 if (sm->sm_first) {
645 top++;
646 sm->sm_first = 0;
647 }
648
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000649 metadata = (height != ip->i_height - 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 if (metadata)
651 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
652
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000653 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 if (error)
655 return error;
656
657 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
658 bstart = 0;
659 blen = 0;
660
661 for (p = top; p < bottom; p++) {
662 if (!*p)
663 continue;
664
665 bn = be64_to_cpu(*p);
666
667 if (bstart + blen == bn)
668 blen++;
669 else {
670 if (bstart)
671 gfs2_rlist_add(sdp, &rlist, bstart);
672
673 bstart = bn;
674 blen = 1;
675 }
676 }
677
678 if (bstart)
679 gfs2_rlist_add(sdp, &rlist, bstart);
680 else
681 goto out; /* Nothing to do */
682
683 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
684
685 for (x = 0; x < rlist.rl_rgrps; x++) {
686 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500687 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100688 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 }
690
691 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
692 if (error)
693 goto out_rlist;
694
695 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
696 RES_INDIRECT + RES_STATFS + RES_QUOTA,
697 revokes);
698 if (error)
699 goto out_rg_gunlock;
700
701 down_write(&ip->i_rw_mutex);
702
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000703 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
704 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705
706 bstart = 0;
707 blen = 0;
708
709 for (p = top; p < bottom; p++) {
710 if (!*p)
711 continue;
712
713 bn = be64_to_cpu(*p);
714
715 if (bstart + blen == bn)
716 blen++;
717 else {
718 if (bstart) {
719 if (metadata)
720 gfs2_free_meta(ip, bstart, blen);
721 else
722 gfs2_free_data(ip, bstart, blen);
723 }
724
725 bstart = bn;
726 blen = 1;
727 }
728
729 *p = 0;
730 if (!ip->i_di.di_blocks)
731 gfs2_consist_inode(ip);
732 ip->i_di.di_blocks--;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500733 gfs2_set_inode_blocks(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 }
735 if (bstart) {
736 if (metadata)
737 gfs2_free_meta(ip, bstart, blen);
738 else
739 gfs2_free_data(ip, bstart, blen);
740 }
741
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100742 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500744 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745
746 up_write(&ip->i_rw_mutex);
747
748 gfs2_trans_end(sdp);
749
Steven Whitehousea91ea692006-09-04 12:04:26 -0400750out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400752out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753 gfs2_rlist_free(&rlist);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400754out:
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000755 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 return error;
757}
758
759/**
760 * do_grow - Make a file look bigger than it is
761 * @ip: the inode
762 * @size: the size to set the file to
763 *
764 * Called with an exclusive lock on @ip.
765 *
766 * Returns: errno
767 */
768
Steven Whitehousecd915492006-09-04 12:49:07 -0400769static int do_grow(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400771 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772 struct gfs2_alloc *al;
773 struct buffer_head *dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 int error;
775
776 al = gfs2_alloc_get(ip);
777
778 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
779 if (error)
780 goto out;
781
Steven Whitehouse2933f922006-11-01 13:23:29 -0500782 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 if (error)
784 goto out_gunlock_q;
785
786 al->al_requested = sdp->sd_max_height + RES_DATA;
787
788 error = gfs2_inplace_reserve(ip);
789 if (error)
790 goto out_gunlock_q;
791
792 error = gfs2_trans_begin(sdp,
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100793 sdp->sd_max_height + al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
795 if (error)
796 goto out_ipres;
797
798 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000799 const u64 *arr = sdp->sd_heightsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400801 error = gfs2_unstuff_dinode(ip, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802 if (error)
803 goto out_end_trans;
804 }
805
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000806 down_write(&ip->i_rw_mutex);
807 if (size > arr[ip->i_height]) {
808 u8 height = ip->i_height;
809 while(size > arr[height])
810 height++;
811 error = build_height(&ip->i_inode, height);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 }
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000813 up_write(&ip->i_rw_mutex);
814 if (error)
815 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000816 }
817
818 ip->i_di.di_size = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100819 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820
821 error = gfs2_meta_inode_buffer(ip, &dibh);
822 if (error)
823 goto out_end_trans;
824
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000825 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500826 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827 brelse(dibh);
828
Steven Whitehousea91ea692006-09-04 12:04:26 -0400829out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400831out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400833out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400835out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837 return error;
838}
839
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400840
841/**
842 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
843 *
844 * This is partly borrowed from ext3.
845 */
846static int gfs2_block_truncate_page(struct address_space *mapping)
847{
848 struct inode *inode = mapping->host;
849 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400850 loff_t from = inode->i_size;
851 unsigned long index = from >> PAGE_CACHE_SHIFT;
852 unsigned offset = from & (PAGE_CACHE_SIZE-1);
853 unsigned blocksize, iblock, length, pos;
854 struct buffer_head *bh;
855 struct page *page;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400856 int err;
857
858 page = grab_cache_page(mapping, index);
859 if (!page)
860 return 0;
861
862 blocksize = inode->i_sb->s_blocksize;
863 length = blocksize - (offset & (blocksize - 1));
864 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
865
866 if (!page_has_buffers(page))
867 create_empty_buffers(page, blocksize, 0);
868
869 /* Find the buffer that contains "offset" */
870 bh = page_buffers(page);
871 pos = blocksize;
872 while (offset >= pos) {
873 bh = bh->b_this_page;
874 iblock++;
875 pos += blocksize;
876 }
877
878 err = 0;
879
880 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600881 gfs2_block_map(inode, iblock, bh, 0);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400882 /* unmapped? It's a hole - nothing to do */
883 if (!buffer_mapped(bh))
884 goto unlock;
885 }
886
887 /* Ok, it's mapped. Make sure it's up-to-date */
888 if (PageUptodate(page))
889 set_buffer_uptodate(bh);
890
891 if (!buffer_uptodate(bh)) {
892 err = -EIO;
893 ll_rw_block(READ, 1, &bh);
894 wait_on_buffer(bh);
895 /* Uhhuh. Read error. Complain and punt. */
896 if (!buffer_uptodate(bh))
897 goto unlock;
S. Wendy Cheng1875f2f2007-06-25 21:14:31 -0400898 err = 0;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400899 }
900
Steven Whitehousebf36a712007-10-17 08:35:19 +0100901 if (!gfs2_is_writeback(ip))
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400902 gfs2_trans_add_bh(ip->i_gl, bh, 0);
903
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800904 zero_user(page, offset, length);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400905
906unlock:
907 unlock_page(page);
908 page_cache_release(page);
909 return err;
910}
911
Steven Whitehousecd915492006-09-04 12:49:07 -0400912static int trunc_start(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400914 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000915 struct buffer_head *dibh;
916 int journaled = gfs2_is_jdata(ip);
917 int error;
918
919 error = gfs2_trans_begin(sdp,
Jan Engelhardtc53921242006-09-05 14:30:40 +0200920 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921 if (error)
922 return error;
923
924 error = gfs2_meta_inode_buffer(ip, &dibh);
925 if (error)
926 goto out;
927
928 if (gfs2_is_stuffed(ip)) {
929 ip->i_di.di_size = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100930 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000931 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500932 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000933 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
934 error = 1;
935
936 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -0400937 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400938 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000939
940 if (!error) {
941 ip->i_di.di_size = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100942 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000944 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500945 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946 }
947 }
948
949 brelse(dibh);
950
Steven Whitehousea91ea692006-09-04 12:04:26 -0400951out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953 return error;
954}
955
Steven Whitehousecd915492006-09-04 12:49:07 -0400956static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000957{
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000958 unsigned int height = ip->i_height;
Steven Whitehousecd915492006-09-04 12:49:07 -0400959 u64 lblock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 struct metapath mp;
961 int error;
962
963 if (!size)
964 lblock = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000965 else
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400966 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967
968 find_metapath(ip, lblock, &mp);
969 gfs2_alloc_get(ip);
970
971 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
972 if (error)
973 goto out;
974
975 while (height--) {
976 struct strip_mine sm;
977 sm.sm_first = !!size;
978 sm.sm_height = height;
979
980 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
981 if (error)
982 break;
983 }
984
985 gfs2_quota_unhold(ip);
986
Steven Whitehousea91ea692006-09-04 12:04:26 -0400987out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 gfs2_alloc_put(ip);
989 return error;
990}
991
992static int trunc_end(struct gfs2_inode *ip)
993{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400994 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000995 struct buffer_head *dibh;
996 int error;
997
998 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
999 if (error)
1000 return error;
1001
1002 down_write(&ip->i_rw_mutex);
1003
1004 error = gfs2_meta_inode_buffer(ip, &dibh);
1005 if (error)
1006 goto out;
1007
1008 if (!ip->i_di.di_size) {
Steven Whitehouseecc30c72008-01-28 10:37:35 +00001009 ip->i_height = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001010 ip->i_di.di_goal_meta =
1011 ip->i_di.di_goal_data =
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001012 ip->i_no_addr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001013 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1014 }
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001015 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1017
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001018 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001019 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020 brelse(dibh);
1021
Steven Whitehousea91ea692006-09-04 12:04:26 -04001022out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025 return error;
1026}
1027
1028/**
1029 * do_shrink - make a file smaller
1030 * @ip: the inode
1031 * @size: the size to make the file
1032 * @truncator: function to truncate the last partial block
1033 *
1034 * Called with an exclusive lock on @ip.
1035 *
1036 * Returns: errno
1037 */
1038
Steven Whitehousecd915492006-09-04 12:49:07 -04001039static int do_shrink(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040{
1041 int error;
1042
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001043 error = trunc_start(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044 if (error < 0)
1045 return error;
1046 if (error > 0)
1047 return 0;
1048
1049 error = trunc_dealloc(ip, size);
1050 if (!error)
1051 error = trunc_end(ip);
1052
1053 return error;
1054}
1055
Wendy Chenga13b8c52007-08-20 09:29:53 -04001056static int do_touch(struct gfs2_inode *ip, u64 size)
1057{
1058 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1059 struct buffer_head *dibh;
1060 int error;
1061
1062 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1063 if (error)
1064 return error;
1065
1066 down_write(&ip->i_rw_mutex);
1067
1068 error = gfs2_meta_inode_buffer(ip, &dibh);
1069 if (error)
1070 goto do_touch_out;
1071
1072 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1073 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1074 gfs2_dinode_out(ip, dibh->b_data);
1075 brelse(dibh);
1076
1077do_touch_out:
1078 up_write(&ip->i_rw_mutex);
1079 gfs2_trans_end(sdp);
1080 return error;
1081}
1082
David Teiglandb3b94fa2006-01-16 16:50:04 +00001083/**
Steven Whitehouse666a2c52006-01-18 10:29:04 +00001084 * gfs2_truncatei - make a file a given size
David Teiglandb3b94fa2006-01-16 16:50:04 +00001085 * @ip: the inode
1086 * @size: the size to make the file
1087 * @truncator: function to truncate the last partial block
1088 *
1089 * The file size can grow, shrink, or stay the same size.
1090 *
1091 * Returns: errno
1092 */
1093
Steven Whitehousecd915492006-09-04 12:49:07 -04001094int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001095{
1096 int error;
1097
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001098 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 return -EINVAL;
1100
1101 if (size > ip->i_di.di_size)
1102 error = do_grow(ip, size);
Wendy Chenga13b8c52007-08-20 09:29:53 -04001103 else if (size < ip->i_di.di_size)
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001104 error = do_shrink(ip, size);
Wendy Chenga13b8c52007-08-20 09:29:53 -04001105 else
1106 /* update time stamps */
1107 error = do_touch(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001108
1109 return error;
1110}
1111
1112int gfs2_truncatei_resume(struct gfs2_inode *ip)
1113{
1114 int error;
1115 error = trunc_dealloc(ip, ip->i_di.di_size);
1116 if (!error)
1117 error = trunc_end(ip);
1118 return error;
1119}
1120
1121int gfs2_file_dealloc(struct gfs2_inode *ip)
1122{
1123 return trunc_dealloc(ip, 0);
1124}
1125
1126/**
1127 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1128 * @ip: the file
1129 * @len: the number of bytes to be written to the file
1130 * @data_blocks: returns the number of data blocks required
1131 * @ind_blocks: returns the number of indirect blocks required
1132 *
1133 */
1134
1135void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1136 unsigned int *data_blocks, unsigned int *ind_blocks)
1137{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001138 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139 unsigned int tmp;
1140
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001141 if (gfs2_is_dir(ip)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001142 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001143 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1144 } else {
1145 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1146 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1147 }
1148
1149 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001150 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001151 *ind_blocks += tmp;
1152 }
1153}
1154
1155/**
1156 * gfs2_write_alloc_required - figure out if a write will require an allocation
1157 * @ip: the file being written to
1158 * @offset: the offset to write to
1159 * @len: the number of bytes being written
1160 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1161 *
1162 * Returns: errno
1163 */
1164
Steven Whitehousecd915492006-09-04 12:49:07 -04001165int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166 unsigned int len, int *alloc_required)
1167{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001168 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001169 struct buffer_head bh;
1170 unsigned int shift;
1171 u64 lblock, lblock_stop, size;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172
1173 *alloc_required = 0;
1174
1175 if (!len)
1176 return 0;
1177
1178 if (gfs2_is_stuffed(ip)) {
1179 if (offset + len >
1180 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1181 *alloc_required = 1;
1182 return 0;
1183 }
1184
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001185 *alloc_required = 1;
1186 shift = sdp->sd_sb.sb_bsize_shift;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001187 if (gfs2_is_dir(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001188 unsigned int bsize = sdp->sd_jbsize;
1189 lblock = offset;
1190 do_div(lblock, bsize);
1191 lblock_stop = offset + len + bsize - 1;
1192 do_div(lblock_stop, bsize);
1193 } else {
Steven Whitehouse1af53572008-01-16 14:24:05 +00001194 u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 lblock = offset >> shift;
1196 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001197 if (lblock_stop > end_of_file)
Bob Peterson05220532008-01-11 13:44:50 -06001198 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199 }
1200
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001201 size = (lblock_stop - lblock) << shift;
1202 do {
1203 bh.b_state = 0;
1204 bh.b_size = size;
1205 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1206 if (!buffer_mapped(&bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001207 return 0;
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001208 size -= bh.b_size;
1209 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1210 } while(size > 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001212 *alloc_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213 return 0;
1214}
1215