blob: a5630ec6c045fb0e872ea6c08600ccee5566d7dc [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 Whitehouse5c676f62006-02-27 17:23:27 -050020#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020021#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022
23#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050024#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "glock.h"
26#include "glops.h"
27#include "inode.h"
28#include "log.h"
29#include "lops.h"
30#include "meta_io.h"
31#include "rgrp.h"
32#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050033#include "util.h"
Steven Whitehouse4340fe62006-07-11 09:46:33 -040034#include "ops_address.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000035
36#define buffer_busy(bh) \
37((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned)))
38#define buffer_in_io(bh) \
39((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock)))
40
41static int aspace_get_block(struct inode *inode, sector_t lblock,
42 struct buffer_head *bh_result, int create)
43{
Steven Whitehouse5c676f62006-02-27 17:23:27 -050044 gfs2_assert_warn(inode->i_sb->s_fs_info, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000045 return -EOPNOTSUPP;
46}
47
48static int gfs2_aspace_writepage(struct page *page,
49 struct writeback_control *wbc)
50{
51 return block_write_full_page(page, aspace_get_block, wbc);
52}
53
Steven Whitehouse66de0452006-07-03 13:37:30 -040054static const struct address_space_operations aspace_aops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +000055 .writepage = gfs2_aspace_writepage,
Steven Whitehouse4340fe62006-07-11 09:46:33 -040056 .releasepage = gfs2_releasepage,
David Teiglandb3b94fa2006-01-16 16:50:04 +000057};
58
59/**
60 * gfs2_aspace_get - Create and initialize a struct inode structure
61 * @sdp: the filesystem the aspace is in
62 *
63 * Right now a struct inode is just a struct inode. Maybe Linux
64 * will supply a more lightweight address space construct (that works)
65 * in the future.
66 *
67 * Make sure pages/buffers in this aspace aren't in high memory.
68 *
69 * Returns: the aspace
70 */
71
72struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
73{
74 struct inode *aspace;
75
76 aspace = new_inode(sdp->sd_vfs);
77 if (aspace) {
Steven Whitehousef3bba032006-07-11 09:50:54 -040078 mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +000079 aspace->i_mapping->a_ops = &aspace_aops;
80 aspace->i_size = ~0ULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -050081 aspace->u.generic_ip = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000082 insert_inode_hash(aspace);
83 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000084 return aspace;
85}
86
87void gfs2_aspace_put(struct inode *aspace)
88{
89 remove_inode_hash(aspace);
90 iput(aspace);
91}
92
93/**
94 * gfs2_ail1_start_one - Start I/O on a part of the AIL
95 * @sdp: the filesystem
96 * @tr: the part of the AIL
97 *
98 */
99
100void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
101{
102 struct gfs2_bufdata *bd, *s;
103 struct buffer_head *bh;
104 int retry;
105
Steven Whitehouse190562b2006-04-20 16:57:23 -0400106 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
107
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108 do {
109 retry = 0;
110
111 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
112 bd_ail_st_list) {
113 bh = bd->bd_bh;
114
115 gfs2_assert(sdp, bd->bd_ail == ai);
116
117 if (!buffer_busy(bh)) {
Steven Whitehouse1b502592006-05-18 14:10:52 -0400118 if (!buffer_uptodate(bh)) {
119 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120 gfs2_io_error_bh(sdp, bh);
Steven Whitehouse1b502592006-05-18 14:10:52 -0400121 gfs2_log_lock(sdp);
122 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123 list_move(&bd->bd_ail_st_list,
124 &ai->ai_ail2_list);
125 continue;
126 }
127
128 if (!buffer_dirty(bh))
129 continue;
130
131 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
132
133 gfs2_log_unlock(sdp);
134 wait_on_buffer(bh);
135 ll_rw_block(WRITE, 1, &bh);
136 gfs2_log_lock(sdp);
137
138 retry = 1;
139 break;
140 }
141 } while (retry);
142}
143
144/**
145 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
146 * @sdp: the filesystem
147 * @ai: the AIL entry
148 *
149 */
150
151int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
152{
153 struct gfs2_bufdata *bd, *s;
154 struct buffer_head *bh;
155
156 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
157 bd_ail_st_list) {
158 bh = bd->bd_bh;
159
160 gfs2_assert(sdp, bd->bd_ail == ai);
161
162 if (buffer_busy(bh)) {
163 if (flags & DIO_ALL)
164 continue;
165 else
166 break;
167 }
168
169 if (!buffer_uptodate(bh))
170 gfs2_io_error_bh(sdp, bh);
171
172 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
173 }
174
175 return list_empty(&ai->ai_ail1_list);
176}
177
178/**
179 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
180 * @sdp: the filesystem
181 * @ai: the AIL entry
182 *
183 */
184
185void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
186{
187 struct list_head *head = &ai->ai_ail2_list;
188 struct gfs2_bufdata *bd;
189
190 while (!list_empty(head)) {
191 bd = list_entry(head->prev, struct gfs2_bufdata,
192 bd_ail_st_list);
193 gfs2_assert(sdp, bd->bd_ail == ai);
194 bd->bd_ail = NULL;
195 list_del(&bd->bd_ail_st_list);
196 list_del(&bd->bd_ail_gl_list);
197 atomic_dec(&bd->bd_gl->gl_ail_count);
198 brelse(bd->bd_bh);
199 }
200}
201
202/**
203 * ail_empty_gl - remove all buffers for a given lock from the AIL
204 * @gl: the glock
205 *
206 * None of the buffers should be dirty, locked, or pinned.
207 */
208
209void gfs2_ail_empty_gl(struct gfs2_glock *gl)
210{
211 struct gfs2_sbd *sdp = gl->gl_sbd;
212 unsigned int blocks;
213 struct list_head *head = &gl->gl_ail_list;
214 struct gfs2_bufdata *bd;
215 struct buffer_head *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400216 u64 blkno;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217 int error;
218
219 blocks = atomic_read(&gl->gl_ail_count);
220 if (!blocks)
221 return;
222
223 error = gfs2_trans_begin(sdp, 0, blocks);
224 if (gfs2_assert_withdraw(sdp, !error))
225 return;
226
227 gfs2_log_lock(sdp);
228 while (!list_empty(head)) {
229 bd = list_entry(head->next, struct gfs2_bufdata,
230 bd_ail_gl_list);
231 bh = bd->bd_bh;
232 blkno = bh->b_blocknr;
233 gfs2_assert_withdraw(sdp, !buffer_busy(bh));
234
235 bd->bd_ail = NULL;
236 list_del(&bd->bd_ail_st_list);
237 list_del(&bd->bd_ail_gl_list);
238 atomic_dec(&gl->gl_ail_count);
239 brelse(bh);
240 gfs2_log_unlock(sdp);
241
242 gfs2_trans_add_revoke(sdp, blkno);
243
244 gfs2_log_lock(sdp);
245 }
246 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
247 gfs2_log_unlock(sdp);
248
249 gfs2_trans_end(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400250 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251}
252
253/**
254 * gfs2_meta_inval - Invalidate all buffers associated with a glock
255 * @gl: the glock
256 *
257 */
258
259void gfs2_meta_inval(struct gfs2_glock *gl)
260{
261 struct gfs2_sbd *sdp = gl->gl_sbd;
262 struct inode *aspace = gl->gl_aspace;
263 struct address_space *mapping = gl->gl_aspace->i_mapping;
264
265 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
266
267 atomic_inc(&aspace->i_writecount);
268 truncate_inode_pages(mapping, 0);
269 atomic_dec(&aspace->i_writecount);
270
271 gfs2_assert_withdraw(sdp, !mapping->nrpages);
272}
273
274/**
275 * gfs2_meta_sync - Sync all buffers associated with a glock
276 * @gl: The glock
277 * @flags: DIO_START | DIO_WAIT
278 *
279 */
280
281void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
282{
283 struct address_space *mapping = gl->gl_aspace->i_mapping;
284 int error = 0;
285
286 if (flags & DIO_START)
287 filemap_fdatawrite(mapping);
288 if (!error && (flags & DIO_WAIT))
289 error = filemap_fdatawait(mapping);
290
291 if (error)
292 gfs2_io_error(gl->gl_sbd);
293}
294
295/**
296 * getbuf - Get a buffer with a given address space
297 * @sdp: the filesystem
298 * @aspace: the address space
299 * @blkno: the block number (filesystem scope)
300 * @create: 1 if the buffer should be created
301 *
302 * Returns: the buffer
303 */
304
305static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
Steven Whitehousecd915492006-09-04 12:49:07 -0400306 u64 blkno, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307{
308 struct page *page;
309 struct buffer_head *bh;
310 unsigned int shift;
311 unsigned long index;
312 unsigned int bufnum;
313
314 shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
315 index = blkno >> shift; /* convert block to page */
316 bufnum = blkno - (index << shift); /* block buf index within page */
317
318 if (create) {
319 for (;;) {
320 page = grab_cache_page(aspace->i_mapping, index);
321 if (page)
322 break;
323 yield();
324 }
325 } else {
326 page = find_lock_page(aspace->i_mapping, index);
327 if (!page)
328 return NULL;
329 }
330
331 if (!page_has_buffers(page))
332 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
333
334 /* Locate header for our buffer within our page */
335 for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
336 /* Do nothing */;
337 get_bh(bh);
338
339 if (!buffer_mapped(bh))
340 map_bh(bh, sdp->sd_vfs, blkno);
341
342 unlock_page(page);
343 mark_page_accessed(page);
344 page_cache_release(page);
345
346 return bh;
347}
348
349static void meta_prep_new(struct buffer_head *bh)
350{
351 struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
352
353 lock_buffer(bh);
354 clear_buffer_dirty(bh);
355 set_buffer_uptodate(bh);
356 unlock_buffer(bh);
357
358 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
359}
360
361/**
362 * gfs2_meta_new - Get a block
363 * @gl: The glock associated with this block
364 * @blkno: The block number
365 *
366 * Returns: The buffer
367 */
368
Steven Whitehousecd915492006-09-04 12:49:07 -0400369struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370{
371 struct buffer_head *bh;
372 bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
373 meta_prep_new(bh);
374 return bh;
375}
376
377/**
378 * gfs2_meta_read - Read a block from disk
379 * @gl: The glock covering the block
380 * @blkno: The block number
381 * @flags: flags to gfs2_dreread()
382 * @bhp: the place where the buffer is returned (NULL on failure)
383 *
384 * Returns: errno
385 */
386
Steven Whitehousecd915492006-09-04 12:49:07 -0400387int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 struct buffer_head **bhp)
389{
390 int error;
391
392 *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
393 error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags);
394 if (error)
395 brelse(*bhp);
396
397 return error;
398}
399
400/**
401 * gfs2_meta_reread - Reread a block from disk
402 * @sdp: the filesystem
403 * @bh: The block to read
404 * @flags: Flags that control the read
405 *
406 * Returns: errno
407 */
408
409int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
410{
411 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
412 return -EIO;
413
414 if (flags & DIO_FORCE)
415 clear_buffer_uptodate(bh);
416
417 if ((flags & DIO_START) && !buffer_uptodate(bh))
418 ll_rw_block(READ, 1, &bh);
419
420 if (flags & DIO_WAIT) {
421 wait_on_buffer(bh);
422
423 if (!buffer_uptodate(bh)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500424 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 if (tr && tr->tr_touched)
426 gfs2_io_error_bh(sdp, bh);
427 return -EIO;
428 }
429 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
430 return -EIO;
431 }
432
433 return 0;
434}
435
436/**
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000437 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438 * @gl: the glock the buffer belongs to
439 * @bh: The buffer to be attached to
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000440 * @meta: Flag to indicate whether its metadata or not
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441 */
442
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500443void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
444 int meta)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445{
446 struct gfs2_bufdata *bd;
447
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000448 if (meta)
449 lock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000450
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500451 if (bh->b_private) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000452 if (meta)
453 unlock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454 return;
455 }
456
Steven Whitehousef55ab262006-02-21 12:51:39 +0000457 bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 memset(bd, 0, sizeof(struct gfs2_bufdata));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 bd->bd_bh = bh;
460 bd->bd_gl = gl;
461
462 INIT_LIST_HEAD(&bd->bd_list_tr);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400463 if (meta)
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000464 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400465 else
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000466 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500467 bh->b_private = bd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000469 if (meta)
470 unlock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471}
472
473/**
Steven Whitehousea98ab222006-01-18 13:38:44 +0000474 * gfs2_pin - Pin a buffer in memory
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 * @sdp: the filesystem the buffer belongs to
476 * @bh: The buffer to be pinned
477 *
478 */
479
Steven Whitehousea98ab222006-01-18 13:38:44 +0000480void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500482 struct gfs2_bufdata *bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483
484 gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
485
486 if (test_set_buffer_pinned(bh))
487 gfs2_assert_withdraw(sdp, 0);
488
489 wait_on_buffer(bh);
490
491 /* If this buffer is in the AIL and it has already been written
492 to in-place disk block, remove it from the AIL. */
493
494 gfs2_log_lock(sdp);
495 if (bd->bd_ail && !buffer_in_io(bh))
496 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
497 gfs2_log_unlock(sdp);
498
499 clear_buffer_dirty(bh);
500 wait_on_buffer(bh);
501
502 if (!buffer_uptodate(bh))
503 gfs2_io_error_bh(sdp, bh);
504
505 get_bh(bh);
506}
507
508/**
Steven Whitehousea98ab222006-01-18 13:38:44 +0000509 * gfs2_unpin - Unpin a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510 * @sdp: the filesystem the buffer belongs to
511 * @bh: The buffer to unpin
512 * @ai:
513 *
514 */
515
Steven Whitehousea98ab222006-01-18 13:38:44 +0000516void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
517 struct gfs2_ail *ai)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500519 struct gfs2_bufdata *bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520
521 gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
522
523 if (!buffer_pinned(bh))
524 gfs2_assert_withdraw(sdp, 0);
525
526 mark_buffer_dirty(bh);
527 clear_buffer_pinned(bh);
528
529 gfs2_log_lock(sdp);
530 if (bd->bd_ail) {
531 list_del(&bd->bd_ail_st_list);
532 brelse(bh);
533 } else {
534 struct gfs2_glock *gl = bd->bd_gl;
535 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
536 atomic_inc(&gl->gl_ail_count);
537 }
538 bd->bd_ail = ai;
539 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
540 gfs2_log_unlock(sdp);
541}
542
543/**
544 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
545 * @ip: the inode who owns the buffers
546 * @bstart: the first buffer in the run
547 * @blen: the number of buffers in the run
548 *
549 */
550
Steven Whitehousecd915492006-09-04 12:49:07 -0400551void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400553 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554 struct inode *aspace = ip->i_gl->gl_aspace;
555 struct buffer_head *bh;
556
557 while (blen) {
558 bh = getbuf(sdp, aspace, bstart, NO_CREATE);
559 if (bh) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500560 struct gfs2_bufdata *bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000561
562 if (test_clear_buffer_pinned(bh)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500563 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000564 gfs2_log_lock(sdp);
565 list_del_init(&bd->bd_le.le_list);
566 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
567 sdp->sd_log_num_buf--;
568 gfs2_log_unlock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500569 tr->tr_num_buf_rm++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570 brelse(bh);
571 }
572 if (bd) {
573 gfs2_log_lock(sdp);
574 if (bd->bd_ail) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400575 u64 blkno = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 bd->bd_ail = NULL;
577 list_del(&bd->bd_ail_st_list);
578 list_del(&bd->bd_ail_gl_list);
579 atomic_dec(&bd->bd_gl->gl_ail_count);
580 brelse(bh);
581 gfs2_log_unlock(sdp);
582 gfs2_trans_add_revoke(sdp, blkno);
583 } else
584 gfs2_log_unlock(sdp);
585 }
586
587 lock_buffer(bh);
588 clear_buffer_dirty(bh);
589 clear_buffer_uptodate(bh);
590 unlock_buffer(bh);
591
592 brelse(bh);
593 }
594
595 bstart++;
596 blen--;
597 }
598}
599
600/**
601 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
602 * @ip: The GFS2 inode
603 *
604 * This releases buffers that are in the most-recently-used array of
605 * blocks used for indirect block addressing for this inode.
606 */
607
608void gfs2_meta_cache_flush(struct gfs2_inode *ip)
609{
610 struct buffer_head **bh_slot;
611 unsigned int x;
612
613 spin_lock(&ip->i_spin);
614
615 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
616 bh_slot = &ip->i_cache[x];
617 if (!*bh_slot)
618 break;
619 brelse(*bh_slot);
620 *bh_slot = NULL;
621 }
622
623 spin_unlock(&ip->i_spin);
624}
625
626/**
627 * gfs2_meta_indirect_buffer - Get a metadata buffer
628 * @ip: The GFS2 inode
629 * @height: The level of this buf in the metadata (indir addr) tree (if any)
630 * @num: The block number (device relative) of the buffer
631 * @new: Non-zero if we may create a new buffer
632 * @bhp: the buffer is returned here
633 *
634 * Try to use the gfs2_inode's MRU metadata tree cache.
635 *
636 * Returns: errno
637 */
638
Steven Whitehousecd915492006-09-04 12:49:07 -0400639int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640 int new, struct buffer_head **bhp)
641{
642 struct buffer_head *bh, **bh_slot = ip->i_cache + height;
643 int error;
644
645 spin_lock(&ip->i_spin);
646 bh = *bh_slot;
647 if (bh) {
648 if (bh->b_blocknr == num)
649 get_bh(bh);
650 else
651 bh = NULL;
652 }
653 spin_unlock(&ip->i_spin);
654
655 if (bh) {
656 if (new)
657 meta_prep_new(bh);
658 else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400659 error = gfs2_meta_reread(GFS2_SB(&ip->i_inode), bh,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 DIO_START | DIO_WAIT);
661 if (error) {
662 brelse(bh);
663 return error;
664 }
665 }
666 } else {
667 if (new)
668 bh = gfs2_meta_new(ip->i_gl, num);
669 else {
670 error = gfs2_meta_read(ip->i_gl, num,
671 DIO_START | DIO_WAIT, &bh);
672 if (error)
673 return error;
674 }
675
676 spin_lock(&ip->i_spin);
677 if (*bh_slot != bh) {
678 brelse(*bh_slot);
679 *bh_slot = bh;
680 get_bh(bh);
681 }
682 spin_unlock(&ip->i_spin);
683 }
684
685 if (new) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400686 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), height)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 brelse(bh);
688 return -EIO;
689 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000690 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
692 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
693
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400694 } else if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695 (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
696 brelse(bh);
697 return -EIO;
698 }
699
700 *bhp = bh;
701
702 return 0;
703}
704
705/**
706 * gfs2_meta_ra - start readahead on an extent of a file
707 * @gl: the glock the blocks belong to
708 * @dblock: the starting disk block
709 * @extlen: the number of blocks in the extent
710 *
711 */
712
Steven Whitehousecd915492006-09-04 12:49:07 -0400713void gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714{
715 struct gfs2_sbd *sdp = gl->gl_sbd;
716 struct inode *aspace = gl->gl_aspace;
717 struct buffer_head *first_bh, *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400718 u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500719 sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720 int error;
721
722 if (!extlen || !max_ra)
723 return;
724 if (extlen > max_ra)
725 extlen = max_ra;
726
727 first_bh = getbuf(sdp, aspace, dblock, CREATE);
728
729 if (buffer_uptodate(first_bh))
730 goto out;
731 if (!buffer_locked(first_bh)) {
732 error = gfs2_meta_reread(sdp, first_bh, DIO_START);
733 if (error)
734 goto out;
735 }
736
737 dblock++;
738 extlen--;
739
740 while (extlen) {
741 bh = getbuf(sdp, aspace, dblock, CREATE);
742
743 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
744 error = gfs2_meta_reread(sdp, bh, DIO_START);
745 brelse(bh);
746 if (error)
747 goto out;
748 } else
749 brelse(bh);
750
751 dblock++;
752 extlen--;
753
754 if (buffer_uptodate(first_bh))
755 break;
756 }
757
Steven Whitehousea91ea692006-09-04 12:04:26 -0400758out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 brelse(first_bh);
760}
761
762/**
763 * gfs2_meta_syncfs - sync all the buffers in a filesystem
764 * @sdp: the filesystem
765 *
766 */
767
768void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
769{
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400770 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771 for (;;) {
772 gfs2_ail1_start(sdp, DIO_ALL);
773 if (gfs2_ail1_empty(sdp, DIO_ALL))
774 break;
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400775 msleep(10);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 }
777}
778