blob: 19097bc7c81d11628b1e879a1d7ce382395306b6 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/mm.h>
16#include <linux/pagemap.h>
17#include <linux/writeback.h>
18#include <linux/swap.h>
19#include <linux/delay.h>
Steven Whitehouse2e565bb2006-10-02 11:38:25 -040020#include <linux/bio.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020022#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000023
24#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include "glock.h"
27#include "glops.h"
28#include "inode.h"
29#include "log.h"
30#include "lops.h"
31#include "meta_io.h"
32#include "rgrp.h"
33#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050034#include "util.h"
Steven Whitehouse4340fe62006-07-11 09:46:33 -040035#include "ops_address.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000036
David Teiglandb3b94fa2006-01-16 16:50:04 +000037static int aspace_get_block(struct inode *inode, sector_t lblock,
38 struct buffer_head *bh_result, int create)
39{
Steven Whitehouse5c676f62006-02-27 17:23:27 -050040 gfs2_assert_warn(inode->i_sb->s_fs_info, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000041 return -EOPNOTSUPP;
42}
43
44static int gfs2_aspace_writepage(struct page *page,
45 struct writeback_control *wbc)
46{
47 return block_write_full_page(page, aspace_get_block, wbc);
48}
49
Steven Whitehouse66de0452006-07-03 13:37:30 -040050static const struct address_space_operations aspace_aops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 .writepage = gfs2_aspace_writepage,
Steven Whitehouse4340fe62006-07-11 09:46:33 -040052 .releasepage = gfs2_releasepage,
David Teiglandb3b94fa2006-01-16 16:50:04 +000053};
54
55/**
56 * gfs2_aspace_get - Create and initialize a struct inode structure
57 * @sdp: the filesystem the aspace is in
58 *
59 * Right now a struct inode is just a struct inode. Maybe Linux
60 * will supply a more lightweight address space construct (that works)
61 * in the future.
62 *
63 * Make sure pages/buffers in this aspace aren't in high memory.
64 *
65 * Returns: the aspace
66 */
67
68struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
69{
70 struct inode *aspace;
71
72 aspace = new_inode(sdp->sd_vfs);
73 if (aspace) {
Steven Whitehousef3bba032006-07-11 09:50:54 -040074 mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +000075 aspace->i_mapping->a_ops = &aspace_aops;
76 aspace->i_size = ~0ULL;
Theodore Ts'obba9dfd2006-09-27 01:50:31 -070077 aspace->i_private = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000078 insert_inode_hash(aspace);
79 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000080 return aspace;
81}
82
83void gfs2_aspace_put(struct inode *aspace)
84{
85 remove_inode_hash(aspace);
86 iput(aspace);
87}
88
89/**
David Teiglandb3b94fa2006-01-16 16:50:04 +000090 * gfs2_meta_inval - Invalidate all buffers associated with a glock
91 * @gl: the glock
92 *
93 */
94
95void gfs2_meta_inval(struct gfs2_glock *gl)
96{
97 struct gfs2_sbd *sdp = gl->gl_sbd;
98 struct inode *aspace = gl->gl_aspace;
99 struct address_space *mapping = gl->gl_aspace->i_mapping;
100
101 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
102
103 atomic_inc(&aspace->i_writecount);
104 truncate_inode_pages(mapping, 0);
105 atomic_dec(&aspace->i_writecount);
106
107 gfs2_assert_withdraw(sdp, !mapping->nrpages);
108}
109
110/**
111 * gfs2_meta_sync - Sync all buffers associated with a glock
112 * @gl: The glock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 *
114 */
115
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400116void gfs2_meta_sync(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117{
118 struct address_space *mapping = gl->gl_aspace->i_mapping;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400119 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400121 filemap_fdatawrite(mapping);
122 error = filemap_fdatawait(mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123
124 if (error)
125 gfs2_io_error(gl->gl_sbd);
126}
127
128/**
129 * getbuf - Get a buffer with a given address space
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500130 * @gl: the glock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131 * @blkno: the block number (filesystem scope)
132 * @create: 1 if the buffer should be created
133 *
134 * Returns: the buffer
135 */
136
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500137static struct buffer_head *getbuf(struct gfs2_glock *gl, u64 blkno, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000138{
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500139 struct address_space *mapping = gl->gl_aspace->i_mapping;
140 struct gfs2_sbd *sdp = gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 struct page *page;
142 struct buffer_head *bh;
143 unsigned int shift;
144 unsigned long index;
145 unsigned int bufnum;
146
147 shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
148 index = blkno >> shift; /* convert block to page */
149 bufnum = blkno - (index << shift); /* block buf index within page */
150
151 if (create) {
152 for (;;) {
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500153 page = grab_cache_page(mapping, index);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 if (page)
155 break;
156 yield();
157 }
158 } else {
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500159 page = find_lock_page(mapping, index);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 if (!page)
161 return NULL;
162 }
163
164 if (!page_has_buffers(page))
165 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
166
167 /* Locate header for our buffer within our page */
168 for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
169 /* Do nothing */;
170 get_bh(bh);
171
172 if (!buffer_mapped(bh))
173 map_bh(bh, sdp->sd_vfs, blkno);
174
175 unlock_page(page);
176 mark_page_accessed(page);
177 page_cache_release(page);
178
179 return bh;
180}
181
182static void meta_prep_new(struct buffer_head *bh)
183{
184 struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
185
186 lock_buffer(bh);
187 clear_buffer_dirty(bh);
188 set_buffer_uptodate(bh);
189 unlock_buffer(bh);
190
191 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
192}
193
194/**
195 * gfs2_meta_new - Get a block
196 * @gl: The glock associated with this block
197 * @blkno: The block number
198 *
199 * Returns: The buffer
200 */
201
Steven Whitehousecd915492006-09-04 12:49:07 -0400202struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203{
204 struct buffer_head *bh;
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500205 bh = getbuf(gl, blkno, CREATE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 meta_prep_new(bh);
207 return bh;
208}
209
210/**
211 * gfs2_meta_read - Read a block from disk
212 * @gl: The glock covering the block
213 * @blkno: The block number
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400214 * @flags: flags
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215 * @bhp: the place where the buffer is returned (NULL on failure)
216 *
217 * Returns: errno
218 */
219
Steven Whitehousecd915492006-09-04 12:49:07 -0400220int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000221 struct buffer_head **bhp)
222{
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500223 *bhp = getbuf(gl, blkno, CREATE);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400224 if (!buffer_uptodate(*bhp))
Steven Whitehouse2e565bb2006-10-02 11:38:25 -0400225 ll_rw_block(READ_META, 1, bhp);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400226 if (flags & DIO_WAIT) {
227 int error = gfs2_meta_wait(gl->gl_sbd, *bhp);
228 if (error) {
229 brelse(*bhp);
230 return error;
231 }
232 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400234 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235}
236
237/**
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400238 * gfs2_meta_wait - Reread a block from disk
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239 * @sdp: the filesystem
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400240 * @bh: The block to wait for
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241 *
242 * Returns: errno
243 */
244
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400245int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246{
247 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
248 return -EIO;
249
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400250 wait_on_buffer(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400252 if (!buffer_uptodate(bh)) {
253 struct gfs2_trans *tr = current->journal_info;
254 if (tr && tr->tr_touched)
255 gfs2_io_error_bh(sdp, bh);
256 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400258 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
259 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260
261 return 0;
262}
263
264/**
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000265 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000266 * @gl: the glock the buffer belongs to
267 * @bh: The buffer to be attached to
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000268 * @meta: Flag to indicate whether its metadata or not
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269 */
270
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500271void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
272 int meta)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273{
274 struct gfs2_bufdata *bd;
275
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000276 if (meta)
277 lock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500279 if (bh->b_private) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000280 if (meta)
281 unlock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 return;
283 }
284
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800285 bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000286 bd->bd_bh = bh;
287 bd->bd_gl = gl;
288
289 INIT_LIST_HEAD(&bd->bd_list_tr);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400290 if (meta)
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000291 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400292 else
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000293 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500294 bh->b_private = bd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000296 if (meta)
297 unlock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298}
299
300/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
302 * @ip: the inode who owns the buffers
303 * @bstart: the first buffer in the run
304 * @blen: the number of buffers in the run
305 *
306 */
307
Steven Whitehousecd915492006-09-04 12:49:07 -0400308void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400310 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311 struct buffer_head *bh;
312
313 while (blen) {
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500314 bh = getbuf(ip->i_gl, bstart, NO_CREATE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315 if (bh) {
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100316 struct gfs2_bufdata *bd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100318 lock_buffer(bh);
319 gfs2_log_lock(sdp);
320 bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 if (test_clear_buffer_pinned(bh)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500322 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323 list_del_init(&bd->bd_le.le_list);
324 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
325 sdp->sd_log_num_buf--;
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100326 tr->tr_num_buf_rm++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327 brelse(bh);
328 }
329 if (bd) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000330 if (bd->bd_ail) {
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100331 gfs2_remove_from_ail(NULL, bd);
332 bh->b_private = NULL;
333 bd->bd_bh = NULL;
334 bd->bd_blkno = bh->b_blocknr;
335 gfs2_trans_add_revoke(sdp, bd);
336 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338 clear_buffer_dirty(bh);
339 clear_buffer_uptodate(bh);
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100340 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 unlock_buffer(bh);
342
343 brelse(bh);
344 }
345
346 bstart++;
347 blen--;
348 }
349}
350
351/**
352 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
353 * @ip: The GFS2 inode
354 *
355 * This releases buffers that are in the most-recently-used array of
356 * blocks used for indirect block addressing for this inode.
357 */
358
359void gfs2_meta_cache_flush(struct gfs2_inode *ip)
360{
361 struct buffer_head **bh_slot;
362 unsigned int x;
363
364 spin_lock(&ip->i_spin);
365
366 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
367 bh_slot = &ip->i_cache[x];
368 if (!*bh_slot)
369 break;
370 brelse(*bh_slot);
371 *bh_slot = NULL;
372 }
373
374 spin_unlock(&ip->i_spin);
375}
376
377/**
378 * gfs2_meta_indirect_buffer - Get a metadata buffer
379 * @ip: The GFS2 inode
380 * @height: The level of this buf in the metadata (indir addr) tree (if any)
381 * @num: The block number (device relative) of the buffer
382 * @new: Non-zero if we may create a new buffer
383 * @bhp: the buffer is returned here
384 *
385 * Try to use the gfs2_inode's MRU metadata tree cache.
386 *
387 * Returns: errno
388 */
389
Steven Whitehousecd915492006-09-04 12:49:07 -0400390int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391 int new, struct buffer_head **bhp)
392{
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400393 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
394 struct gfs2_glock *gl = ip->i_gl;
395 struct buffer_head *bh = NULL, **bh_slot = ip->i_cache + height;
396 int in_cache = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397
Steven Whitehouseb0041572006-11-23 10:51:34 -0500398 BUG_ON(!gl);
399 BUG_ON(!sdp);
400
David Teiglandb3b94fa2006-01-16 16:50:04 +0000401 spin_lock(&ip->i_spin);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400402 if (*bh_slot && (*bh_slot)->b_blocknr == num) {
403 bh = *bh_slot;
404 get_bh(bh);
405 in_cache = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000406 }
407 spin_unlock(&ip->i_spin);
408
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400409 if (!bh)
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500410 bh = getbuf(gl, num, CREATE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400412 if (!bh)
413 return -ENOBUFS;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414
415 if (new) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400416 if (gfs2_assert_warn(sdp, height))
417 goto err;
418 meta_prep_new(bh);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000419 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
421 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400422 } else {
423 u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
424 if (!buffer_uptodate(bh)) {
Steven Whitehouse2e565bb2006-10-02 11:38:25 -0400425 ll_rw_block(READ_META, 1, &bh);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400426 if (gfs2_meta_wait(sdp, bh))
427 goto err;
428 }
429 if (gfs2_metatype_check(sdp, bh, mtype))
430 goto err;
431 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400433 if (!in_cache) {
434 spin_lock(&ip->i_spin);
435 if (*bh_slot)
436 brelse(*bh_slot);
437 *bh_slot = bh;
438 get_bh(bh);
439 spin_unlock(&ip->i_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 }
441
442 *bhp = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443 return 0;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400444err:
445 brelse(bh);
446 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447}
448
449/**
450 * gfs2_meta_ra - start readahead on an extent of a file
451 * @gl: the glock the blocks belong to
452 * @dblock: the starting disk block
453 * @extlen: the number of blocks in the extent
454 *
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400455 * returns: the first buffer in the extent
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456 */
457
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400458struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459{
460 struct gfs2_sbd *sdp = gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461 struct buffer_head *first_bh, *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400462 u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500463 sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400465 BUG_ON(!extlen);
466
467 if (max_ra < 1)
468 max_ra = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 if (extlen > max_ra)
470 extlen = max_ra;
471
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500472 first_bh = getbuf(gl, dblock, CREATE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473
474 if (buffer_uptodate(first_bh))
475 goto out;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400476 if (!buffer_locked(first_bh))
Steven Whitehouse2e565bb2006-10-02 11:38:25 -0400477 ll_rw_block(READ_META, 1, &first_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478
479 dblock++;
480 extlen--;
481
482 while (extlen) {
Steven Whitehousecb4c0312006-11-23 11:16:32 -0500483 bh = getbuf(gl, dblock, CREATE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400485 if (!buffer_uptodate(bh) && !buffer_locked(bh))
486 ll_rw_block(READA, 1, &bh);
487 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488 dblock++;
489 extlen--;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400490 if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
491 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 }
493
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400494 wait_on_buffer(first_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400495out:
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400496 return first_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000497}
498