blob: 6b52aacb073624038bbc17d5b81b4f99f30c20a5 [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 }
Steven Whitehouse56965532006-09-20 15:48:09 -0400123 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000124 continue;
125 }
126
127 if (!buffer_dirty(bh))
128 continue;
129
130 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
131
132 gfs2_log_unlock(sdp);
133 wait_on_buffer(bh);
134 ll_rw_block(WRITE, 1, &bh);
135 gfs2_log_lock(sdp);
136
137 retry = 1;
138 break;
139 }
140 } while (retry);
141}
142
143/**
144 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
145 * @sdp: the filesystem
146 * @ai: the AIL entry
147 *
148 */
149
150int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
151{
152 struct gfs2_bufdata *bd, *s;
153 struct buffer_head *bh;
154
155 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
156 bd_ail_st_list) {
157 bh = bd->bd_bh;
158
159 gfs2_assert(sdp, bd->bd_ail == ai);
160
161 if (buffer_busy(bh)) {
162 if (flags & DIO_ALL)
163 continue;
164 else
165 break;
166 }
167
168 if (!buffer_uptodate(bh))
169 gfs2_io_error_bh(sdp, bh);
170
171 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
172 }
173
174 return list_empty(&ai->ai_ail1_list);
175}
176
177/**
178 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
179 * @sdp: the filesystem
180 * @ai: the AIL entry
181 *
182 */
183
184void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
185{
186 struct list_head *head = &ai->ai_ail2_list;
187 struct gfs2_bufdata *bd;
188
189 while (!list_empty(head)) {
190 bd = list_entry(head->prev, struct gfs2_bufdata,
191 bd_ail_st_list);
192 gfs2_assert(sdp, bd->bd_ail == ai);
193 bd->bd_ail = NULL;
194 list_del(&bd->bd_ail_st_list);
195 list_del(&bd->bd_ail_gl_list);
196 atomic_dec(&bd->bd_gl->gl_ail_count);
197 brelse(bd->bd_bh);
198 }
199}
200
201/**
202 * ail_empty_gl - remove all buffers for a given lock from the AIL
203 * @gl: the glock
204 *
205 * None of the buffers should be dirty, locked, or pinned.
206 */
207
208void gfs2_ail_empty_gl(struct gfs2_glock *gl)
209{
210 struct gfs2_sbd *sdp = gl->gl_sbd;
211 unsigned int blocks;
212 struct list_head *head = &gl->gl_ail_list;
213 struct gfs2_bufdata *bd;
214 struct buffer_head *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400215 u64 blkno;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 int error;
217
218 blocks = atomic_read(&gl->gl_ail_count);
219 if (!blocks)
220 return;
221
222 error = gfs2_trans_begin(sdp, 0, blocks);
223 if (gfs2_assert_withdraw(sdp, !error))
224 return;
225
226 gfs2_log_lock(sdp);
227 while (!list_empty(head)) {
228 bd = list_entry(head->next, struct gfs2_bufdata,
229 bd_ail_gl_list);
230 bh = bd->bd_bh;
231 blkno = bh->b_blocknr;
232 gfs2_assert_withdraw(sdp, !buffer_busy(bh));
233
234 bd->bd_ail = NULL;
235 list_del(&bd->bd_ail_st_list);
236 list_del(&bd->bd_ail_gl_list);
237 atomic_dec(&gl->gl_ail_count);
238 brelse(bh);
239 gfs2_log_unlock(sdp);
240
241 gfs2_trans_add_revoke(sdp, blkno);
242
243 gfs2_log_lock(sdp);
244 }
245 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
246 gfs2_log_unlock(sdp);
247
248 gfs2_trans_end(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400249 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250}
251
252/**
253 * gfs2_meta_inval - Invalidate all buffers associated with a glock
254 * @gl: the glock
255 *
256 */
257
258void gfs2_meta_inval(struct gfs2_glock *gl)
259{
260 struct gfs2_sbd *sdp = gl->gl_sbd;
261 struct inode *aspace = gl->gl_aspace;
262 struct address_space *mapping = gl->gl_aspace->i_mapping;
263
264 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
265
266 atomic_inc(&aspace->i_writecount);
267 truncate_inode_pages(mapping, 0);
268 atomic_dec(&aspace->i_writecount);
269
270 gfs2_assert_withdraw(sdp, !mapping->nrpages);
271}
272
273/**
274 * gfs2_meta_sync - Sync all buffers associated with a glock
275 * @gl: The glock
276 * @flags: DIO_START | DIO_WAIT
277 *
278 */
279
280void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
281{
282 struct address_space *mapping = gl->gl_aspace->i_mapping;
283 int error = 0;
284
285 if (flags & DIO_START)
286 filemap_fdatawrite(mapping);
287 if (!error && (flags & DIO_WAIT))
288 error = filemap_fdatawait(mapping);
289
290 if (error)
291 gfs2_io_error(gl->gl_sbd);
292}
293
294/**
295 * getbuf - Get a buffer with a given address space
296 * @sdp: the filesystem
297 * @aspace: the address space
298 * @blkno: the block number (filesystem scope)
299 * @create: 1 if the buffer should be created
300 *
301 * Returns: the buffer
302 */
303
304static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
Steven Whitehousecd915492006-09-04 12:49:07 -0400305 u64 blkno, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306{
307 struct page *page;
308 struct buffer_head *bh;
309 unsigned int shift;
310 unsigned long index;
311 unsigned int bufnum;
312
313 shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
314 index = blkno >> shift; /* convert block to page */
315 bufnum = blkno - (index << shift); /* block buf index within page */
316
317 if (create) {
318 for (;;) {
319 page = grab_cache_page(aspace->i_mapping, index);
320 if (page)
321 break;
322 yield();
323 }
324 } else {
325 page = find_lock_page(aspace->i_mapping, index);
326 if (!page)
327 return NULL;
328 }
329
330 if (!page_has_buffers(page))
331 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
332
333 /* Locate header for our buffer within our page */
334 for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
335 /* Do nothing */;
336 get_bh(bh);
337
338 if (!buffer_mapped(bh))
339 map_bh(bh, sdp->sd_vfs, blkno);
340
341 unlock_page(page);
342 mark_page_accessed(page);
343 page_cache_release(page);
344
345 return bh;
346}
347
348static void meta_prep_new(struct buffer_head *bh)
349{
350 struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
351
352 lock_buffer(bh);
353 clear_buffer_dirty(bh);
354 set_buffer_uptodate(bh);
355 unlock_buffer(bh);
356
357 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
358}
359
360/**
361 * gfs2_meta_new - Get a block
362 * @gl: The glock associated with this block
363 * @blkno: The block number
364 *
365 * Returns: The buffer
366 */
367
Steven Whitehousecd915492006-09-04 12:49:07 -0400368struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369{
370 struct buffer_head *bh;
371 bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
372 meta_prep_new(bh);
373 return bh;
374}
375
376/**
377 * gfs2_meta_read - Read a block from disk
378 * @gl: The glock covering the block
379 * @blkno: The block number
380 * @flags: flags to gfs2_dreread()
381 * @bhp: the place where the buffer is returned (NULL on failure)
382 *
383 * Returns: errno
384 */
385
Steven Whitehousecd915492006-09-04 12:49:07 -0400386int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 struct buffer_head **bhp)
388{
389 int error;
390
391 *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
392 error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags);
393 if (error)
394 brelse(*bhp);
395
396 return error;
397}
398
399/**
400 * gfs2_meta_reread - Reread a block from disk
401 * @sdp: the filesystem
402 * @bh: The block to read
403 * @flags: Flags that control the read
404 *
405 * Returns: errno
406 */
407
408int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
409{
410 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
411 return -EIO;
412
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413 if ((flags & DIO_START) && !buffer_uptodate(bh))
414 ll_rw_block(READ, 1, &bh);
415
416 if (flags & DIO_WAIT) {
417 wait_on_buffer(bh);
418
419 if (!buffer_uptodate(bh)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500420 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421 if (tr && tr->tr_touched)
422 gfs2_io_error_bh(sdp, bh);
423 return -EIO;
424 }
425 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
426 return -EIO;
427 }
428
429 return 0;
430}
431
432/**
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000433 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000434 * @gl: the glock the buffer belongs to
435 * @bh: The buffer to be attached to
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000436 * @meta: Flag to indicate whether its metadata or not
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437 */
438
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500439void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
440 int meta)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441{
442 struct gfs2_bufdata *bd;
443
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000444 if (meta)
445 lock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500447 if (bh->b_private) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000448 if (meta)
449 unlock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000450 return;
451 }
452
Steven Whitehousef55ab262006-02-21 12:51:39 +0000453 bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454 memset(bd, 0, sizeof(struct gfs2_bufdata));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455 bd->bd_bh = bh;
456 bd->bd_gl = gl;
457
458 INIT_LIST_HEAD(&bd->bd_list_tr);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400459 if (meta)
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000460 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400461 else
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000462 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500463 bh->b_private = bd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000465 if (meta)
466 unlock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467}
468
469/**
Steven Whitehousea98ab222006-01-18 13:38:44 +0000470 * gfs2_pin - Pin a buffer in memory
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471 * @sdp: the filesystem the buffer belongs to
472 * @bh: The buffer to be pinned
473 *
474 */
475
Steven Whitehousea98ab222006-01-18 13:38:44 +0000476void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500478 struct gfs2_bufdata *bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479
480 gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
481
482 if (test_set_buffer_pinned(bh))
483 gfs2_assert_withdraw(sdp, 0);
484
485 wait_on_buffer(bh);
486
487 /* If this buffer is in the AIL and it has already been written
488 to in-place disk block, remove it from the AIL. */
489
490 gfs2_log_lock(sdp);
491 if (bd->bd_ail && !buffer_in_io(bh))
492 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
493 gfs2_log_unlock(sdp);
494
495 clear_buffer_dirty(bh);
496 wait_on_buffer(bh);
497
498 if (!buffer_uptodate(bh))
499 gfs2_io_error_bh(sdp, bh);
500
501 get_bh(bh);
502}
503
504/**
Steven Whitehousea98ab222006-01-18 13:38:44 +0000505 * gfs2_unpin - Unpin a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000506 * @sdp: the filesystem the buffer belongs to
507 * @bh: The buffer to unpin
508 * @ai:
509 *
510 */
511
Steven Whitehousea98ab222006-01-18 13:38:44 +0000512void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
513 struct gfs2_ail *ai)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000514{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500515 struct gfs2_bufdata *bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516
517 gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
518
519 if (!buffer_pinned(bh))
520 gfs2_assert_withdraw(sdp, 0);
521
522 mark_buffer_dirty(bh);
523 clear_buffer_pinned(bh);
524
525 gfs2_log_lock(sdp);
526 if (bd->bd_ail) {
527 list_del(&bd->bd_ail_st_list);
528 brelse(bh);
529 } else {
530 struct gfs2_glock *gl = bd->bd_gl;
531 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
532 atomic_inc(&gl->gl_ail_count);
533 }
534 bd->bd_ail = ai;
535 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
536 gfs2_log_unlock(sdp);
537}
538
539/**
540 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
541 * @ip: the inode who owns the buffers
542 * @bstart: the first buffer in the run
543 * @blen: the number of buffers in the run
544 *
545 */
546
Steven Whitehousecd915492006-09-04 12:49:07 -0400547void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400549 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550 struct inode *aspace = ip->i_gl->gl_aspace;
551 struct buffer_head *bh;
552
553 while (blen) {
554 bh = getbuf(sdp, aspace, bstart, NO_CREATE);
555 if (bh) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500556 struct gfs2_bufdata *bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557
558 if (test_clear_buffer_pinned(bh)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500559 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000560 gfs2_log_lock(sdp);
561 list_del_init(&bd->bd_le.le_list);
562 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
563 sdp->sd_log_num_buf--;
564 gfs2_log_unlock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500565 tr->tr_num_buf_rm++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 brelse(bh);
567 }
568 if (bd) {
569 gfs2_log_lock(sdp);
570 if (bd->bd_ail) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400571 u64 blkno = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572 bd->bd_ail = NULL;
573 list_del(&bd->bd_ail_st_list);
574 list_del(&bd->bd_ail_gl_list);
575 atomic_dec(&bd->bd_gl->gl_ail_count);
576 brelse(bh);
577 gfs2_log_unlock(sdp);
578 gfs2_trans_add_revoke(sdp, blkno);
579 } else
580 gfs2_log_unlock(sdp);
581 }
582
583 lock_buffer(bh);
584 clear_buffer_dirty(bh);
585 clear_buffer_uptodate(bh);
586 unlock_buffer(bh);
587
588 brelse(bh);
589 }
590
591 bstart++;
592 blen--;
593 }
594}
595
596/**
597 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
598 * @ip: The GFS2 inode
599 *
600 * This releases buffers that are in the most-recently-used array of
601 * blocks used for indirect block addressing for this inode.
602 */
603
604void gfs2_meta_cache_flush(struct gfs2_inode *ip)
605{
606 struct buffer_head **bh_slot;
607 unsigned int x;
608
609 spin_lock(&ip->i_spin);
610
611 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
612 bh_slot = &ip->i_cache[x];
613 if (!*bh_slot)
614 break;
615 brelse(*bh_slot);
616 *bh_slot = NULL;
617 }
618
619 spin_unlock(&ip->i_spin);
620}
621
622/**
623 * gfs2_meta_indirect_buffer - Get a metadata buffer
624 * @ip: The GFS2 inode
625 * @height: The level of this buf in the metadata (indir addr) tree (if any)
626 * @num: The block number (device relative) of the buffer
627 * @new: Non-zero if we may create a new buffer
628 * @bhp: the buffer is returned here
629 *
630 * Try to use the gfs2_inode's MRU metadata tree cache.
631 *
632 * Returns: errno
633 */
634
Steven Whitehousecd915492006-09-04 12:49:07 -0400635int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636 int new, struct buffer_head **bhp)
637{
638 struct buffer_head *bh, **bh_slot = ip->i_cache + height;
639 int error;
640
641 spin_lock(&ip->i_spin);
642 bh = *bh_slot;
643 if (bh) {
644 if (bh->b_blocknr == num)
645 get_bh(bh);
646 else
647 bh = NULL;
648 }
649 spin_unlock(&ip->i_spin);
650
651 if (bh) {
652 if (new)
653 meta_prep_new(bh);
654 else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400655 error = gfs2_meta_reread(GFS2_SB(&ip->i_inode), bh,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656 DIO_START | DIO_WAIT);
657 if (error) {
658 brelse(bh);
659 return error;
660 }
661 }
662 } else {
663 if (new)
664 bh = gfs2_meta_new(ip->i_gl, num);
665 else {
666 error = gfs2_meta_read(ip->i_gl, num,
667 DIO_START | DIO_WAIT, &bh);
668 if (error)
669 return error;
670 }
671
672 spin_lock(&ip->i_spin);
673 if (*bh_slot != bh) {
674 brelse(*bh_slot);
675 *bh_slot = bh;
676 get_bh(bh);
677 }
678 spin_unlock(&ip->i_spin);
679 }
680
681 if (new) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400682 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), height)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683 brelse(bh);
684 return -EIO;
685 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000686 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
688 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
689
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400690 } else if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
692 brelse(bh);
693 return -EIO;
694 }
695
696 *bhp = bh;
697
698 return 0;
699}
700
701/**
702 * gfs2_meta_ra - start readahead on an extent of a file
703 * @gl: the glock the blocks belong to
704 * @dblock: the starting disk block
705 * @extlen: the number of blocks in the extent
706 *
707 */
708
Steven Whitehousecd915492006-09-04 12:49:07 -0400709void gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710{
711 struct gfs2_sbd *sdp = gl->gl_sbd;
712 struct inode *aspace = gl->gl_aspace;
713 struct buffer_head *first_bh, *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400714 u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500715 sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716 int error;
717
718 if (!extlen || !max_ra)
719 return;
720 if (extlen > max_ra)
721 extlen = max_ra;
722
723 first_bh = getbuf(sdp, aspace, dblock, CREATE);
724
725 if (buffer_uptodate(first_bh))
726 goto out;
727 if (!buffer_locked(first_bh)) {
728 error = gfs2_meta_reread(sdp, first_bh, DIO_START);
729 if (error)
730 goto out;
731 }
732
733 dblock++;
734 extlen--;
735
736 while (extlen) {
737 bh = getbuf(sdp, aspace, dblock, CREATE);
738
739 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
740 error = gfs2_meta_reread(sdp, bh, DIO_START);
741 brelse(bh);
742 if (error)
743 goto out;
744 } else
745 brelse(bh);
746
747 dblock++;
748 extlen--;
749
750 if (buffer_uptodate(first_bh))
751 break;
752 }
753
Steven Whitehousea91ea692006-09-04 12:04:26 -0400754out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755 brelse(first_bh);
756}
757
758/**
759 * gfs2_meta_syncfs - sync all the buffers in a filesystem
760 * @sdp: the filesystem
761 *
762 */
763
764void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
765{
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400766 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 for (;;) {
768 gfs2_ail1_start(sdp, DIO_ALL);
769 if (gfs2_ail1_empty(sdp, DIO_ALL))
770 break;
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400771 msleep(10);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772 }
773}
774