blob: 0db0cd92a38d92f7dcf882c2d942b037c632da17 [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000016
17#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000019#include "bmap.h"
20#include "glock.h"
21#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include "quota.h"
24#include "rgrp.h"
25#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000026#include "dir.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050027#include "util.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010028#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
30/* This doesn't need to be that large as max 64 bit pointers in a 4k
31 * block is 512, so __u16 is fine for that. It saves stack space to
32 * keep it small.
33 */
34struct metapath {
Steven Whitehousedbac6712008-01-29 09:12:55 +000035 struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
David Teiglandb3b94fa2006-01-16 16:50:04 +000036 __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);
Steven Whitehouse602c89d2010-03-25 14:32:43 +000075 u64 dsize = i_size_read(inode);
76
77 if (dsize > (dibh->b_size - sizeof(struct gfs2_dinode)))
78 dsize = dibh->b_size - sizeof(struct gfs2_dinode);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040079
Steven Whitehouse602c89d2010-03-25 14:32:43 +000080 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
81 memset(kaddr + dsize, 0, PAGE_CACHE_SIZE - dsize);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040082 kunmap(page);
83
84 SetPageUptodate(page);
85 }
86
87 if (!page_has_buffers(page))
88 create_empty_buffers(page, 1 << inode->i_blkbits,
89 (1 << BH_Uptodate));
90
91 bh = page_buffers(page);
92
93 if (!buffer_mapped(bh))
94 map_bh(bh, inode->i_sb, block);
95
96 set_buffer_uptodate(bh);
Steven Whitehouseeaf96522007-08-27 09:49:37 +010097 if (!gfs2_is_jdata(ip))
98 mark_buffer_dirty(bh);
Steven Whitehousebf36a712007-10-17 08:35:19 +010099 if (!gfs2_is_writeback(ip))
Bob Peterson84754872007-09-02 10:55:29 +0100100 gfs2_trans_add_bh(ip->i_gl, bh, 0);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400101
102 if (release) {
103 unlock_page(page);
104 page_cache_release(page);
105 }
106
107 return 0;
108}
109
110/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
112 * @ip: The GFS2 inode to unstuff
113 * @unstuffer: the routine that handles unstuffing a non-zero length file
114 * @private: private data for the unstuffer
115 *
116 * This routine unstuffs a dinode and returns it to a "normal" state such
117 * that the height can be grown in the traditional way.
118 *
119 * Returns: errno
120 */
121
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400122int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123{
124 struct buffer_head *bh, *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400125 struct gfs2_dinode *di;
Steven Whitehousecd915492006-09-04 12:49:07 -0400126 u64 block = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000127 int isdir = gfs2_is_dir(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 int error;
129
130 down_write(&ip->i_rw_mutex);
131
132 error = gfs2_meta_inode_buffer(ip, &dibh);
133 if (error)
134 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400135
Steven Whitehousec9e98882008-11-04 09:47:33 +0000136 if (ip->i_disksize) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137 /* Get a free block, fill it with the stuffed data,
138 and write it out to disk */
139
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000140 unsigned int n = 1;
Steven Whitehouse09010972009-05-20 10:48:47 +0100141 error = gfs2_alloc_block(ip, &block, &n);
142 if (error)
143 goto out_brelse;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000144 if (isdir) {
Steven Whitehouse5731be52008-02-01 13:16:55 +0000145 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400146 error = gfs2_dir_get_new_buffer(ip, block, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 if (error)
148 goto out_brelse;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400149 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 dibh, sizeof(struct gfs2_dinode));
151 brelse(bh);
152 } else {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400153 error = gfs2_unstuffer_page(ip, dibh, block, page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 if (error)
155 goto out_brelse;
156 }
157 }
158
159 /* Set up the pointer to the new block */
160
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000161 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400162 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
164
Steven Whitehousec9e98882008-11-04 09:47:33 +0000165 if (ip->i_disksize) {
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400166 *(__be64 *)(di + 1) = cpu_to_be64(block);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000167 gfs2_add_inode_blocks(&ip->i_inode, 1);
168 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 }
170
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000171 ip->i_height = 1;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400172 di->di_height = cpu_to_be16(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173
Steven Whitehousea91ea692006-09-04 12:04:26 -0400174out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400176out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178 return error;
179}
180
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181
182/**
183 * find_metapath - Find path through the metadata tree
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000184 * @sdp: The superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 * @mp: The metapath to return the result in
186 * @block: The disk block to look up
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000187 * @height: The pre-calculated height of the metadata tree
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188 *
189 * This routine returns a struct metapath structure that defines a path
190 * through the metadata of inode "ip" to get to block "block".
191 *
192 * Example:
193 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
194 * filesystem with a blocksize of 4096.
195 *
196 * find_metapath() would return a struct metapath structure set to:
197 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
198 * and mp_list[2] = 165.
199 *
200 * That means that in order to get to the block containing the byte at
201 * offset 101342453, we would load the indirect block pointed to by pointer
202 * 0 in the dinode. We would then load the indirect block pointed to by
203 * pointer 48 in that indirect block. We would then load the data block
204 * pointed to by pointer 165 in that indirect block.
205 *
206 * ----------------------------------------
207 * | Dinode | |
208 * | | 4|
209 * | |0 1 2 3 4 5 9|
210 * | | 6|
211 * ----------------------------------------
212 * |
213 * |
214 * V
215 * ----------------------------------------
216 * | Indirect Block |
217 * | 5|
218 * | 4 4 4 4 4 5 5 1|
219 * |0 5 6 7 8 9 0 1 2|
220 * ----------------------------------------
221 * |
222 * |
223 * V
224 * ----------------------------------------
225 * | Indirect Block |
226 * | 1 1 1 1 1 5|
227 * | 6 6 6 6 6 1|
228 * |0 3 4 5 6 7 2|
229 * ----------------------------------------
230 * |
231 * |
232 * V
233 * ----------------------------------------
234 * | Data block containing offset |
235 * | 101342453 |
236 * | |
237 * | |
238 * ----------------------------------------
239 *
240 */
241
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000242static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
243 struct metapath *mp, unsigned int height)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 unsigned int i;
246
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000247 for (i = height; i--;)
Bob Peterson7eabb772008-01-28 11:24:35 -0600248 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249
250}
251
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500252static inline unsigned int metapath_branch_start(const struct metapath *mp)
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000253{
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500254 if (mp->mp_list[0] == 0)
255 return 2;
256 return 1;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000257}
258
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259/**
260 * metapointer - Return pointer to start of metadata in a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261 * @height: The metadata height (0 = dinode)
262 * @mp: The metapath
263 *
264 * Return a pointer to the block number of the next height of the metadata
265 * tree given a buffer containing the pointer to the current height of the
266 * metadata tree.
267 */
268
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000269static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270{
Steven Whitehousedbac6712008-01-29 09:12:55 +0000271 struct buffer_head *bh = mp->mp_bh[height];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272 unsigned int head_size = (height > 0) ?
273 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000274 return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000275}
276
277/**
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000278 * lookup_metapath - Walk the metadata tree to a specific point
279 * @ip: The inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280 * @mp: The metapath
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000282 * Assumes that the inode's buffer has already been looked up and
283 * hooked onto mp->mp_bh[0] and that the metapath has been initialised
284 * by find_metapath().
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000286 * If this function encounters part of the tree which has not been
287 * allocated, it returns the current height of the tree at the point
288 * at which it found the unallocated block. Blocks which are found are
289 * added to the mp->mp_bh[] list.
290 *
291 * Returns: error or height of metadata tree
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 */
293
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000294static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295{
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000296 unsigned int end_of_metadata = ip->i_height - 1;
297 unsigned int x;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000298 __be64 *ptr;
299 u64 dblock;
Steven Whitehousee23159d2008-02-12 14:48:39 +0000300 int ret;
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000301
302 for (x = 0; x < end_of_metadata; x++) {
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000303 ptr = metapointer(x, mp);
304 dblock = be64_to_cpu(*ptr);
305 if (!dblock)
306 return x + 1;
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000307
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000308 ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, 0, &mp->mp_bh[x+1]);
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000309 if (ret)
310 return ret;
311 }
312
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000313 return ip->i_height;
Steven Whitehousedbac6712008-01-29 09:12:55 +0000314}
315
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000316static inline void release_metapath(struct metapath *mp)
Steven Whitehousedbac6712008-01-29 09:12:55 +0000317{
318 int i;
319
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000320 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
321 if (mp->mp_bh[i] == NULL)
322 break;
323 brelse(mp->mp_bh[i]);
324 }
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000325}
326
Steven Whitehouse30cbf182008-02-08 13:18:11 +0000327/**
328 * gfs2_extent_length - Returns length of an extent of blocks
329 * @start: Start of the buffer
330 * @len: Length of the buffer in bytes
331 * @ptr: Current position in the buffer
332 * @limit: Max extent length to return (0 = unlimited)
333 * @eob: Set to 1 if we hit "end of block"
334 *
335 * If the first block is zero (unallocated) it will return the number of
336 * unallocated blocks in the extent, otherwise it will return the number
337 * of contiguous blocks in the extent.
338 *
339 * Returns: The length of the extent (minimum of one block)
340 */
341
342static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
343{
344 const __be64 *end = (start + len);
345 const __be64 *first = ptr;
346 u64 d = be64_to_cpu(*ptr);
347
348 *eob = 0;
349 do {
350 ptr++;
351 if (ptr >= end)
352 break;
353 if (limit && --limit == 0)
354 break;
355 if (d)
356 d++;
357 } while(be64_to_cpu(*ptr) == d);
358 if (ptr >= end)
359 *eob = 1;
360 return (ptr - first);
361}
362
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000363static inline void bmap_lock(struct gfs2_inode *ip, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400364{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400365 if (create)
366 down_write(&ip->i_rw_mutex);
367 else
368 down_read(&ip->i_rw_mutex);
369}
370
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000371static inline void bmap_unlock(struct gfs2_inode *ip, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400372{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373 if (create)
374 up_write(&ip->i_rw_mutex);
375 else
376 up_read(&ip->i_rw_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400377}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000378
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000379static inline __be64 *gfs2_indirect_init(struct metapath *mp,
380 struct gfs2_glock *gl, unsigned int i,
381 unsigned offset, u64 bn)
382{
383 __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
384 ((i > 1) ? sizeof(struct gfs2_meta_header) :
385 sizeof(struct gfs2_dinode)));
386 BUG_ON(i < 1);
387 BUG_ON(mp->mp_bh[i] != NULL);
388 mp->mp_bh[i] = gfs2_meta_new(gl, bn);
389 gfs2_trans_add_bh(gl, mp->mp_bh[i], 1);
390 gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
391 gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
392 ptr += offset;
393 *ptr = cpu_to_be64(bn);
394 return ptr;
395}
396
397enum alloc_state {
398 ALLOC_DATA = 0,
399 ALLOC_GROW_DEPTH = 1,
400 ALLOC_GROW_HEIGHT = 2,
401 /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
402};
403
404/**
405 * gfs2_bmap_alloc - Build a metadata tree of the requested height
406 * @inode: The GFS2 inode
407 * @lblock: The logical starting block of the extent
408 * @bh_map: This is used to return the mapping details
409 * @mp: The metapath
410 * @sheight: The starting height (i.e. whats already mapped)
411 * @height: The height to build to
412 * @maxlen: The max number of data blocks to alloc
413 *
414 * In this routine we may have to alloc:
415 * i) Indirect blocks to grow the metadata tree height
416 * ii) Indirect blocks to fill in lower part of the metadata tree
417 * iii) Data blocks
418 *
419 * The function is in two parts. The first part works out the total
420 * number of blocks which we need. The second part does the actual
421 * allocation asking for an extent at a time (if enough contiguous free
422 * blocks are available, there will only be one request per bmap call)
423 * and uses the state machine to initialise the blocks in order.
424 *
425 * Returns: errno on error
426 */
427
428static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
429 struct buffer_head *bh_map, struct metapath *mp,
430 const unsigned int sheight,
431 const unsigned int height,
432 const unsigned int maxlen)
433{
434 struct gfs2_inode *ip = GFS2_I(inode);
435 struct gfs2_sbd *sdp = GFS2_SB(inode);
436 struct buffer_head *dibh = mp->mp_bh[0];
437 u64 bn, dblock = 0;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500438 unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000439 unsigned dblks = 0;
440 unsigned ptrs_per_blk;
441 const unsigned end_of_metadata = height - 1;
442 int eob = 0;
443 enum alloc_state state;
444 __be64 *ptr;
445 __be64 zero_bn = 0;
446
447 BUG_ON(sheight < 1);
448 BUG_ON(dibh == NULL);
449
450 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
451
452 if (height == sheight) {
453 struct buffer_head *bh;
454 /* Bottom indirect block exists, find unalloced extent size */
455 ptr = metapointer(end_of_metadata, mp);
456 bh = mp->mp_bh[end_of_metadata];
457 dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
458 &eob);
459 BUG_ON(dblks < 1);
460 state = ALLOC_DATA;
461 } else {
462 /* Need to allocate indirect blocks */
463 ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
464 dblks = min(maxlen, ptrs_per_blk - mp->mp_list[end_of_metadata]);
465 if (height == ip->i_height) {
466 /* Writing into existing tree, extend tree down */
467 iblks = height - sheight;
468 state = ALLOC_GROW_DEPTH;
469 } else {
470 /* Building up tree height */
471 state = ALLOC_GROW_HEIGHT;
472 iblks = height - ip->i_height;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500473 branch_start = metapath_branch_start(mp);
474 iblks += (height - branch_start);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000475 }
476 }
477
478 /* start of the second part of the function (state machine) */
479
480 blks = dblks + iblks;
481 i = sheight;
482 do {
Steven Whitehouse09010972009-05-20 10:48:47 +0100483 int error;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000484 n = blks - alloced;
Steven Whitehouse09010972009-05-20 10:48:47 +0100485 error = gfs2_alloc_block(ip, &bn, &n);
486 if (error)
487 return error;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000488 alloced += n;
489 if (state != ALLOC_DATA || gfs2_is_jdata(ip))
490 gfs2_trans_add_unrevoke(sdp, bn, n);
491 switch (state) {
492 /* Growing height of tree */
493 case ALLOC_GROW_HEIGHT:
494 if (i == 1) {
495 ptr = (__be64 *)(dibh->b_data +
496 sizeof(struct gfs2_dinode));
497 zero_bn = *ptr;
498 }
499 for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
500 gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
501 if (i - 1 == height - ip->i_height) {
502 i--;
503 gfs2_buffer_copy_tail(mp->mp_bh[i],
504 sizeof(struct gfs2_meta_header),
505 dibh, sizeof(struct gfs2_dinode));
506 gfs2_buffer_clear_tail(dibh,
507 sizeof(struct gfs2_dinode) +
508 sizeof(__be64));
509 ptr = (__be64 *)(mp->mp_bh[i]->b_data +
510 sizeof(struct gfs2_meta_header));
511 *ptr = zero_bn;
512 state = ALLOC_GROW_DEPTH;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500513 for(i = branch_start; i < height; i++) {
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000514 if (mp->mp_bh[i] == NULL)
515 break;
516 brelse(mp->mp_bh[i]);
517 mp->mp_bh[i] = NULL;
518 }
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500519 i = branch_start;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000520 }
521 if (n == 0)
522 break;
523 /* Branching from existing tree */
524 case ALLOC_GROW_DEPTH:
525 if (i > 1 && i < height)
526 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i-1], 1);
527 for (; i < height && n > 0; i++, n--)
528 gfs2_indirect_init(mp, ip->i_gl, i,
529 mp->mp_list[i-1], bn++);
530 if (i == height)
531 state = ALLOC_DATA;
532 if (n == 0)
533 break;
534 /* Tree complete, adding data blocks */
535 case ALLOC_DATA:
536 BUG_ON(n > dblks);
537 BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
538 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[end_of_metadata], 1);
539 dblks = n;
540 ptr = metapointer(end_of_metadata, mp);
541 dblock = bn;
542 while (n-- > 0)
543 *ptr++ = cpu_to_be64(bn++);
544 break;
545 }
Steven Whitehouse07ccb7b2010-02-12 10:10:55 +0000546 } while ((state != ALLOC_DATA) || !dblock);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000547
548 ip->i_height = height;
549 gfs2_add_inode_blocks(&ip->i_inode, alloced);
550 gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
551 map_bh(bh_map, inode->i_sb, dblock);
552 bh_map->b_size = dblks << inode->i_blkbits;
553 set_buffer_new(bh_map);
554 return 0;
555}
556
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500557/**
558 * gfs2_block_map - Map a block from an inode to a disk block
559 * @inode: The inode
560 * @lblock: The logical block number
561 * @bh_map: The bh to be mapped
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000562 * @create: True if its ok to alloc blocks to satify the request
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500563 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000564 * Sets buffer_mapped() if successful, sets buffer_boundary() if a
565 * read of metadata will be required before the next block can be
566 * mapped. Sets buffer_new() if new blocks were allocated.
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500567 *
568 * Returns: errno
569 */
570
Bob Petersone9e1ef22007-12-10 14:13:27 -0600571int gfs2_block_map(struct inode *inode, sector_t lblock,
572 struct buffer_head *bh_map, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400573{
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500574 struct gfs2_inode *ip = GFS2_I(inode);
575 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000576 unsigned int bsize = sdp->sd_sb.sb_bsize;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000577 const unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000578 const u64 *arr = sdp->sd_heightsize;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000579 __be64 *ptr;
580 u64 size;
581 struct metapath mp;
582 int ret;
583 int eob;
584 unsigned int len;
585 struct buffer_head *bh;
586 u8 height;
587
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500588 BUG_ON(maxlen == 0);
589
Steven Whitehousedbac6712008-01-29 09:12:55 +0000590 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000591 bmap_lock(ip, create);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500592 clear_buffer_mapped(bh_map);
593 clear_buffer_new(bh_map);
594 clear_buffer_boundary(bh_map);
Steven Whitehouse63997772009-06-12 08:49:20 +0100595 trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000596 if (gfs2_is_dir(ip)) {
597 bsize = sdp->sd_jbsize;
598 arr = sdp->sd_jheightsize;
599 }
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000600
601 ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
602 if (ret)
603 goto out;
604
605 height = ip->i_height;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500606 size = (lblock + 1) * bsize;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000607 while (size > arr[height])
608 height++;
609 find_metapath(sdp, lblock, &mp, height);
610 ret = 1;
611 if (height > ip->i_height || gfs2_is_stuffed(ip))
612 goto do_alloc;
613 ret = lookup_metapath(ip, &mp);
614 if (ret < 0)
615 goto out;
616 if (ret != ip->i_height)
617 goto do_alloc;
618 ptr = metapointer(ip->i_height - 1, &mp);
619 if (*ptr == 0)
620 goto do_alloc;
621 map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
622 bh = mp.mp_bh[ip->i_height - 1];
623 len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
624 bh_map->b_size = (len << inode->i_blkbits);
625 if (eob)
626 set_buffer_boundary(bh_map);
627 ret = 0;
628out:
Steven Whitehousedbac6712008-01-29 09:12:55 +0000629 release_metapath(&mp);
Steven Whitehouse63997772009-06-12 08:49:20 +0100630 trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000631 bmap_unlock(ip, create);
632 return ret;
633
634do_alloc:
635 /* All allocations are done here, firstly check create flag */
636 if (!create) {
637 BUG_ON(gfs2_is_stuffed(ip));
638 ret = 0;
639 goto out;
640 }
641
642 /* At this point ret is the tree depth of already allocated blocks */
643 ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
644 goto out;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400645}
646
Steven Whitehouse941e6d72008-01-28 08:47:38 +0000647/*
648 * Deprecated: do not use in new code
649 */
Steven Whitehousefd88de562006-05-05 16:59:11 -0400650int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
651{
Steven Whitehouse23591252006-10-13 17:25:45 -0400652 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400653 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400654 int create = *new;
655
656 BUG_ON(!extlen);
657 BUG_ON(!dblock);
658 BUG_ON(!new);
659
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000660 bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
Bob Petersone9e1ef22007-12-10 14:13:27 -0600661 ret = gfs2_block_map(inode, lblock, &bh, create);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400662 *extlen = bh.b_size >> inode->i_blkbits;
663 *dblock = bh.b_blocknr;
664 if (buffer_new(&bh))
665 *new = 1;
666 else
667 *new = 0;
668 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669}
670
671/**
672 * recursive_scan - recursively scan through the end of a file
673 * @ip: the inode
674 * @dibh: the dinode buffer
675 * @mp: the path through the metadata to the point to start
676 * @height: the height the recursion is at
677 * @block: the indirect block to look at
678 * @first: 1 if this is the first block
679 * @bc: the call to make for each piece of metadata
680 * @data: data opaque to this function to pass to @bc
681 *
682 * When this is first called @height and @block should be zero and
683 * @first should be 1.
684 *
685 * Returns: errno
686 */
687
688static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
689 struct metapath *mp, unsigned int height,
Steven Whitehousecd915492006-09-04 12:49:07 -0400690 u64 block, int first, block_call_t bc,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 void *data)
692{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400693 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 struct buffer_head *bh = NULL;
Al Virob44b84d2006-10-14 10:46:30 -0400695 __be64 *top, *bottom;
Steven Whitehousecd915492006-09-04 12:49:07 -0400696 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697 int error;
698 int mh_size = sizeof(struct gfs2_meta_header);
699
700 if (!height) {
701 error = gfs2_meta_inode_buffer(ip, &bh);
702 if (error)
703 return error;
704 dibh = bh;
705
Al Virob44b84d2006-10-14 10:46:30 -0400706 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
707 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708 } else {
709 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
710 if (error)
711 return error;
712
Al Virob44b84d2006-10-14 10:46:30 -0400713 top = (__be64 *)(bh->b_data + mh_size) +
Jan Engelhardtc5392122006-09-05 14:30:40 +0200714 (first ? mp->mp_list[height] : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715
Al Virob44b84d2006-10-14 10:46:30 -0400716 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 }
718
719 error = bc(ip, dibh, bh, top, bottom, height, data);
720 if (error)
721 goto out;
722
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000723 if (height < ip->i_height - 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 for (; top < bottom; top++, first = 0) {
725 if (!*top)
726 continue;
727
728 bn = be64_to_cpu(*top);
729
730 error = recursive_scan(ip, dibh, mp, height + 1, bn,
731 first, bc, data);
732 if (error)
733 break;
734 }
735
Steven Whitehousea91ea692006-09-04 12:04:26 -0400736out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 return error;
739}
740
741/**
742 * do_strip - Look for a layer a particular layer of the file and strip it off
743 * @ip: the inode
744 * @dibh: the dinode buffer
745 * @bh: A buffer of pointers
746 * @top: The first pointer in the buffer
747 * @bottom: One more than the last pointer
748 * @height: the height this buffer is at
749 * @data: a pointer to a struct strip_mine
750 *
751 * Returns: errno
752 */
753
754static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
Al Virob44b84d2006-10-14 10:46:30 -0400755 struct buffer_head *bh, __be64 *top, __be64 *bottom,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 unsigned int height, void *data)
757{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400758 struct strip_mine *sm = data;
759 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 struct gfs2_rgrp_list rlist;
Steven Whitehousecd915492006-09-04 12:49:07 -0400761 u64 bn, bstart;
762 u32 blen;
Al Virob44b84d2006-10-14 10:46:30 -0400763 __be64 *p;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 unsigned int rg_blocks = 0;
765 int metadata;
766 unsigned int revokes = 0;
767 int x;
768 int error;
769
770 if (!*top)
771 sm->sm_first = 0;
772
773 if (height != sm->sm_height)
774 return 0;
775
776 if (sm->sm_first) {
777 top++;
778 sm->sm_first = 0;
779 }
780
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000781 metadata = (height != ip->i_height - 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 if (metadata)
783 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
784
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000785 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 if (error)
787 return error;
788
789 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
790 bstart = 0;
791 blen = 0;
792
793 for (p = top; p < bottom; p++) {
794 if (!*p)
795 continue;
796
797 bn = be64_to_cpu(*p);
798
799 if (bstart + blen == bn)
800 blen++;
801 else {
802 if (bstart)
803 gfs2_rlist_add(sdp, &rlist, bstart);
804
805 bstart = bn;
806 blen = 1;
807 }
808 }
809
810 if (bstart)
811 gfs2_rlist_add(sdp, &rlist, bstart);
812 else
813 goto out; /* Nothing to do */
814
Bob Petersonfe6c9912008-01-28 11:13:02 -0600815 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000816
817 for (x = 0; x < rlist.rl_rgrps; x++) {
818 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500819 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100820 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821 }
822
823 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
824 if (error)
825 goto out_rlist;
826
827 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
828 RES_INDIRECT + RES_STATFS + RES_QUOTA,
829 revokes);
830 if (error)
831 goto out_rg_gunlock;
832
833 down_write(&ip->i_rw_mutex);
834
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000835 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
836 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837
838 bstart = 0;
839 blen = 0;
840
841 for (p = top; p < bottom; p++) {
842 if (!*p)
843 continue;
844
845 bn = be64_to_cpu(*p);
846
847 if (bstart + blen == bn)
848 blen++;
849 else {
850 if (bstart) {
851 if (metadata)
852 gfs2_free_meta(ip, bstart, blen);
853 else
854 gfs2_free_data(ip, bstart, blen);
855 }
856
857 bstart = bn;
858 blen = 1;
859 }
860
861 *p = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000862 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000863 }
864 if (bstart) {
865 if (metadata)
866 gfs2_free_meta(ip, bstart, blen);
867 else
868 gfs2_free_data(ip, bstart, blen);
869 }
870
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100871 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500873 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874
875 up_write(&ip->i_rw_mutex);
876
877 gfs2_trans_end(sdp);
878
Steven Whitehousea91ea692006-09-04 12:04:26 -0400879out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000880 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400881out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 gfs2_rlist_free(&rlist);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400883out:
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000884 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 return error;
886}
887
888/**
889 * do_grow - Make a file look bigger than it is
890 * @ip: the inode
891 * @size: the size to set the file to
892 *
893 * Called with an exclusive lock on @ip.
894 *
895 * Returns: errno
896 */
897
Steven Whitehousecd915492006-09-04 12:49:07 -0400898static int do_grow(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400900 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901 struct gfs2_alloc *al;
902 struct buffer_head *dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903 int error;
904
905 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300906 if (!al)
907 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908
Steven Whitehoused82661d2008-03-10 15:34:50 +0000909 error = gfs2_quota_lock_check(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910 if (error)
911 goto out;
912
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 al->al_requested = sdp->sd_max_height + RES_DATA;
914
915 error = gfs2_inplace_reserve(ip);
916 if (error)
917 goto out_gunlock_q;
918
919 error = gfs2_trans_begin(sdp,
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100920 sdp->sd_max_height + al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
922 if (error)
923 goto out_ipres;
924
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925 error = gfs2_meta_inode_buffer(ip, &dibh);
926 if (error)
927 goto out_end_trans;
928
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000929 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
930 if (gfs2_is_stuffed(ip)) {
931 error = gfs2_unstuff_dinode(ip, NULL);
932 if (error)
933 goto out_brelse;
934 }
935 }
936
Steven Whitehousec9e98882008-11-04 09:47:33 +0000937 ip->i_disksize = size;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000938 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000939 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500940 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000942out_brelse:
943 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400944out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000945 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400946out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400948out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400950out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952 return error;
953}
954
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400955
956/**
957 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
958 *
959 * This is partly borrowed from ext3.
960 */
961static int gfs2_block_truncate_page(struct address_space *mapping)
962{
963 struct inode *inode = mapping->host;
964 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400965 loff_t from = inode->i_size;
966 unsigned long index = from >> PAGE_CACHE_SHIFT;
967 unsigned offset = from & (PAGE_CACHE_SIZE-1);
968 unsigned blocksize, iblock, length, pos;
969 struct buffer_head *bh;
970 struct page *page;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400971 int err;
972
973 page = grab_cache_page(mapping, index);
974 if (!page)
975 return 0;
976
977 blocksize = inode->i_sb->s_blocksize;
978 length = blocksize - (offset & (blocksize - 1));
979 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
980
981 if (!page_has_buffers(page))
982 create_empty_buffers(page, blocksize, 0);
983
984 /* Find the buffer that contains "offset" */
985 bh = page_buffers(page);
986 pos = blocksize;
987 while (offset >= pos) {
988 bh = bh->b_this_page;
989 iblock++;
990 pos += blocksize;
991 }
992
993 err = 0;
994
995 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600996 gfs2_block_map(inode, iblock, bh, 0);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400997 /* unmapped? It's a hole - nothing to do */
998 if (!buffer_mapped(bh))
999 goto unlock;
1000 }
1001
1002 /* Ok, it's mapped. Make sure it's up-to-date */
1003 if (PageUptodate(page))
1004 set_buffer_uptodate(bh);
1005
1006 if (!buffer_uptodate(bh)) {
1007 err = -EIO;
1008 ll_rw_block(READ, 1, &bh);
1009 wait_on_buffer(bh);
1010 /* Uhhuh. Read error. Complain and punt. */
1011 if (!buffer_uptodate(bh))
1012 goto unlock;
S. Wendy Cheng1875f2f2007-06-25 21:14:31 -04001013 err = 0;
Steven Whitehouseba7f7292006-07-26 11:27:10 -04001014 }
1015
Steven Whitehousebf36a712007-10-17 08:35:19 +01001016 if (!gfs2_is_writeback(ip))
Steven Whitehouseba7f7292006-07-26 11:27:10 -04001017 gfs2_trans_add_bh(ip->i_gl, bh, 0);
1018
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001019 zero_user(page, offset, length);
Steven Whitehouse40bc9a22009-06-10 09:09:40 +01001020 mark_buffer_dirty(bh);
Steven Whitehouseba7f7292006-07-26 11:27:10 -04001021unlock:
1022 unlock_page(page);
1023 page_cache_release(page);
1024 return err;
1025}
1026
Steven Whitehousecd915492006-09-04 12:49:07 -04001027static int trunc_start(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001029 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001030 struct buffer_head *dibh;
1031 int journaled = gfs2_is_jdata(ip);
1032 int error;
1033
1034 error = gfs2_trans_begin(sdp,
Jan Engelhardtc5392122006-09-05 14:30:40 +02001035 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 if (error)
1037 return error;
1038
1039 error = gfs2_meta_inode_buffer(ip, &dibh);
1040 if (error)
1041 goto out;
1042
1043 if (gfs2_is_stuffed(ip)) {
Steven Whitehouse602c89d2010-03-25 14:32:43 +00001044 u64 dsize = size + sizeof(struct gfs2_inode);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001045 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001046 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001047 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehouse602c89d2010-03-25 14:32:43 +00001048 if (dsize > dibh->b_size)
1049 dsize = dibh->b_size;
1050 gfs2_buffer_clear_tail(dibh, dsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001051 error = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -04001053 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001054 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001055
1056 if (!error) {
Steven Whitehousec9e98882008-11-04 09:47:33 +00001057 ip->i_disksize = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001058 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001059 ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001060 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001061 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 }
1063 }
1064
1065 brelse(dibh);
1066
Steven Whitehousea91ea692006-09-04 12:04:26 -04001067out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069 return error;
1070}
1071
Steven Whitehousecd915492006-09-04 12:49:07 -04001072static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001073{
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001074 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +00001075 unsigned int height = ip->i_height;
Steven Whitehousecd915492006-09-04 12:49:07 -04001076 u64 lblock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077 struct metapath mp;
1078 int error;
1079
1080 if (!size)
1081 lblock = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001082 else
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001083 lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001085 find_metapath(sdp, lblock, &mp, ip->i_height);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001086 if (!gfs2_alloc_get(ip))
1087 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088
1089 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1090 if (error)
1091 goto out;
1092
1093 while (height--) {
1094 struct strip_mine sm;
1095 sm.sm_first = !!size;
1096 sm.sm_height = height;
1097
1098 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1099 if (error)
1100 break;
1101 }
1102
1103 gfs2_quota_unhold(ip);
1104
Steven Whitehousea91ea692006-09-04 12:04:26 -04001105out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106 gfs2_alloc_put(ip);
1107 return error;
1108}
1109
1110static int trunc_end(struct gfs2_inode *ip)
1111{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001112 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113 struct buffer_head *dibh;
1114 int error;
1115
1116 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1117 if (error)
1118 return error;
1119
1120 down_write(&ip->i_rw_mutex);
1121
1122 error = gfs2_meta_inode_buffer(ip, &dibh);
1123 if (error)
1124 goto out;
1125
Steven Whitehousec9e98882008-11-04 09:47:33 +00001126 if (!ip->i_disksize) {
Steven Whitehouseecc30c72008-01-28 10:37:35 +00001127 ip->i_height = 0;
Steven Whitehousece276b02008-02-06 09:25:45 +00001128 ip->i_goal = ip->i_no_addr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001129 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1130 }
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001131 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001132 ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001134 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001135 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 brelse(dibh);
1137
Steven Whitehousea91ea692006-09-04 12:04:26 -04001138out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001140 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141 return error;
1142}
1143
1144/**
1145 * do_shrink - make a file smaller
1146 * @ip: the inode
1147 * @size: the size to make the file
1148 * @truncator: function to truncate the last partial block
1149 *
1150 * Called with an exclusive lock on @ip.
1151 *
1152 * Returns: errno
1153 */
1154
Steven Whitehousecd915492006-09-04 12:49:07 -04001155static int do_shrink(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156{
1157 int error;
1158
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001159 error = trunc_start(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160 if (error < 0)
1161 return error;
1162 if (error > 0)
1163 return 0;
1164
1165 error = trunc_dealloc(ip, size);
1166 if (!error)
1167 error = trunc_end(ip);
1168
1169 return error;
1170}
1171
Wendy Chenga13b8c52007-08-20 09:29:53 -04001172static int do_touch(struct gfs2_inode *ip, u64 size)
1173{
1174 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1175 struct buffer_head *dibh;
1176 int error;
1177
1178 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1179 if (error)
1180 return error;
1181
1182 down_write(&ip->i_rw_mutex);
1183
1184 error = gfs2_meta_inode_buffer(ip, &dibh);
1185 if (error)
1186 goto do_touch_out;
1187
1188 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1189 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1190 gfs2_dinode_out(ip, dibh->b_data);
1191 brelse(dibh);
1192
1193do_touch_out:
1194 up_write(&ip->i_rw_mutex);
1195 gfs2_trans_end(sdp);
1196 return error;
1197}
1198
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199/**
Steven Whitehouse666a2c52006-01-18 10:29:04 +00001200 * gfs2_truncatei - make a file a given size
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201 * @ip: the inode
1202 * @size: the size to make the file
1203 * @truncator: function to truncate the last partial block
1204 *
1205 * The file size can grow, shrink, or stay the same size.
1206 *
1207 * Returns: errno
1208 */
1209
Steven Whitehousecd915492006-09-04 12:49:07 -04001210int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211{
1212 int error;
1213
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001214 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001215 return -EINVAL;
1216
Steven Whitehousec9e98882008-11-04 09:47:33 +00001217 if (size > ip->i_disksize)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001218 error = do_grow(ip, size);
Steven Whitehousec9e98882008-11-04 09:47:33 +00001219 else if (size < ip->i_disksize)
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001220 error = do_shrink(ip, size);
Wendy Chenga13b8c52007-08-20 09:29:53 -04001221 else
1222 /* update time stamps */
1223 error = do_touch(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001224
1225 return error;
1226}
1227
1228int gfs2_truncatei_resume(struct gfs2_inode *ip)
1229{
1230 int error;
Steven Whitehousec9e98882008-11-04 09:47:33 +00001231 error = trunc_dealloc(ip, ip->i_disksize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001232 if (!error)
1233 error = trunc_end(ip);
1234 return error;
1235}
1236
1237int gfs2_file_dealloc(struct gfs2_inode *ip)
1238{
1239 return trunc_dealloc(ip, 0);
1240}
1241
1242/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001243 * gfs2_write_alloc_required - figure out if a write will require an allocation
1244 * @ip: the file being written to
1245 * @offset: the offset to write to
1246 * @len: the number of bytes being written
1247 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1248 *
1249 * Returns: errno
1250 */
1251
Steven Whitehousecd915492006-09-04 12:49:07 -04001252int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001253 unsigned int len, int *alloc_required)
1254{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001255 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001256 struct buffer_head bh;
1257 unsigned int shift;
1258 u64 lblock, lblock_stop, size;
Steven Whitehouse7ed122e2008-12-10 10:28:10 +00001259 u64 end_of_file;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001260
1261 *alloc_required = 0;
1262
1263 if (!len)
1264 return 0;
1265
1266 if (gfs2_is_stuffed(ip)) {
1267 if (offset + len >
1268 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1269 *alloc_required = 1;
1270 return 0;
1271 }
1272
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001273 *alloc_required = 1;
1274 shift = sdp->sd_sb.sb_bsize_shift;
Steven Whitehouse7ed122e2008-12-10 10:28:10 +00001275 BUG_ON(gfs2_is_dir(ip));
1276 end_of_file = (ip->i_disksize + sdp->sd_sb.sb_bsize - 1) >> shift;
1277 lblock = offset >> shift;
1278 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1279 if (lblock_stop > end_of_file)
1280 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001282 size = (lblock_stop - lblock) << shift;
1283 do {
1284 bh.b_state = 0;
1285 bh.b_size = size;
1286 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1287 if (!buffer_mapped(&bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001288 return 0;
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001289 size -= bh.b_size;
1290 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1291 } while(size > 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001293 *alloc_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001294 return 0;
1295}
1296