blob: 11ffc56f1f819f0808fdbf9d081510478256ca08 [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 {
Steven Whitehousedbac6712008-01-29 09:12:55 +000036 struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
David Teiglandb3b94fa2006-01-16 16:50:04 +000037 __u16 mp_list[GFS2_MAX_META_HEIGHT];
38};
39
40typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
Al Virob44b84d2006-10-14 10:46:30 -040041 struct buffer_head *bh, __be64 *top,
42 __be64 *bottom, unsigned int height,
David Teiglandb3b94fa2006-01-16 16:50:04 +000043 void *data);
44
45struct strip_mine {
46 int sm_first;
47 unsigned int sm_height;
48};
49
50/**
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040051 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
52 * @ip: the inode
53 * @dibh: the dinode buffer
54 * @block: the block number that was allocated
55 * @private: any locked page held by the caller process
56 *
57 * Returns: errno
58 */
59
60static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
Steven Whitehousecd915492006-09-04 12:49:07 -040061 u64 block, struct page *page)
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040062{
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040063 struct inode *inode = &ip->i_inode;
64 struct buffer_head *bh;
65 int release = 0;
66
67 if (!page || page->index) {
68 page = grab_cache_page(inode->i_mapping, 0);
69 if (!page)
70 return -ENOMEM;
71 release = 1;
72 }
73
74 if (!PageUptodate(page)) {
75 void *kaddr = kmap(page);
76
77 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
Steven Whitehousec9e98882008-11-04 09:47:33 +000078 ip->i_disksize);
79 memset(kaddr + ip->i_disksize, 0,
80 PAGE_CACHE_SIZE - ip->i_disksize);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040081 kunmap(page);
82
83 SetPageUptodate(page);
84 }
85
86 if (!page_has_buffers(page))
87 create_empty_buffers(page, 1 << inode->i_blkbits,
88 (1 << BH_Uptodate));
89
90 bh = page_buffers(page);
91
92 if (!buffer_mapped(bh))
93 map_bh(bh, inode->i_sb, block);
94
95 set_buffer_uptodate(bh);
Steven Whitehouseeaf96522007-08-27 09:49:37 +010096 if (!gfs2_is_jdata(ip))
97 mark_buffer_dirty(bh);
Steven Whitehousebf36a712007-10-17 08:35:19 +010098 if (!gfs2_is_writeback(ip))
Bob Peterson84754872007-09-02 10:55:29 +010099 gfs2_trans_add_bh(ip->i_gl, bh, 0);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400100
101 if (release) {
102 unlock_page(page);
103 page_cache_release(page);
104 }
105
106 return 0;
107}
108
109/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
111 * @ip: The GFS2 inode to unstuff
112 * @unstuffer: the routine that handles unstuffing a non-zero length file
113 * @private: private data for the unstuffer
114 *
115 * This routine unstuffs a dinode and returns it to a "normal" state such
116 * that the height can be grown in the traditional way.
117 *
118 * Returns: errno
119 */
120
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400121int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122{
123 struct buffer_head *bh, *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400124 struct gfs2_dinode *di;
Steven Whitehousecd915492006-09-04 12:49:07 -0400125 u64 block = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000126 int isdir = gfs2_is_dir(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 int error;
128
129 down_write(&ip->i_rw_mutex);
130
131 error = gfs2_meta_inode_buffer(ip, &dibh);
132 if (error)
133 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400134
Steven Whitehousec9e98882008-11-04 09:47:33 +0000135 if (ip->i_disksize) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136 /* Get a free block, fill it with the stuffed data,
137 and write it out to disk */
138
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000139 unsigned int n = 1;
140 block = gfs2_alloc_block(ip, &n);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000141 if (isdir) {
Steven Whitehouse5731be52008-02-01 13:16:55 +0000142 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400143 error = gfs2_dir_get_new_buffer(ip, block, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144 if (error)
145 goto out_brelse;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400146 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 dibh, sizeof(struct gfs2_dinode));
148 brelse(bh);
149 } else {
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
Steven Whitehousec9e98882008-11-04 09:47:33 +0000162 if (ip->i_disksize) {
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400163 *(__be64 *)(di + 1) = cpu_to_be64(block);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000164 gfs2_add_inode_blocks(&ip->i_inode, 1);
165 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166 }
167
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000168 ip->i_height = 1;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400169 di->di_height = cpu_to_be16(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000170
Steven Whitehousea91ea692006-09-04 12:04:26 -0400171out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400173out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 return error;
176}
177
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178
179/**
180 * find_metapath - Find path through the metadata tree
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000181 * @sdp: The superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 * @mp: The metapath to return the result in
183 * @block: The disk block to look up
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000184 * @height: The pre-calculated height of the metadata tree
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 *
186 * This routine returns a struct metapath structure that defines a path
187 * through the metadata of inode "ip" to get to block "block".
188 *
189 * Example:
190 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
191 * filesystem with a blocksize of 4096.
192 *
193 * find_metapath() would return a struct metapath structure set to:
194 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
195 * and mp_list[2] = 165.
196 *
197 * That means that in order to get to the block containing the byte at
198 * offset 101342453, we would load the indirect block pointed to by pointer
199 * 0 in the dinode. We would then load the indirect block pointed to by
200 * pointer 48 in that indirect block. We would then load the data block
201 * pointed to by pointer 165 in that indirect block.
202 *
203 * ----------------------------------------
204 * | Dinode | |
205 * | | 4|
206 * | |0 1 2 3 4 5 9|
207 * | | 6|
208 * ----------------------------------------
209 * |
210 * |
211 * V
212 * ----------------------------------------
213 * | Indirect Block |
214 * | 5|
215 * | 4 4 4 4 4 5 5 1|
216 * |0 5 6 7 8 9 0 1 2|
217 * ----------------------------------------
218 * |
219 * |
220 * V
221 * ----------------------------------------
222 * | Indirect Block |
223 * | 1 1 1 1 1 5|
224 * | 6 6 6 6 6 1|
225 * |0 3 4 5 6 7 2|
226 * ----------------------------------------
227 * |
228 * |
229 * V
230 * ----------------------------------------
231 * | Data block containing offset |
232 * | 101342453 |
233 * | |
234 * | |
235 * ----------------------------------------
236 *
237 */
238
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000239static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
240 struct metapath *mp, unsigned int height)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 unsigned int i;
243
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000244 for (i = height; i--;)
Bob Peterson7eabb772008-01-28 11:24:35 -0600245 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246
247}
248
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500249static inline unsigned int metapath_branch_start(const struct metapath *mp)
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000250{
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500251 if (mp->mp_list[0] == 0)
252 return 2;
253 return 1;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000254}
255
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256/**
257 * metapointer - Return pointer to start of metadata in a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000258 * @height: The metadata height (0 = dinode)
259 * @mp: The metapath
260 *
261 * Return a pointer to the block number of the next height of the metadata
262 * tree given a buffer containing the pointer to the current height of the
263 * metadata tree.
264 */
265
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000266static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000267{
Steven Whitehousedbac6712008-01-29 09:12:55 +0000268 struct buffer_head *bh = mp->mp_bh[height];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269 unsigned int head_size = (height > 0) ?
270 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000271 return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272}
273
274/**
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000275 * lookup_metapath - Walk the metadata tree to a specific point
276 * @ip: The inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277 * @mp: The metapath
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000279 * Assumes that the inode's buffer has already been looked up and
280 * hooked onto mp->mp_bh[0] and that the metapath has been initialised
281 * by find_metapath().
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000283 * If this function encounters part of the tree which has not been
284 * allocated, it returns the current height of the tree at the point
285 * at which it found the unallocated block. Blocks which are found are
286 * added to the mp->mp_bh[] list.
287 *
288 * Returns: error or height of metadata tree
David Teiglandb3b94fa2006-01-16 16:50:04 +0000289 */
290
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000291static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292{
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000293 unsigned int end_of_metadata = ip->i_height - 1;
294 unsigned int x;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000295 __be64 *ptr;
296 u64 dblock;
Steven Whitehousee23159d2008-02-12 14:48:39 +0000297 int ret;
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000298
299 for (x = 0; x < end_of_metadata; x++) {
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000300 ptr = metapointer(x, mp);
301 dblock = be64_to_cpu(*ptr);
302 if (!dblock)
303 return x + 1;
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000304
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000305 ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, 0, &mp->mp_bh[x+1]);
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000306 if (ret)
307 return ret;
308 }
309
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000310 return ip->i_height;
Steven Whitehousedbac6712008-01-29 09:12:55 +0000311}
312
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000313static inline void release_metapath(struct metapath *mp)
Steven Whitehousedbac6712008-01-29 09:12:55 +0000314{
315 int i;
316
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000317 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
318 if (mp->mp_bh[i] == NULL)
319 break;
320 brelse(mp->mp_bh[i]);
321 }
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000322}
323
Steven Whitehouse30cbf182008-02-08 13:18:11 +0000324/**
325 * gfs2_extent_length - Returns length of an extent of blocks
326 * @start: Start of the buffer
327 * @len: Length of the buffer in bytes
328 * @ptr: Current position in the buffer
329 * @limit: Max extent length to return (0 = unlimited)
330 * @eob: Set to 1 if we hit "end of block"
331 *
332 * If the first block is zero (unallocated) it will return the number of
333 * unallocated blocks in the extent, otherwise it will return the number
334 * of contiguous blocks in the extent.
335 *
336 * Returns: The length of the extent (minimum of one block)
337 */
338
339static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
340{
341 const __be64 *end = (start + len);
342 const __be64 *first = ptr;
343 u64 d = be64_to_cpu(*ptr);
344
345 *eob = 0;
346 do {
347 ptr++;
348 if (ptr >= end)
349 break;
350 if (limit && --limit == 0)
351 break;
352 if (d)
353 d++;
354 } while(be64_to_cpu(*ptr) == d);
355 if (ptr >= end)
356 *eob = 1;
357 return (ptr - first);
358}
359
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000360static inline void bmap_lock(struct gfs2_inode *ip, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400361{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400362 if (create)
363 down_write(&ip->i_rw_mutex);
364 else
365 down_read(&ip->i_rw_mutex);
366}
367
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000368static inline void bmap_unlock(struct gfs2_inode *ip, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400369{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370 if (create)
371 up_write(&ip->i_rw_mutex);
372 else
373 up_read(&ip->i_rw_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400374}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000375
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000376static inline __be64 *gfs2_indirect_init(struct metapath *mp,
377 struct gfs2_glock *gl, unsigned int i,
378 unsigned offset, u64 bn)
379{
380 __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
381 ((i > 1) ? sizeof(struct gfs2_meta_header) :
382 sizeof(struct gfs2_dinode)));
383 BUG_ON(i < 1);
384 BUG_ON(mp->mp_bh[i] != NULL);
385 mp->mp_bh[i] = gfs2_meta_new(gl, bn);
386 gfs2_trans_add_bh(gl, mp->mp_bh[i], 1);
387 gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
388 gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
389 ptr += offset;
390 *ptr = cpu_to_be64(bn);
391 return ptr;
392}
393
394enum alloc_state {
395 ALLOC_DATA = 0,
396 ALLOC_GROW_DEPTH = 1,
397 ALLOC_GROW_HEIGHT = 2,
398 /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
399};
400
401/**
402 * gfs2_bmap_alloc - Build a metadata tree of the requested height
403 * @inode: The GFS2 inode
404 * @lblock: The logical starting block of the extent
405 * @bh_map: This is used to return the mapping details
406 * @mp: The metapath
407 * @sheight: The starting height (i.e. whats already mapped)
408 * @height: The height to build to
409 * @maxlen: The max number of data blocks to alloc
410 *
411 * In this routine we may have to alloc:
412 * i) Indirect blocks to grow the metadata tree height
413 * ii) Indirect blocks to fill in lower part of the metadata tree
414 * iii) Data blocks
415 *
416 * The function is in two parts. The first part works out the total
417 * number of blocks which we need. The second part does the actual
418 * allocation asking for an extent at a time (if enough contiguous free
419 * blocks are available, there will only be one request per bmap call)
420 * and uses the state machine to initialise the blocks in order.
421 *
422 * Returns: errno on error
423 */
424
425static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
426 struct buffer_head *bh_map, struct metapath *mp,
427 const unsigned int sheight,
428 const unsigned int height,
429 const unsigned int maxlen)
430{
431 struct gfs2_inode *ip = GFS2_I(inode);
432 struct gfs2_sbd *sdp = GFS2_SB(inode);
433 struct buffer_head *dibh = mp->mp_bh[0];
434 u64 bn, dblock = 0;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500435 unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000436 unsigned dblks = 0;
437 unsigned ptrs_per_blk;
438 const unsigned end_of_metadata = height - 1;
439 int eob = 0;
440 enum alloc_state state;
441 __be64 *ptr;
442 __be64 zero_bn = 0;
443
444 BUG_ON(sheight < 1);
445 BUG_ON(dibh == NULL);
446
447 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
448
449 if (height == sheight) {
450 struct buffer_head *bh;
451 /* Bottom indirect block exists, find unalloced extent size */
452 ptr = metapointer(end_of_metadata, mp);
453 bh = mp->mp_bh[end_of_metadata];
454 dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
455 &eob);
456 BUG_ON(dblks < 1);
457 state = ALLOC_DATA;
458 } else {
459 /* Need to allocate indirect blocks */
460 ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
461 dblks = min(maxlen, ptrs_per_blk - mp->mp_list[end_of_metadata]);
462 if (height == ip->i_height) {
463 /* Writing into existing tree, extend tree down */
464 iblks = height - sheight;
465 state = ALLOC_GROW_DEPTH;
466 } else {
467 /* Building up tree height */
468 state = ALLOC_GROW_HEIGHT;
469 iblks = height - ip->i_height;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500470 branch_start = metapath_branch_start(mp);
471 iblks += (height - branch_start);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000472 }
473 }
474
475 /* start of the second part of the function (state machine) */
476
477 blks = dblks + iblks;
478 i = sheight;
479 do {
480 n = blks - alloced;
481 bn = gfs2_alloc_block(ip, &n);
482 alloced += n;
483 if (state != ALLOC_DATA || gfs2_is_jdata(ip))
484 gfs2_trans_add_unrevoke(sdp, bn, n);
485 switch (state) {
486 /* Growing height of tree */
487 case ALLOC_GROW_HEIGHT:
488 if (i == 1) {
489 ptr = (__be64 *)(dibh->b_data +
490 sizeof(struct gfs2_dinode));
491 zero_bn = *ptr;
492 }
493 for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
494 gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
495 if (i - 1 == height - ip->i_height) {
496 i--;
497 gfs2_buffer_copy_tail(mp->mp_bh[i],
498 sizeof(struct gfs2_meta_header),
499 dibh, sizeof(struct gfs2_dinode));
500 gfs2_buffer_clear_tail(dibh,
501 sizeof(struct gfs2_dinode) +
502 sizeof(__be64));
503 ptr = (__be64 *)(mp->mp_bh[i]->b_data +
504 sizeof(struct gfs2_meta_header));
505 *ptr = zero_bn;
506 state = ALLOC_GROW_DEPTH;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500507 for(i = branch_start; i < height; i++) {
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000508 if (mp->mp_bh[i] == NULL)
509 break;
510 brelse(mp->mp_bh[i]);
511 mp->mp_bh[i] = NULL;
512 }
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500513 i = branch_start;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000514 }
515 if (n == 0)
516 break;
517 /* Branching from existing tree */
518 case ALLOC_GROW_DEPTH:
519 if (i > 1 && i < height)
520 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i-1], 1);
521 for (; i < height && n > 0; i++, n--)
522 gfs2_indirect_init(mp, ip->i_gl, i,
523 mp->mp_list[i-1], bn++);
524 if (i == height)
525 state = ALLOC_DATA;
526 if (n == 0)
527 break;
528 /* Tree complete, adding data blocks */
529 case ALLOC_DATA:
530 BUG_ON(n > dblks);
531 BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
532 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[end_of_metadata], 1);
533 dblks = n;
534 ptr = metapointer(end_of_metadata, mp);
535 dblock = bn;
536 while (n-- > 0)
537 *ptr++ = cpu_to_be64(bn++);
538 break;
539 }
540 } while (state != ALLOC_DATA);
541
542 ip->i_height = height;
543 gfs2_add_inode_blocks(&ip->i_inode, alloced);
544 gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
545 map_bh(bh_map, inode->i_sb, dblock);
546 bh_map->b_size = dblks << inode->i_blkbits;
547 set_buffer_new(bh_map);
548 return 0;
549}
550
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500551/**
552 * gfs2_block_map - Map a block from an inode to a disk block
553 * @inode: The inode
554 * @lblock: The logical block number
555 * @bh_map: The bh to be mapped
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000556 * @create: True if its ok to alloc blocks to satify the request
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500557 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000558 * Sets buffer_mapped() if successful, sets buffer_boundary() if a
559 * read of metadata will be required before the next block can be
560 * mapped. Sets buffer_new() if new blocks were allocated.
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500561 *
562 * Returns: errno
563 */
564
Bob Petersone9e1ef22007-12-10 14:13:27 -0600565int gfs2_block_map(struct inode *inode, sector_t lblock,
566 struct buffer_head *bh_map, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400567{
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500568 struct gfs2_inode *ip = GFS2_I(inode);
569 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000570 unsigned int bsize = sdp->sd_sb.sb_bsize;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000571 const unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000572 const u64 *arr = sdp->sd_heightsize;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000573 __be64 *ptr;
574 u64 size;
575 struct metapath mp;
576 int ret;
577 int eob;
578 unsigned int len;
579 struct buffer_head *bh;
580 u8 height;
581
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500582 BUG_ON(maxlen == 0);
583
Steven Whitehousedbac6712008-01-29 09:12:55 +0000584 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000585 bmap_lock(ip, create);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500586 clear_buffer_mapped(bh_map);
587 clear_buffer_new(bh_map);
588 clear_buffer_boundary(bh_map);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000589 if (gfs2_is_dir(ip)) {
590 bsize = sdp->sd_jbsize;
591 arr = sdp->sd_jheightsize;
592 }
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000593
594 ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
595 if (ret)
596 goto out;
597
598 height = ip->i_height;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500599 size = (lblock + 1) * bsize;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000600 while (size > arr[height])
601 height++;
602 find_metapath(sdp, lblock, &mp, height);
603 ret = 1;
604 if (height > ip->i_height || gfs2_is_stuffed(ip))
605 goto do_alloc;
606 ret = lookup_metapath(ip, &mp);
607 if (ret < 0)
608 goto out;
609 if (ret != ip->i_height)
610 goto do_alloc;
611 ptr = metapointer(ip->i_height - 1, &mp);
612 if (*ptr == 0)
613 goto do_alloc;
614 map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
615 bh = mp.mp_bh[ip->i_height - 1];
616 len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
617 bh_map->b_size = (len << inode->i_blkbits);
618 if (eob)
619 set_buffer_boundary(bh_map);
620 ret = 0;
621out:
Steven Whitehousedbac6712008-01-29 09:12:55 +0000622 release_metapath(&mp);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000623 bmap_unlock(ip, create);
624 return ret;
625
626do_alloc:
627 /* All allocations are done here, firstly check create flag */
628 if (!create) {
629 BUG_ON(gfs2_is_stuffed(ip));
630 ret = 0;
631 goto out;
632 }
633
634 /* At this point ret is the tree depth of already allocated blocks */
635 ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
636 goto out;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400637}
638
Steven Whitehouse941e6d72008-01-28 08:47:38 +0000639/*
640 * Deprecated: do not use in new code
641 */
Steven Whitehousefd88de562006-05-05 16:59:11 -0400642int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
643{
Steven Whitehouse23591252006-10-13 17:25:45 -0400644 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400645 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400646 int create = *new;
647
648 BUG_ON(!extlen);
649 BUG_ON(!dblock);
650 BUG_ON(!new);
651
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000652 bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
Bob Petersone9e1ef22007-12-10 14:13:27 -0600653 ret = gfs2_block_map(inode, lblock, &bh, create);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400654 *extlen = bh.b_size >> inode->i_blkbits;
655 *dblock = bh.b_blocknr;
656 if (buffer_new(&bh))
657 *new = 1;
658 else
659 *new = 0;
660 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661}
662
663/**
664 * recursive_scan - recursively scan through the end of a file
665 * @ip: the inode
666 * @dibh: the dinode buffer
667 * @mp: the path through the metadata to the point to start
668 * @height: the height the recursion is at
669 * @block: the indirect block to look at
670 * @first: 1 if this is the first block
671 * @bc: the call to make for each piece of metadata
672 * @data: data opaque to this function to pass to @bc
673 *
674 * When this is first called @height and @block should be zero and
675 * @first should be 1.
676 *
677 * Returns: errno
678 */
679
680static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
681 struct metapath *mp, unsigned int height,
Steven Whitehousecd915492006-09-04 12:49:07 -0400682 u64 block, int first, block_call_t bc,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683 void *data)
684{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400685 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686 struct buffer_head *bh = NULL;
Al Virob44b84d2006-10-14 10:46:30 -0400687 __be64 *top, *bottom;
Steven Whitehousecd915492006-09-04 12:49:07 -0400688 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 int error;
690 int mh_size = sizeof(struct gfs2_meta_header);
691
692 if (!height) {
693 error = gfs2_meta_inode_buffer(ip, &bh);
694 if (error)
695 return error;
696 dibh = bh;
697
Al Virob44b84d2006-10-14 10:46:30 -0400698 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
699 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700 } else {
701 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
702 if (error)
703 return error;
704
Al Virob44b84d2006-10-14 10:46:30 -0400705 top = (__be64 *)(bh->b_data + mh_size) +
Jan Engelhardtc5392122006-09-05 14:30:40 +0200706 (first ? mp->mp_list[height] : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707
Al Virob44b84d2006-10-14 10:46:30 -0400708 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000709 }
710
711 error = bc(ip, dibh, bh, top, bottom, height, data);
712 if (error)
713 goto out;
714
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000715 if (height < ip->i_height - 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716 for (; top < bottom; top++, first = 0) {
717 if (!*top)
718 continue;
719
720 bn = be64_to_cpu(*top);
721
722 error = recursive_scan(ip, dibh, mp, height + 1, bn,
723 first, bc, data);
724 if (error)
725 break;
726 }
727
Steven Whitehousea91ea692006-09-04 12:04:26 -0400728out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 return error;
731}
732
733/**
734 * do_strip - Look for a layer a particular layer of the file and strip it off
735 * @ip: the inode
736 * @dibh: the dinode buffer
737 * @bh: A buffer of pointers
738 * @top: The first pointer in the buffer
739 * @bottom: One more than the last pointer
740 * @height: the height this buffer is at
741 * @data: a pointer to a struct strip_mine
742 *
743 * Returns: errno
744 */
745
746static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
Al Virob44b84d2006-10-14 10:46:30 -0400747 struct buffer_head *bh, __be64 *top, __be64 *bottom,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000748 unsigned int height, void *data)
749{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400750 struct strip_mine *sm = data;
751 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752 struct gfs2_rgrp_list rlist;
Steven Whitehousecd915492006-09-04 12:49:07 -0400753 u64 bn, bstart;
754 u32 blen;
Al Virob44b84d2006-10-14 10:46:30 -0400755 __be64 *p;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 unsigned int rg_blocks = 0;
757 int metadata;
758 unsigned int revokes = 0;
759 int x;
760 int error;
761
762 if (!*top)
763 sm->sm_first = 0;
764
765 if (height != sm->sm_height)
766 return 0;
767
768 if (sm->sm_first) {
769 top++;
770 sm->sm_first = 0;
771 }
772
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000773 metadata = (height != ip->i_height - 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 if (metadata)
775 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
776
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000777 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778 if (error)
779 return error;
780
781 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
782 bstart = 0;
783 blen = 0;
784
785 for (p = top; p < bottom; p++) {
786 if (!*p)
787 continue;
788
789 bn = be64_to_cpu(*p);
790
791 if (bstart + blen == bn)
792 blen++;
793 else {
794 if (bstart)
795 gfs2_rlist_add(sdp, &rlist, bstart);
796
797 bstart = bn;
798 blen = 1;
799 }
800 }
801
802 if (bstart)
803 gfs2_rlist_add(sdp, &rlist, bstart);
804 else
805 goto out; /* Nothing to do */
806
Bob Petersonfe6c9912008-01-28 11:13:02 -0600807 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808
809 for (x = 0; x < rlist.rl_rgrps; x++) {
810 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500811 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100812 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813 }
814
815 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
816 if (error)
817 goto out_rlist;
818
819 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
820 RES_INDIRECT + RES_STATFS + RES_QUOTA,
821 revokes);
822 if (error)
823 goto out_rg_gunlock;
824
825 down_write(&ip->i_rw_mutex);
826
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000827 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
828 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829
830 bstart = 0;
831 blen = 0;
832
833 for (p = top; p < bottom; p++) {
834 if (!*p)
835 continue;
836
837 bn = be64_to_cpu(*p);
838
839 if (bstart + blen == bn)
840 blen++;
841 else {
842 if (bstart) {
843 if (metadata)
844 gfs2_free_meta(ip, bstart, blen);
845 else
846 gfs2_free_data(ip, bstart, blen);
847 }
848
849 bstart = bn;
850 blen = 1;
851 }
852
853 *p = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000854 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855 }
856 if (bstart) {
857 if (metadata)
858 gfs2_free_meta(ip, bstart, blen);
859 else
860 gfs2_free_data(ip, bstart, blen);
861 }
862
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100863 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500865 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866
867 up_write(&ip->i_rw_mutex);
868
869 gfs2_trans_end(sdp);
870
Steven Whitehousea91ea692006-09-04 12:04:26 -0400871out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400873out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874 gfs2_rlist_free(&rlist);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400875out:
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000876 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877 return error;
878}
879
880/**
881 * do_grow - Make a file look bigger than it is
882 * @ip: the inode
883 * @size: the size to set the file to
884 *
885 * Called with an exclusive lock on @ip.
886 *
887 * Returns: errno
888 */
889
Steven Whitehousecd915492006-09-04 12:49:07 -0400890static int do_grow(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400892 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893 struct gfs2_alloc *al;
894 struct buffer_head *dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 int error;
896
897 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300898 if (!al)
899 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900
Steven Whitehoused82661d2008-03-10 15:34:50 +0000901 error = gfs2_quota_lock_check(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000902 if (error)
903 goto out;
904
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905 al->al_requested = sdp->sd_max_height + RES_DATA;
906
907 error = gfs2_inplace_reserve(ip);
908 if (error)
909 goto out_gunlock_q;
910
911 error = gfs2_trans_begin(sdp,
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100912 sdp->sd_max_height + al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
914 if (error)
915 goto out_ipres;
916
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 error = gfs2_meta_inode_buffer(ip, &dibh);
918 if (error)
919 goto out_end_trans;
920
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000921 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
922 if (gfs2_is_stuffed(ip)) {
923 error = gfs2_unstuff_dinode(ip, NULL);
924 if (error)
925 goto out_brelse;
926 }
927 }
928
Steven Whitehousec9e98882008-11-04 09:47:33 +0000929 ip->i_disksize = size;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000930 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
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000934out_brelse:
935 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400936out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400938out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000939 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400940out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400942out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944 return error;
945}
946
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400947
948/**
949 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
950 *
951 * This is partly borrowed from ext3.
952 */
953static int gfs2_block_truncate_page(struct address_space *mapping)
954{
955 struct inode *inode = mapping->host;
956 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400957 loff_t from = inode->i_size;
958 unsigned long index = from >> PAGE_CACHE_SHIFT;
959 unsigned offset = from & (PAGE_CACHE_SIZE-1);
960 unsigned blocksize, iblock, length, pos;
961 struct buffer_head *bh;
962 struct page *page;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400963 int err;
964
965 page = grab_cache_page(mapping, index);
966 if (!page)
967 return 0;
968
969 blocksize = inode->i_sb->s_blocksize;
970 length = blocksize - (offset & (blocksize - 1));
971 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
972
973 if (!page_has_buffers(page))
974 create_empty_buffers(page, blocksize, 0);
975
976 /* Find the buffer that contains "offset" */
977 bh = page_buffers(page);
978 pos = blocksize;
979 while (offset >= pos) {
980 bh = bh->b_this_page;
981 iblock++;
982 pos += blocksize;
983 }
984
985 err = 0;
986
987 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600988 gfs2_block_map(inode, iblock, bh, 0);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400989 /* unmapped? It's a hole - nothing to do */
990 if (!buffer_mapped(bh))
991 goto unlock;
992 }
993
994 /* Ok, it's mapped. Make sure it's up-to-date */
995 if (PageUptodate(page))
996 set_buffer_uptodate(bh);
997
998 if (!buffer_uptodate(bh)) {
999 err = -EIO;
1000 ll_rw_block(READ, 1, &bh);
1001 wait_on_buffer(bh);
1002 /* Uhhuh. Read error. Complain and punt. */
1003 if (!buffer_uptodate(bh))
1004 goto unlock;
S. Wendy Cheng1875f2f2007-06-25 21:14:31 -04001005 err = 0;
Steven Whitehouseba7f7292006-07-26 11:27:10 -04001006 }
1007
Steven Whitehousebf36a712007-10-17 08:35:19 +01001008 if (!gfs2_is_writeback(ip))
Steven Whitehouseba7f7292006-07-26 11:27:10 -04001009 gfs2_trans_add_bh(ip->i_gl, bh, 0);
1010
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001011 zero_user(page, offset, length);
Steven Whitehouseba7f7292006-07-26 11:27:10 -04001012
1013unlock:
1014 unlock_page(page);
1015 page_cache_release(page);
1016 return err;
1017}
1018
Steven Whitehousecd915492006-09-04 12:49:07 -04001019static int trunc_start(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001021 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 struct buffer_head *dibh;
1023 int journaled = gfs2_is_jdata(ip);
1024 int error;
1025
1026 error = gfs2_trans_begin(sdp,
Jan Engelhardtc5392122006-09-05 14:30:40 +02001027 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028 if (error)
1029 return error;
1030
1031 error = gfs2_meta_inode_buffer(ip, &dibh);
1032 if (error)
1033 goto out;
1034
1035 if (gfs2_is_stuffed(ip)) {
Steven Whitehousec9e98882008-11-04 09:47:33 +00001036 ip->i_disksize = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001037 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001038 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001039 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
1041 error = 1;
1042
1043 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -04001044 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001045 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046
1047 if (!error) {
Steven Whitehousec9e98882008-11-04 09:47:33 +00001048 ip->i_disksize = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001049 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001050 ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001051 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001052 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 }
1054 }
1055
1056 brelse(dibh);
1057
Steven Whitehousea91ea692006-09-04 12:04:26 -04001058out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001060 return error;
1061}
1062
Steven Whitehousecd915492006-09-04 12:49:07 -04001063static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001064{
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001065 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +00001066 unsigned int height = ip->i_height;
Steven Whitehousecd915492006-09-04 12:49:07 -04001067 u64 lblock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068 struct metapath mp;
1069 int error;
1070
1071 if (!size)
1072 lblock = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001073 else
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001074 lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001076 find_metapath(sdp, lblock, &mp, ip->i_height);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001077 if (!gfs2_alloc_get(ip))
1078 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079
1080 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1081 if (error)
1082 goto out;
1083
1084 while (height--) {
1085 struct strip_mine sm;
1086 sm.sm_first = !!size;
1087 sm.sm_height = height;
1088
1089 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1090 if (error)
1091 break;
1092 }
1093
1094 gfs2_quota_unhold(ip);
1095
Steven Whitehousea91ea692006-09-04 12:04:26 -04001096out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 gfs2_alloc_put(ip);
1098 return error;
1099}
1100
1101static int trunc_end(struct gfs2_inode *ip)
1102{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001103 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104 struct buffer_head *dibh;
1105 int error;
1106
1107 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1108 if (error)
1109 return error;
1110
1111 down_write(&ip->i_rw_mutex);
1112
1113 error = gfs2_meta_inode_buffer(ip, &dibh);
1114 if (error)
1115 goto out;
1116
Steven Whitehousec9e98882008-11-04 09:47:33 +00001117 if (!ip->i_disksize) {
Steven Whitehouseecc30c72008-01-28 10:37:35 +00001118 ip->i_height = 0;
Steven Whitehousece276b02008-02-06 09:25:45 +00001119 ip->i_goal = ip->i_no_addr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1121 }
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001122 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001123 ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001124
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001125 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001126 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001127 brelse(dibh);
1128
Steven Whitehousea91ea692006-09-04 12:04:26 -04001129out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001131 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132 return error;
1133}
1134
1135/**
1136 * do_shrink - make a file smaller
1137 * @ip: the inode
1138 * @size: the size to make the file
1139 * @truncator: function to truncate the last partial block
1140 *
1141 * Called with an exclusive lock on @ip.
1142 *
1143 * Returns: errno
1144 */
1145
Steven Whitehousecd915492006-09-04 12:49:07 -04001146static int do_shrink(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147{
1148 int error;
1149
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001150 error = trunc_start(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001151 if (error < 0)
1152 return error;
1153 if (error > 0)
1154 return 0;
1155
1156 error = trunc_dealloc(ip, size);
1157 if (!error)
1158 error = trunc_end(ip);
1159
1160 return error;
1161}
1162
Wendy Chenga13b8c52007-08-20 09:29:53 -04001163static int do_touch(struct gfs2_inode *ip, u64 size)
1164{
1165 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1166 struct buffer_head *dibh;
1167 int error;
1168
1169 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1170 if (error)
1171 return error;
1172
1173 down_write(&ip->i_rw_mutex);
1174
1175 error = gfs2_meta_inode_buffer(ip, &dibh);
1176 if (error)
1177 goto do_touch_out;
1178
1179 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1180 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1181 gfs2_dinode_out(ip, dibh->b_data);
1182 brelse(dibh);
1183
1184do_touch_out:
1185 up_write(&ip->i_rw_mutex);
1186 gfs2_trans_end(sdp);
1187 return error;
1188}
1189
David Teiglandb3b94fa2006-01-16 16:50:04 +00001190/**
Steven Whitehouse666a2c52006-01-18 10:29:04 +00001191 * gfs2_truncatei - make a file a given size
David Teiglandb3b94fa2006-01-16 16:50:04 +00001192 * @ip: the inode
1193 * @size: the size to make the file
1194 * @truncator: function to truncate the last partial block
1195 *
1196 * The file size can grow, shrink, or stay the same size.
1197 *
1198 * Returns: errno
1199 */
1200
Steven Whitehousecd915492006-09-04 12:49:07 -04001201int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001202{
1203 int error;
1204
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001205 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206 return -EINVAL;
1207
Steven Whitehousec9e98882008-11-04 09:47:33 +00001208 if (size > ip->i_disksize)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 error = do_grow(ip, size);
Steven Whitehousec9e98882008-11-04 09:47:33 +00001210 else if (size < ip->i_disksize)
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001211 error = do_shrink(ip, size);
Wendy Chenga13b8c52007-08-20 09:29:53 -04001212 else
1213 /* update time stamps */
1214 error = do_touch(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001215
1216 return error;
1217}
1218
1219int gfs2_truncatei_resume(struct gfs2_inode *ip)
1220{
1221 int error;
Steven Whitehousec9e98882008-11-04 09:47:33 +00001222 error = trunc_dealloc(ip, ip->i_disksize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001223 if (!error)
1224 error = trunc_end(ip);
1225 return error;
1226}
1227
1228int gfs2_file_dealloc(struct gfs2_inode *ip)
1229{
1230 return trunc_dealloc(ip, 0);
1231}
1232
1233/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001234 * gfs2_write_alloc_required - figure out if a write will require an allocation
1235 * @ip: the file being written to
1236 * @offset: the offset to write to
1237 * @len: the number of bytes being written
1238 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1239 *
1240 * Returns: errno
1241 */
1242
Steven Whitehousecd915492006-09-04 12:49:07 -04001243int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244 unsigned int len, int *alloc_required)
1245{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001246 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001247 struct buffer_head bh;
1248 unsigned int shift;
1249 u64 lblock, lblock_stop, size;
Steven Whitehouse7ed122e2008-12-10 10:28:10 +00001250 u64 end_of_file;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001251
1252 *alloc_required = 0;
1253
1254 if (!len)
1255 return 0;
1256
1257 if (gfs2_is_stuffed(ip)) {
1258 if (offset + len >
1259 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1260 *alloc_required = 1;
1261 return 0;
1262 }
1263
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001264 *alloc_required = 1;
1265 shift = sdp->sd_sb.sb_bsize_shift;
Steven Whitehouse7ed122e2008-12-10 10:28:10 +00001266 BUG_ON(gfs2_is_dir(ip));
1267 end_of_file = (ip->i_disksize + sdp->sd_sb.sb_bsize - 1) >> shift;
1268 lblock = offset >> shift;
1269 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1270 if (lblock_stop > end_of_file)
1271 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001273 size = (lblock_stop - lblock) << shift;
1274 do {
1275 bh.b_state = 0;
1276 bh.b_size = size;
1277 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1278 if (!buffer_mapped(&bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 return 0;
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001280 size -= bh.b_size;
1281 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1282 } while(size > 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001283
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001284 *alloc_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285 return 0;
1286}
1287