blob: d0d981dac4cd4340833835c9d8be916073fc3910 [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
37#define buffer_busy(bh) \
38((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned)))
39#define buffer_in_io(bh) \
40((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock)))
41
42static int aspace_get_block(struct inode *inode, sector_t lblock,
43 struct buffer_head *bh_result, int create)
44{
Steven Whitehouse5c676f62006-02-27 17:23:27 -050045 gfs2_assert_warn(inode->i_sb->s_fs_info, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000046 return -EOPNOTSUPP;
47}
48
49static int gfs2_aspace_writepage(struct page *page,
50 struct writeback_control *wbc)
51{
52 return block_write_full_page(page, aspace_get_block, wbc);
53}
54
Steven Whitehouse66de0452006-07-03 13:37:30 -040055static const struct address_space_operations aspace_aops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +000056 .writepage = gfs2_aspace_writepage,
Steven Whitehouse4340fe62006-07-11 09:46:33 -040057 .releasepage = gfs2_releasepage,
David Teiglandb3b94fa2006-01-16 16:50:04 +000058};
59
60/**
61 * gfs2_aspace_get - Create and initialize a struct inode structure
62 * @sdp: the filesystem the aspace is in
63 *
64 * Right now a struct inode is just a struct inode. Maybe Linux
65 * will supply a more lightweight address space construct (that works)
66 * in the future.
67 *
68 * Make sure pages/buffers in this aspace aren't in high memory.
69 *
70 * Returns: the aspace
71 */
72
73struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
74{
75 struct inode *aspace;
76
77 aspace = new_inode(sdp->sd_vfs);
78 if (aspace) {
Steven Whitehousef3bba032006-07-11 09:50:54 -040079 mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +000080 aspace->i_mapping->a_ops = &aspace_aops;
81 aspace->i_size = ~0ULL;
Theodore Ts'obba9dfd2006-09-27 01:50:31 -070082 aspace->i_private = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000083 insert_inode_hash(aspace);
84 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000085 return aspace;
86}
87
88void gfs2_aspace_put(struct inode *aspace)
89{
90 remove_inode_hash(aspace);
91 iput(aspace);
92}
93
94/**
95 * gfs2_ail1_start_one - Start I/O on a part of the AIL
96 * @sdp: the filesystem
97 * @tr: the part of the AIL
98 *
99 */
100
101void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
102{
103 struct gfs2_bufdata *bd, *s;
104 struct buffer_head *bh;
105 int retry;
106
Steven Whitehouse190562b2006-04-20 16:57:23 -0400107 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
108
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 do {
110 retry = 0;
111
112 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
113 bd_ail_st_list) {
114 bh = bd->bd_bh;
115
116 gfs2_assert(sdp, bd->bd_ail == ai);
117
118 if (!buffer_busy(bh)) {
Steven Whitehouse1b502592006-05-18 14:10:52 -0400119 if (!buffer_uptodate(bh)) {
120 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121 gfs2_io_error_bh(sdp, bh);
Steven Whitehouse1b502592006-05-18 14:10:52 -0400122 gfs2_log_lock(sdp);
123 }
Steven Whitehouse56965532006-09-20 15:48:09 -0400124 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125 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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277 *
278 */
279
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400280void gfs2_meta_sync(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281{
282 struct address_space *mapping = gl->gl_aspace->i_mapping;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400283 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000284
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400285 filemap_fdatawrite(mapping);
286 error = filemap_fdatawait(mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287
288 if (error)
289 gfs2_io_error(gl->gl_sbd);
290}
291
292/**
293 * getbuf - Get a buffer with a given address space
294 * @sdp: the filesystem
295 * @aspace: the address space
296 * @blkno: the block number (filesystem scope)
297 * @create: 1 if the buffer should be created
298 *
299 * Returns: the buffer
300 */
301
302static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
Steven Whitehousecd915492006-09-04 12:49:07 -0400303 u64 blkno, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304{
305 struct page *page;
306 struct buffer_head *bh;
307 unsigned int shift;
308 unsigned long index;
309 unsigned int bufnum;
310
311 shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
312 index = blkno >> shift; /* convert block to page */
313 bufnum = blkno - (index << shift); /* block buf index within page */
314
315 if (create) {
316 for (;;) {
317 page = grab_cache_page(aspace->i_mapping, index);
318 if (page)
319 break;
320 yield();
321 }
322 } else {
323 page = find_lock_page(aspace->i_mapping, index);
324 if (!page)
325 return NULL;
326 }
327
328 if (!page_has_buffers(page))
329 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
330
331 /* Locate header for our buffer within our page */
332 for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
333 /* Do nothing */;
334 get_bh(bh);
335
336 if (!buffer_mapped(bh))
337 map_bh(bh, sdp->sd_vfs, blkno);
338
339 unlock_page(page);
340 mark_page_accessed(page);
341 page_cache_release(page);
342
343 return bh;
344}
345
346static void meta_prep_new(struct buffer_head *bh)
347{
348 struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
349
350 lock_buffer(bh);
351 clear_buffer_dirty(bh);
352 set_buffer_uptodate(bh);
353 unlock_buffer(bh);
354
355 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
356}
357
358/**
359 * gfs2_meta_new - Get a block
360 * @gl: The glock associated with this block
361 * @blkno: The block number
362 *
363 * Returns: The buffer
364 */
365
Steven Whitehousecd915492006-09-04 12:49:07 -0400366struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367{
368 struct buffer_head *bh;
369 bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
370 meta_prep_new(bh);
371 return bh;
372}
373
374/**
375 * gfs2_meta_read - Read a block from disk
376 * @gl: The glock covering the block
377 * @blkno: The block number
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400378 * @flags: flags
David Teiglandb3b94fa2006-01-16 16:50:04 +0000379 * @bhp: the place where the buffer is returned (NULL on failure)
380 *
381 * Returns: errno
382 */
383
Steven Whitehousecd915492006-09-04 12:49:07 -0400384int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385 struct buffer_head **bhp)
386{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400388 if (!buffer_uptodate(*bhp))
Steven Whitehouse2e565bb2006-10-02 11:38:25 -0400389 ll_rw_block(READ_META, 1, bhp);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400390 if (flags & DIO_WAIT) {
391 int error = gfs2_meta_wait(gl->gl_sbd, *bhp);
392 if (error) {
393 brelse(*bhp);
394 return error;
395 }
396 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400398 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000399}
400
401/**
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400402 * gfs2_meta_wait - Reread a block from disk
David Teiglandb3b94fa2006-01-16 16:50:04 +0000403 * @sdp: the filesystem
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400404 * @bh: The block to wait for
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405 *
406 * Returns: errno
407 */
408
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400409int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000410{
411 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
412 return -EIO;
413
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400414 wait_on_buffer(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400416 if (!buffer_uptodate(bh)) {
417 struct gfs2_trans *tr = current->journal_info;
418 if (tr && tr->tr_touched)
419 gfs2_io_error_bh(sdp, bh);
420 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400422 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
423 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424
425 return 0;
426}
427
428/**
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000429 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430 * @gl: the glock the buffer belongs to
431 * @bh: The buffer to be attached to
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000432 * @meta: Flag to indicate whether its metadata or not
David Teiglandb3b94fa2006-01-16 16:50:04 +0000433 */
434
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500435void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
436 int meta)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437{
438 struct gfs2_bufdata *bd;
439
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000440 if (meta)
441 lock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500443 if (bh->b_private) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000444 if (meta)
445 unlock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446 return;
447 }
448
Steven Whitehousef55ab262006-02-21 12:51:39 +0000449 bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000450 memset(bd, 0, sizeof(struct gfs2_bufdata));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451 bd->bd_bh = bh;
452 bd->bd_gl = gl;
453
454 INIT_LIST_HEAD(&bd->bd_list_tr);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400455 if (meta)
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000456 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400457 else
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000458 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500459 bh->b_private = bd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000461 if (meta)
462 unlock_page(bh->b_page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000463}
464
465/**
Steven Whitehousea98ab222006-01-18 13:38:44 +0000466 * gfs2_pin - Pin a buffer in memory
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467 * @sdp: the filesystem the buffer belongs to
468 * @bh: The buffer to be pinned
469 *
470 */
471
Steven Whitehousea98ab222006-01-18 13:38:44 +0000472void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500474 struct gfs2_bufdata *bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475
476 gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
477
478 if (test_set_buffer_pinned(bh))
479 gfs2_assert_withdraw(sdp, 0);
480
481 wait_on_buffer(bh);
482
483 /* If this buffer is in the AIL and it has already been written
484 to in-place disk block, remove it from the AIL. */
485
486 gfs2_log_lock(sdp);
487 if (bd->bd_ail && !buffer_in_io(bh))
488 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
489 gfs2_log_unlock(sdp);
490
491 clear_buffer_dirty(bh);
492 wait_on_buffer(bh);
493
494 if (!buffer_uptodate(bh))
495 gfs2_io_error_bh(sdp, bh);
496
497 get_bh(bh);
498}
499
500/**
Steven Whitehousea98ab222006-01-18 13:38:44 +0000501 * gfs2_unpin - Unpin a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502 * @sdp: the filesystem the buffer belongs to
503 * @bh: The buffer to unpin
504 * @ai:
505 *
506 */
507
Steven Whitehousea98ab222006-01-18 13:38:44 +0000508void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
509 struct gfs2_ail *ai)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500511 struct gfs2_bufdata *bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512
513 gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
514
515 if (!buffer_pinned(bh))
516 gfs2_assert_withdraw(sdp, 0);
517
518 mark_buffer_dirty(bh);
519 clear_buffer_pinned(bh);
520
521 gfs2_log_lock(sdp);
522 if (bd->bd_ail) {
523 list_del(&bd->bd_ail_st_list);
524 brelse(bh);
525 } else {
526 struct gfs2_glock *gl = bd->bd_gl;
527 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
528 atomic_inc(&gl->gl_ail_count);
529 }
530 bd->bd_ail = ai;
531 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
532 gfs2_log_unlock(sdp);
533}
534
535/**
536 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
537 * @ip: the inode who owns the buffers
538 * @bstart: the first buffer in the run
539 * @blen: the number of buffers in the run
540 *
541 */
542
Steven Whitehousecd915492006-09-04 12:49:07 -0400543void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400545 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546 struct inode *aspace = ip->i_gl->gl_aspace;
547 struct buffer_head *bh;
548
549 while (blen) {
550 bh = getbuf(sdp, aspace, bstart, NO_CREATE);
551 if (bh) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500552 struct gfs2_bufdata *bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553
554 if (test_clear_buffer_pinned(bh)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500555 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556 gfs2_log_lock(sdp);
557 list_del_init(&bd->bd_le.le_list);
558 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
559 sdp->sd_log_num_buf--;
560 gfs2_log_unlock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500561 tr->tr_num_buf_rm++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 brelse(bh);
563 }
564 if (bd) {
565 gfs2_log_lock(sdp);
566 if (bd->bd_ail) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400567 u64 blkno = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 bd->bd_ail = NULL;
569 list_del(&bd->bd_ail_st_list);
570 list_del(&bd->bd_ail_gl_list);
571 atomic_dec(&bd->bd_gl->gl_ail_count);
572 brelse(bh);
573 gfs2_log_unlock(sdp);
574 gfs2_trans_add_revoke(sdp, blkno);
575 } else
576 gfs2_log_unlock(sdp);
577 }
578
579 lock_buffer(bh);
580 clear_buffer_dirty(bh);
581 clear_buffer_uptodate(bh);
582 unlock_buffer(bh);
583
584 brelse(bh);
585 }
586
587 bstart++;
588 blen--;
589 }
590}
591
592/**
593 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
594 * @ip: The GFS2 inode
595 *
596 * This releases buffers that are in the most-recently-used array of
597 * blocks used for indirect block addressing for this inode.
598 */
599
600void gfs2_meta_cache_flush(struct gfs2_inode *ip)
601{
602 struct buffer_head **bh_slot;
603 unsigned int x;
604
605 spin_lock(&ip->i_spin);
606
607 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
608 bh_slot = &ip->i_cache[x];
609 if (!*bh_slot)
610 break;
611 brelse(*bh_slot);
612 *bh_slot = NULL;
613 }
614
615 spin_unlock(&ip->i_spin);
616}
617
618/**
619 * gfs2_meta_indirect_buffer - Get a metadata buffer
620 * @ip: The GFS2 inode
621 * @height: The level of this buf in the metadata (indir addr) tree (if any)
622 * @num: The block number (device relative) of the buffer
623 * @new: Non-zero if we may create a new buffer
624 * @bhp: the buffer is returned here
625 *
626 * Try to use the gfs2_inode's MRU metadata tree cache.
627 *
628 * Returns: errno
629 */
630
Steven Whitehousecd915492006-09-04 12:49:07 -0400631int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632 int new, struct buffer_head **bhp)
633{
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400634 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
635 struct gfs2_glock *gl = ip->i_gl;
636 struct buffer_head *bh = NULL, **bh_slot = ip->i_cache + height;
637 int in_cache = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638
639 spin_lock(&ip->i_spin);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400640 if (*bh_slot && (*bh_slot)->b_blocknr == num) {
641 bh = *bh_slot;
642 get_bh(bh);
643 in_cache = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644 }
645 spin_unlock(&ip->i_spin);
646
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400647 if (!bh)
648 bh = getbuf(gl->gl_sbd, gl->gl_aspace, num, CREATE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000649
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400650 if (!bh)
651 return -ENOBUFS;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652
653 if (new) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400654 if (gfs2_assert_warn(sdp, height))
655 goto err;
656 meta_prep_new(bh);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000657 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
659 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400660 } else {
661 u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
662 if (!buffer_uptodate(bh)) {
Steven Whitehouse2e565bb2006-10-02 11:38:25 -0400663 ll_rw_block(READ_META, 1, &bh);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400664 if (gfs2_meta_wait(sdp, bh))
665 goto err;
666 }
667 if (gfs2_metatype_check(sdp, bh, mtype))
668 goto err;
669 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400671 if (!in_cache) {
672 spin_lock(&ip->i_spin);
673 if (*bh_slot)
674 brelse(*bh_slot);
675 *bh_slot = bh;
676 get_bh(bh);
677 spin_unlock(&ip->i_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 }
679
680 *bhp = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681 return 0;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400682err:
683 brelse(bh);
684 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685}
686
687/**
688 * gfs2_meta_ra - start readahead on an extent of a file
689 * @gl: the glock the blocks belong to
690 * @dblock: the starting disk block
691 * @extlen: the number of blocks in the extent
692 *
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400693 * returns: the first buffer in the extent
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 */
695
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400696struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697{
698 struct gfs2_sbd *sdp = gl->gl_sbd;
699 struct inode *aspace = gl->gl_aspace;
700 struct buffer_head *first_bh, *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400701 u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500702 sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000703
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400704 BUG_ON(!extlen);
705
706 if (max_ra < 1)
707 max_ra = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708 if (extlen > max_ra)
709 extlen = max_ra;
710
711 first_bh = getbuf(sdp, aspace, dblock, CREATE);
712
713 if (buffer_uptodate(first_bh))
714 goto out;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400715 if (!buffer_locked(first_bh))
Steven Whitehouse2e565bb2006-10-02 11:38:25 -0400716 ll_rw_block(READ_META, 1, &first_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717
718 dblock++;
719 extlen--;
720
721 while (extlen) {
722 bh = getbuf(sdp, aspace, dblock, CREATE);
723
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400724 if (!buffer_uptodate(bh) && !buffer_locked(bh))
725 ll_rw_block(READA, 1, &bh);
726 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727 dblock++;
728 extlen--;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400729 if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
730 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000731 }
732
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400733 wait_on_buffer(first_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400734out:
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400735 return first_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736}
737
738/**
739 * gfs2_meta_syncfs - sync all the buffers in a filesystem
740 * @sdp: the filesystem
741 *
742 */
743
744void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
745{
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400746 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747 for (;;) {
748 gfs2_ail1_start(sdp, DIO_ALL);
749 if (gfs2_ail1_empty(sdp, DIO_ALL))
750 break;
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400751 msleep(10);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752 }
753}
754