[GFS2] Change all types to uX style
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / gfs2 / meta_io.c
blob5b10379c20ebb5ee3f2fe1d4dd9b6d6082b8b32c
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
8 */
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>
20 #include <linux/gfs2_ondisk.h>
22 #include "gfs2.h"
23 #include "lm_interface.h"
24 #include "incore.h"
25 #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"
33 #include "util.h"
34 #include "ops_address.h"
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)))
41 static int aspace_get_block(struct inode *inode, sector_t lblock,
42 struct buffer_head *bh_result, int create)
44 gfs2_assert_warn(inode->i_sb->s_fs_info, 0);
45 return -EOPNOTSUPP;
48 static int gfs2_aspace_writepage(struct page *page,
49 struct writeback_control *wbc)
51 return block_write_full_page(page, aspace_get_block, wbc);
54 static const struct address_space_operations aspace_aops = {
55 .writepage = gfs2_aspace_writepage,
56 .releasepage = gfs2_releasepage,
59 /**
60 * gfs2_aspace_get - Create and initialize a struct inode structure
61 * @sdp: the filesystem the aspace is in
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.
67 * Make sure pages/buffers in this aspace aren't in high memory.
69 * Returns: the aspace
72 struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
74 struct inode *aspace;
76 aspace = new_inode(sdp->sd_vfs);
77 if (aspace) {
78 mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS);
79 aspace->i_mapping->a_ops = &aspace_aops;
80 aspace->i_size = ~0ULL;
81 aspace->u.generic_ip = NULL;
82 insert_inode_hash(aspace);
84 return aspace;
87 void gfs2_aspace_put(struct inode *aspace)
89 remove_inode_hash(aspace);
90 iput(aspace);
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
100 void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
102 struct gfs2_bufdata *bd, *s;
103 struct buffer_head *bh;
104 int retry;
106 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
108 do {
109 retry = 0;
111 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
112 bd_ail_st_list) {
113 bh = bd->bd_bh;
115 gfs2_assert(sdp, bd->bd_ail == ai);
117 if (!buffer_busy(bh)) {
118 if (!buffer_uptodate(bh)) {
119 gfs2_log_unlock(sdp);
120 gfs2_io_error_bh(sdp, bh);
121 gfs2_log_lock(sdp);
123 list_move(&bd->bd_ail_st_list,
124 &ai->ai_ail2_list);
125 continue;
128 if (!buffer_dirty(bh))
129 continue;
131 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
133 gfs2_log_unlock(sdp);
134 wait_on_buffer(bh);
135 ll_rw_block(WRITE, 1, &bh);
136 gfs2_log_lock(sdp);
138 retry = 1;
139 break;
141 } while (retry);
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
151 int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
153 struct gfs2_bufdata *bd, *s;
154 struct buffer_head *bh;
156 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
157 bd_ail_st_list) {
158 bh = bd->bd_bh;
160 gfs2_assert(sdp, bd->bd_ail == ai);
162 if (buffer_busy(bh)) {
163 if (flags & DIO_ALL)
164 continue;
165 else
166 break;
169 if (!buffer_uptodate(bh))
170 gfs2_io_error_bh(sdp, bh);
172 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
175 return list_empty(&ai->ai_ail1_list);
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
185 void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
187 struct list_head *head = &ai->ai_ail2_list;
188 struct gfs2_bufdata *bd;
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);
203 * ail_empty_gl - remove all buffers for a given lock from the AIL
204 * @gl: the glock
206 * None of the buffers should be dirty, locked, or pinned.
209 void gfs2_ail_empty_gl(struct gfs2_glock *gl)
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;
216 u64 blkno;
217 int error;
219 blocks = atomic_read(&gl->gl_ail_count);
220 if (!blocks)
221 return;
223 error = gfs2_trans_begin(sdp, 0, blocks);
224 if (gfs2_assert_withdraw(sdp, !error))
225 return;
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));
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);
242 gfs2_trans_add_revoke(sdp, blkno);
244 gfs2_log_lock(sdp);
246 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
247 gfs2_log_unlock(sdp);
249 gfs2_trans_end(sdp);
250 gfs2_log_flush(sdp, NULL);
254 * gfs2_meta_inval - Invalidate all buffers associated with a glock
255 * @gl: the glock
259 void gfs2_meta_inval(struct gfs2_glock *gl)
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;
265 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
267 atomic_inc(&aspace->i_writecount);
268 truncate_inode_pages(mapping, 0);
269 atomic_dec(&aspace->i_writecount);
271 gfs2_assert_withdraw(sdp, !mapping->nrpages);
275 * gfs2_meta_sync - Sync all buffers associated with a glock
276 * @gl: The glock
277 * @flags: DIO_START | DIO_WAIT
281 void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
283 struct address_space *mapping = gl->gl_aspace->i_mapping;
284 int error = 0;
286 if (flags & DIO_START)
287 filemap_fdatawrite(mapping);
288 if (!error && (flags & DIO_WAIT))
289 error = filemap_fdatawait(mapping);
291 if (error)
292 gfs2_io_error(gl->gl_sbd);
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
302 * Returns: the buffer
305 static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
306 u64 blkno, int create)
308 struct page *page;
309 struct buffer_head *bh;
310 unsigned int shift;
311 unsigned long index;
312 unsigned int bufnum;
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 */
318 if (create) {
319 for (;;) {
320 page = grab_cache_page(aspace->i_mapping, index);
321 if (page)
322 break;
323 yield();
325 } else {
326 page = find_lock_page(aspace->i_mapping, index);
327 if (!page)
328 return NULL;
331 if (!page_has_buffers(page))
332 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
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);
339 if (!buffer_mapped(bh))
340 map_bh(bh, sdp->sd_vfs, blkno);
342 unlock_page(page);
343 mark_page_accessed(page);
344 page_cache_release(page);
346 return bh;
349 static void meta_prep_new(struct buffer_head *bh)
351 struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
353 lock_buffer(bh);
354 clear_buffer_dirty(bh);
355 set_buffer_uptodate(bh);
356 unlock_buffer(bh);
358 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
362 * gfs2_meta_new - Get a block
363 * @gl: The glock associated with this block
364 * @blkno: The block number
366 * Returns: The buffer
369 struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
371 struct buffer_head *bh;
372 bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
373 meta_prep_new(bh);
374 return bh;
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)
384 * Returns: errno
387 int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
388 struct buffer_head **bhp)
390 int error;
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);
397 return error;
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
406 * Returns: errno
409 int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
411 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
412 return -EIO;
414 if (flags & DIO_FORCE)
415 clear_buffer_uptodate(bh);
417 if ((flags & DIO_START) && !buffer_uptodate(bh))
418 ll_rw_block(READ, 1, &bh);
420 if (flags & DIO_WAIT) {
421 wait_on_buffer(bh);
423 if (!buffer_uptodate(bh)) {
424 struct gfs2_trans *tr = current->journal_info;
425 if (tr && tr->tr_touched)
426 gfs2_io_error_bh(sdp, bh);
427 return -EIO;
429 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
430 return -EIO;
433 return 0;
437 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
438 * @gl: the glock the buffer belongs to
439 * @bh: The buffer to be attached to
440 * @meta: Flag to indicate whether its metadata or not
443 void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
444 int meta)
446 struct gfs2_bufdata *bd;
448 if (meta)
449 lock_page(bh->b_page);
451 if (bh->b_private) {
452 if (meta)
453 unlock_page(bh->b_page);
454 return;
457 bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
458 memset(bd, 0, sizeof(struct gfs2_bufdata));
459 bd->bd_bh = bh;
460 bd->bd_gl = gl;
462 INIT_LIST_HEAD(&bd->bd_list_tr);
463 if (meta) {
464 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
465 } else {
466 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
468 bh->b_private = bd;
470 if (meta)
471 unlock_page(bh->b_page);
475 * gfs2_pin - Pin a buffer in memory
476 * @sdp: the filesystem the buffer belongs to
477 * @bh: The buffer to be pinned
481 void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
483 struct gfs2_bufdata *bd = bh->b_private;
485 gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
487 if (test_set_buffer_pinned(bh))
488 gfs2_assert_withdraw(sdp, 0);
490 wait_on_buffer(bh);
492 /* If this buffer is in the AIL and it has already been written
493 to in-place disk block, remove it from the AIL. */
495 gfs2_log_lock(sdp);
496 if (bd->bd_ail && !buffer_in_io(bh))
497 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
498 gfs2_log_unlock(sdp);
500 clear_buffer_dirty(bh);
501 wait_on_buffer(bh);
503 if (!buffer_uptodate(bh))
504 gfs2_io_error_bh(sdp, bh);
506 get_bh(bh);
510 * gfs2_unpin - Unpin a buffer
511 * @sdp: the filesystem the buffer belongs to
512 * @bh: The buffer to unpin
513 * @ai:
517 void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
518 struct gfs2_ail *ai)
520 struct gfs2_bufdata *bd = bh->b_private;
522 gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
524 if (!buffer_pinned(bh))
525 gfs2_assert_withdraw(sdp, 0);
527 mark_buffer_dirty(bh);
528 clear_buffer_pinned(bh);
530 gfs2_log_lock(sdp);
531 if (bd->bd_ail) {
532 list_del(&bd->bd_ail_st_list);
533 brelse(bh);
534 } else {
535 struct gfs2_glock *gl = bd->bd_gl;
536 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
537 atomic_inc(&gl->gl_ail_count);
539 bd->bd_ail = ai;
540 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
541 gfs2_log_unlock(sdp);
545 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
546 * @ip: the inode who owns the buffers
547 * @bstart: the first buffer in the run
548 * @blen: the number of buffers in the run
552 void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
554 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
555 struct inode *aspace = ip->i_gl->gl_aspace;
556 struct buffer_head *bh;
558 while (blen) {
559 bh = getbuf(sdp, aspace, bstart, NO_CREATE);
560 if (bh) {
561 struct gfs2_bufdata *bd = bh->b_private;
563 if (test_clear_buffer_pinned(bh)) {
564 struct gfs2_trans *tr = current->journal_info;
565 gfs2_log_lock(sdp);
566 list_del_init(&bd->bd_le.le_list);
567 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
568 sdp->sd_log_num_buf--;
569 gfs2_log_unlock(sdp);
570 tr->tr_num_buf_rm++;
571 brelse(bh);
573 if (bd) {
574 gfs2_log_lock(sdp);
575 if (bd->bd_ail) {
576 u64 blkno = bh->b_blocknr;
577 bd->bd_ail = NULL;
578 list_del(&bd->bd_ail_st_list);
579 list_del(&bd->bd_ail_gl_list);
580 atomic_dec(&bd->bd_gl->gl_ail_count);
581 brelse(bh);
582 gfs2_log_unlock(sdp);
583 gfs2_trans_add_revoke(sdp, blkno);
584 } else
585 gfs2_log_unlock(sdp);
588 lock_buffer(bh);
589 clear_buffer_dirty(bh);
590 clear_buffer_uptodate(bh);
591 unlock_buffer(bh);
593 brelse(bh);
596 bstart++;
597 blen--;
602 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
603 * @ip: The GFS2 inode
605 * This releases buffers that are in the most-recently-used array of
606 * blocks used for indirect block addressing for this inode.
609 void gfs2_meta_cache_flush(struct gfs2_inode *ip)
611 struct buffer_head **bh_slot;
612 unsigned int x;
614 spin_lock(&ip->i_spin);
616 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
617 bh_slot = &ip->i_cache[x];
618 if (!*bh_slot)
619 break;
620 brelse(*bh_slot);
621 *bh_slot = NULL;
624 spin_unlock(&ip->i_spin);
628 * gfs2_meta_indirect_buffer - Get a metadata buffer
629 * @ip: The GFS2 inode
630 * @height: The level of this buf in the metadata (indir addr) tree (if any)
631 * @num: The block number (device relative) of the buffer
632 * @new: Non-zero if we may create a new buffer
633 * @bhp: the buffer is returned here
635 * Try to use the gfs2_inode's MRU metadata tree cache.
637 * Returns: errno
640 int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
641 int new, struct buffer_head **bhp)
643 struct buffer_head *bh, **bh_slot = ip->i_cache + height;
644 int error;
646 spin_lock(&ip->i_spin);
647 bh = *bh_slot;
648 if (bh) {
649 if (bh->b_blocknr == num)
650 get_bh(bh);
651 else
652 bh = NULL;
654 spin_unlock(&ip->i_spin);
656 if (bh) {
657 if (new)
658 meta_prep_new(bh);
659 else {
660 error = gfs2_meta_reread(GFS2_SB(&ip->i_inode), bh,
661 DIO_START | DIO_WAIT);
662 if (error) {
663 brelse(bh);
664 return error;
667 } else {
668 if (new)
669 bh = gfs2_meta_new(ip->i_gl, num);
670 else {
671 error = gfs2_meta_read(ip->i_gl, num,
672 DIO_START | DIO_WAIT, &bh);
673 if (error)
674 return error;
677 spin_lock(&ip->i_spin);
678 if (*bh_slot != bh) {
679 brelse(*bh_slot);
680 *bh_slot = bh;
681 get_bh(bh);
683 spin_unlock(&ip->i_spin);
686 if (new) {
687 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), height)) {
688 brelse(bh);
689 return -EIO;
691 gfs2_trans_add_bh(ip->i_gl, bh, 1);
692 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
693 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
695 } else if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh,
696 (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
697 brelse(bh);
698 return -EIO;
701 *bhp = bh;
703 return 0;
707 * gfs2_meta_ra - start readahead on an extent of a file
708 * @gl: the glock the blocks belong to
709 * @dblock: the starting disk block
710 * @extlen: the number of blocks in the extent
714 void gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
716 struct gfs2_sbd *sdp = gl->gl_sbd;
717 struct inode *aspace = gl->gl_aspace;
718 struct buffer_head *first_bh, *bh;
719 u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
720 sdp->sd_sb.sb_bsize_shift;
721 int error;
723 if (!extlen || !max_ra)
724 return;
725 if (extlen > max_ra)
726 extlen = max_ra;
728 first_bh = getbuf(sdp, aspace, dblock, CREATE);
730 if (buffer_uptodate(first_bh))
731 goto out;
732 if (!buffer_locked(first_bh)) {
733 error = gfs2_meta_reread(sdp, first_bh, DIO_START);
734 if (error)
735 goto out;
738 dblock++;
739 extlen--;
741 while (extlen) {
742 bh = getbuf(sdp, aspace, dblock, CREATE);
744 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
745 error = gfs2_meta_reread(sdp, bh, DIO_START);
746 brelse(bh);
747 if (error)
748 goto out;
749 } else
750 brelse(bh);
752 dblock++;
753 extlen--;
755 if (buffer_uptodate(first_bh))
756 break;
759 out:
760 brelse(first_bh);
764 * gfs2_meta_syncfs - sync all the buffers in a filesystem
765 * @sdp: the filesystem
769 void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
771 gfs2_log_flush(sdp, NULL);
772 for (;;) {
773 gfs2_ail1_start(sdp, DIO_ALL);
774 if (gfs2_ail1_empty(sdp, DIO_ALL))
775 break;
776 msleep(10);