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 v.2.
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>
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>
23 #include "lm_interface.h"
35 #define buffer_busy(bh) \
36 ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned)))
37 #define buffer_in_io(bh) \
38 ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock)))
40 static int aspace_get_block(struct inode
*inode
, sector_t lblock
,
41 struct buffer_head
*bh_result
, int create
)
43 gfs2_assert_warn(inode
->i_sb
->s_fs_info
, 0);
47 static int gfs2_aspace_writepage(struct page
*page
,
48 struct writeback_control
*wbc
)
50 return block_write_full_page(page
, aspace_get_block
, wbc
);
54 * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out.
55 * @bh: the buffer we're stuck on
59 static void stuck_releasepage(struct buffer_head
*bh
)
61 struct inode
*inode
= bh
->b_page
->mapping
->host
;
62 struct gfs2_sbd
*sdp
= inode
->i_sb
->s_fs_info
;
63 struct gfs2_bufdata
*bd
= bh
->b_private
;
64 struct gfs2_glock
*gl
;
66 fs_warn(sdp
, "stuck in gfs2_releasepage() %p\n", inode
);
67 fs_warn(sdp
, "blkno = %llu, bh->b_count = %d\n",
68 (unsigned long long)bh
->b_blocknr
, atomic_read(&bh
->b_count
));
69 fs_warn(sdp
, "pinned = %u\n", buffer_pinned(bh
));
70 fs_warn(sdp
, "bh->b_private = %s\n", (bd
) ? "!NULL" : "NULL");
77 fs_warn(sdp
, "gl = (%u, %llu)\n",
78 gl
->gl_name
.ln_type
, (unsigned long long)gl
->gl_name
.ln_number
);
80 fs_warn(sdp
, "bd_list_tr = %s, bd_le.le_list = %s\n",
81 (list_empty(&bd
->bd_list_tr
)) ? "no" : "yes",
82 (list_empty(&bd
->bd_le
.le_list
)) ? "no" : "yes");
84 if (gl
->gl_ops
== &gfs2_inode_glops
) {
85 struct gfs2_inode
*ip
= gl
->gl_object
;
91 fs_warn(sdp
, "ip = %llu %llu\n",
92 (unsigned long long)ip
->i_num
.no_formal_ino
,
93 (unsigned long long)ip
->i_num
.no_addr
);
95 for (x
= 0; x
< GFS2_MAX_META_HEIGHT
; x
++)
96 fs_warn(sdp
, "ip->i_cache[%u] = %s\n",
97 x
, (ip
->i_cache
[x
]) ? "!NULL" : "NULL");
102 * gfs2_aspace_releasepage - free the metadata associated with a page
103 * @page: the page that's being released
104 * @gfp_mask: passed from Linux VFS, ignored by us
106 * Call try_to_free_buffers() if the buffers in this page can be
112 static int gfs2_aspace_releasepage(struct page
*page
, gfp_t gfp_mask
)
114 struct inode
*aspace
= page
->mapping
->host
;
115 struct gfs2_sbd
*sdp
= aspace
->i_sb
->s_fs_info
;
116 struct buffer_head
*bh
, *head
;
117 struct gfs2_bufdata
*bd
;
120 if (!page_has_buffers(page
))
123 head
= bh
= page_buffers(page
);
127 while (atomic_read(&bh
->b_count
)) {
128 if (atomic_read(&aspace
->i_writecount
)) {
129 if (time_after_eq(jiffies
, t
+
130 gfs2_tune_get(sdp
, gt_stall_secs
) * HZ
)) {
131 stuck_releasepage(bh
);
142 gfs2_assert_warn(sdp
, !buffer_pinned(bh
));
146 gfs2_assert_warn(sdp
, bd
->bd_bh
== bh
);
147 gfs2_assert_warn(sdp
, list_empty(&bd
->bd_list_tr
));
148 gfs2_assert_warn(sdp
, list_empty(&bd
->bd_le
.le_list
));
149 gfs2_assert_warn(sdp
, !bd
->bd_ail
);
150 kmem_cache_free(gfs2_bufdata_cachep
, bd
);
151 bh
->b_private
= NULL
;
154 bh
= bh
->b_this_page
;
159 return try_to_free_buffers(page
);
162 static struct address_space_operations aspace_aops
= {
163 .writepage
= gfs2_aspace_writepage
,
164 .releasepage
= gfs2_aspace_releasepage
,
168 * gfs2_aspace_get - Create and initialize a struct inode structure
169 * @sdp: the filesystem the aspace is in
171 * Right now a struct inode is just a struct inode. Maybe Linux
172 * will supply a more lightweight address space construct (that works)
175 * Make sure pages/buffers in this aspace aren't in high memory.
177 * Returns: the aspace
180 struct inode
*gfs2_aspace_get(struct gfs2_sbd
*sdp
)
182 struct inode
*aspace
;
184 aspace
= new_inode(sdp
->sd_vfs
);
186 mapping_set_gfp_mask(aspace
->i_mapping
, GFP_KERNEL
);
187 aspace
->i_mapping
->a_ops
= &aspace_aops
;
188 aspace
->i_size
= ~0ULL;
189 aspace
->u
.generic_ip
= NULL
;
190 insert_inode_hash(aspace
);
195 void gfs2_aspace_put(struct inode
*aspace
)
197 remove_inode_hash(aspace
);
202 * gfs2_ail1_start_one - Start I/O on a part of the AIL
203 * @sdp: the filesystem
204 * @tr: the part of the AIL
208 void gfs2_ail1_start_one(struct gfs2_sbd
*sdp
, struct gfs2_ail
*ai
)
210 struct gfs2_bufdata
*bd
, *s
;
211 struct buffer_head
*bh
;
214 BUG_ON(!spin_is_locked(&sdp
->sd_log_lock
));
219 list_for_each_entry_safe_reverse(bd
, s
, &ai
->ai_ail1_list
,
223 gfs2_assert(sdp
, bd
->bd_ail
== ai
);
225 if (!buffer_busy(bh
)) {
226 if (!buffer_uptodate(bh
)) {
227 gfs2_log_unlock(sdp
);
228 gfs2_io_error_bh(sdp
, bh
);
231 list_move(&bd
->bd_ail_st_list
,
236 if (!buffer_dirty(bh
))
239 list_move(&bd
->bd_ail_st_list
, &ai
->ai_ail1_list
);
241 gfs2_log_unlock(sdp
);
243 ll_rw_block(WRITE
, 1, &bh
);
253 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
254 * @sdp: the filesystem
259 int gfs2_ail1_empty_one(struct gfs2_sbd
*sdp
, struct gfs2_ail
*ai
, int flags
)
261 struct gfs2_bufdata
*bd
, *s
;
262 struct buffer_head
*bh
;
264 list_for_each_entry_safe_reverse(bd
, s
, &ai
->ai_ail1_list
,
268 gfs2_assert(sdp
, bd
->bd_ail
== ai
);
270 if (buffer_busy(bh
)) {
277 if (!buffer_uptodate(bh
))
278 gfs2_io_error_bh(sdp
, bh
);
280 list_move(&bd
->bd_ail_st_list
, &ai
->ai_ail2_list
);
283 return list_empty(&ai
->ai_ail1_list
);
287 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
288 * @sdp: the filesystem
293 void gfs2_ail2_empty_one(struct gfs2_sbd
*sdp
, struct gfs2_ail
*ai
)
295 struct list_head
*head
= &ai
->ai_ail2_list
;
296 struct gfs2_bufdata
*bd
;
298 while (!list_empty(head
)) {
299 bd
= list_entry(head
->prev
, struct gfs2_bufdata
,
301 gfs2_assert(sdp
, bd
->bd_ail
== ai
);
303 list_del(&bd
->bd_ail_st_list
);
304 list_del(&bd
->bd_ail_gl_list
);
305 atomic_dec(&bd
->bd_gl
->gl_ail_count
);
311 * ail_empty_gl - remove all buffers for a given lock from the AIL
314 * None of the buffers should be dirty, locked, or pinned.
317 void gfs2_ail_empty_gl(struct gfs2_glock
*gl
)
319 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
321 struct list_head
*head
= &gl
->gl_ail_list
;
322 struct gfs2_bufdata
*bd
;
323 struct buffer_head
*bh
;
327 blocks
= atomic_read(&gl
->gl_ail_count
);
331 error
= gfs2_trans_begin(sdp
, 0, blocks
);
332 if (gfs2_assert_withdraw(sdp
, !error
))
336 while (!list_empty(head
)) {
337 bd
= list_entry(head
->next
, struct gfs2_bufdata
,
340 blkno
= bh
->b_blocknr
;
341 gfs2_assert_withdraw(sdp
, !buffer_busy(bh
));
344 list_del(&bd
->bd_ail_st_list
);
345 list_del(&bd
->bd_ail_gl_list
);
346 atomic_dec(&gl
->gl_ail_count
);
348 gfs2_log_unlock(sdp
);
350 gfs2_trans_add_revoke(sdp
, blkno
);
354 gfs2_assert_withdraw(sdp
, !atomic_read(&gl
->gl_ail_count
));
355 gfs2_log_unlock(sdp
);
358 gfs2_log_flush(sdp
, NULL
);
362 * gfs2_meta_inval - Invalidate all buffers associated with a glock
367 void gfs2_meta_inval(struct gfs2_glock
*gl
)
369 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
370 struct inode
*aspace
= gl
->gl_aspace
;
371 struct address_space
*mapping
= gl
->gl_aspace
->i_mapping
;
373 gfs2_assert_withdraw(sdp
, !atomic_read(&gl
->gl_ail_count
));
375 atomic_inc(&aspace
->i_writecount
);
376 truncate_inode_pages(mapping
, 0);
377 atomic_dec(&aspace
->i_writecount
);
379 gfs2_assert_withdraw(sdp
, !mapping
->nrpages
);
383 * gfs2_meta_sync - Sync all buffers associated with a glock
385 * @flags: DIO_START | DIO_WAIT
389 void gfs2_meta_sync(struct gfs2_glock
*gl
, int flags
)
391 struct address_space
*mapping
= gl
->gl_aspace
->i_mapping
;
394 if (flags
& DIO_START
)
395 filemap_fdatawrite(mapping
);
396 if (!error
&& (flags
& DIO_WAIT
))
397 error
= filemap_fdatawait(mapping
);
400 gfs2_io_error(gl
->gl_sbd
);
404 * getbuf - Get a buffer with a given address space
405 * @sdp: the filesystem
406 * @aspace: the address space
407 * @blkno: the block number (filesystem scope)
408 * @create: 1 if the buffer should be created
410 * Returns: the buffer
413 static struct buffer_head
*getbuf(struct gfs2_sbd
*sdp
, struct inode
*aspace
,
414 uint64_t blkno
, int create
)
417 struct buffer_head
*bh
;
422 shift
= PAGE_CACHE_SHIFT
- sdp
->sd_sb
.sb_bsize_shift
;
423 index
= blkno
>> shift
; /* convert block to page */
424 bufnum
= blkno
- (index
<< shift
); /* block buf index within page */
428 page
= grab_cache_page(aspace
->i_mapping
, index
);
434 page
= find_lock_page(aspace
->i_mapping
, index
);
439 if (!page_has_buffers(page
))
440 create_empty_buffers(page
, sdp
->sd_sb
.sb_bsize
, 0);
442 /* Locate header for our buffer within our page */
443 for (bh
= page_buffers(page
); bufnum
--; bh
= bh
->b_this_page
)
447 if (!buffer_mapped(bh
))
448 map_bh(bh
, sdp
->sd_vfs
, blkno
);
451 mark_page_accessed(page
);
452 page_cache_release(page
);
457 static void meta_prep_new(struct buffer_head
*bh
)
459 struct gfs2_meta_header
*mh
= (struct gfs2_meta_header
*)bh
->b_data
;
462 clear_buffer_dirty(bh
);
463 set_buffer_uptodate(bh
);
466 mh
->mh_magic
= cpu_to_be32(GFS2_MAGIC
);
470 * gfs2_meta_new - Get a block
471 * @gl: The glock associated with this block
472 * @blkno: The block number
474 * Returns: The buffer
477 struct buffer_head
*gfs2_meta_new(struct gfs2_glock
*gl
, uint64_t blkno
)
479 struct buffer_head
*bh
;
480 bh
= getbuf(gl
->gl_sbd
, gl
->gl_aspace
, blkno
, CREATE
);
486 * gfs2_meta_read - Read a block from disk
487 * @gl: The glock covering the block
488 * @blkno: The block number
489 * @flags: flags to gfs2_dreread()
490 * @bhp: the place where the buffer is returned (NULL on failure)
495 int gfs2_meta_read(struct gfs2_glock
*gl
, uint64_t blkno
, int flags
,
496 struct buffer_head
**bhp
)
500 *bhp
= getbuf(gl
->gl_sbd
, gl
->gl_aspace
, blkno
, CREATE
);
501 error
= gfs2_meta_reread(gl
->gl_sbd
, *bhp
, flags
);
509 * gfs2_meta_reread - Reread a block from disk
510 * @sdp: the filesystem
511 * @bh: The block to read
512 * @flags: Flags that control the read
517 int gfs2_meta_reread(struct gfs2_sbd
*sdp
, struct buffer_head
*bh
, int flags
)
519 if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
)))
522 if (flags
& DIO_FORCE
)
523 clear_buffer_uptodate(bh
);
525 if ((flags
& DIO_START
) && !buffer_uptodate(bh
))
526 ll_rw_block(READ
, 1, &bh
);
528 if (flags
& DIO_WAIT
) {
531 if (!buffer_uptodate(bh
)) {
532 struct gfs2_trans
*tr
= current
->journal_info
;
533 if (tr
&& tr
->tr_touched
)
534 gfs2_io_error_bh(sdp
, bh
);
537 if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
)))
545 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
546 * @gl: the glock the buffer belongs to
547 * @bh: The buffer to be attached to
548 * @meta: Flag to indicate whether its metadata or not
551 void gfs2_attach_bufdata(struct gfs2_glock
*gl
, struct buffer_head
*bh
,
554 struct gfs2_bufdata
*bd
;
557 lock_page(bh
->b_page
);
561 unlock_page(bh
->b_page
);
565 bd
= kmem_cache_alloc(gfs2_bufdata_cachep
, GFP_NOFS
| __GFP_NOFAIL
),
566 memset(bd
, 0, sizeof(struct gfs2_bufdata
));
570 INIT_LIST_HEAD(&bd
->bd_list_tr
);
572 lops_init_le(&bd
->bd_le
, &gfs2_buf_lops
);
574 lops_init_le(&bd
->bd_le
, &gfs2_databuf_lops
);
580 unlock_page(bh
->b_page
);
584 * gfs2_pin - Pin a buffer in memory
585 * @sdp: the filesystem the buffer belongs to
586 * @bh: The buffer to be pinned
590 void gfs2_pin(struct gfs2_sbd
*sdp
, struct buffer_head
*bh
)
592 struct gfs2_bufdata
*bd
= bh
->b_private
;
594 gfs2_assert_withdraw(sdp
, test_bit(SDF_JOURNAL_LIVE
, &sdp
->sd_flags
));
596 if (test_set_buffer_pinned(bh
))
597 gfs2_assert_withdraw(sdp
, 0);
601 /* If this buffer is in the AIL and it has already been written
602 to in-place disk block, remove it from the AIL. */
605 if (bd
->bd_ail
&& !buffer_in_io(bh
))
606 list_move(&bd
->bd_ail_st_list
, &bd
->bd_ail
->ai_ail2_list
);
607 gfs2_log_unlock(sdp
);
609 clear_buffer_dirty(bh
);
612 if (!buffer_uptodate(bh
))
613 gfs2_io_error_bh(sdp
, bh
);
619 * gfs2_unpin - Unpin a buffer
620 * @sdp: the filesystem the buffer belongs to
621 * @bh: The buffer to unpin
626 void gfs2_unpin(struct gfs2_sbd
*sdp
, struct buffer_head
*bh
,
629 struct gfs2_bufdata
*bd
= bh
->b_private
;
631 gfs2_assert_withdraw(sdp
, buffer_uptodate(bh
));
633 if (!buffer_pinned(bh
))
634 gfs2_assert_withdraw(sdp
, 0);
636 mark_buffer_dirty(bh
);
637 clear_buffer_pinned(bh
);
641 list_del(&bd
->bd_ail_st_list
);
644 struct gfs2_glock
*gl
= bd
->bd_gl
;
645 list_add(&bd
->bd_ail_gl_list
, &gl
->gl_ail_list
);
646 atomic_inc(&gl
->gl_ail_count
);
649 list_add(&bd
->bd_ail_st_list
, &ai
->ai_ail1_list
);
650 gfs2_log_unlock(sdp
);
654 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
655 * @ip: the inode who owns the buffers
656 * @bstart: the first buffer in the run
657 * @blen: the number of buffers in the run
661 void gfs2_meta_wipe(struct gfs2_inode
*ip
, uint64_t bstart
, uint32_t blen
)
663 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
664 struct inode
*aspace
= ip
->i_gl
->gl_aspace
;
665 struct buffer_head
*bh
;
668 bh
= getbuf(sdp
, aspace
, bstart
, NO_CREATE
);
670 struct gfs2_bufdata
*bd
= bh
->b_private
;
672 if (test_clear_buffer_pinned(bh
)) {
673 struct gfs2_trans
*tr
= current
->journal_info
;
675 list_del_init(&bd
->bd_le
.le_list
);
676 gfs2_assert_warn(sdp
, sdp
->sd_log_num_buf
);
677 sdp
->sd_log_num_buf
--;
678 gfs2_log_unlock(sdp
);
685 uint64_t blkno
= bh
->b_blocknr
;
687 list_del(&bd
->bd_ail_st_list
);
688 list_del(&bd
->bd_ail_gl_list
);
689 atomic_dec(&bd
->bd_gl
->gl_ail_count
);
691 gfs2_log_unlock(sdp
);
692 gfs2_trans_add_revoke(sdp
, blkno
);
694 gfs2_log_unlock(sdp
);
698 clear_buffer_dirty(bh
);
699 clear_buffer_uptodate(bh
);
711 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
712 * @ip: The GFS2 inode
714 * This releases buffers that are in the most-recently-used array of
715 * blocks used for indirect block addressing for this inode.
718 void gfs2_meta_cache_flush(struct gfs2_inode
*ip
)
720 struct buffer_head
**bh_slot
;
723 spin_lock(&ip
->i_spin
);
725 for (x
= 0; x
< GFS2_MAX_META_HEIGHT
; x
++) {
726 bh_slot
= &ip
->i_cache
[x
];
733 spin_unlock(&ip
->i_spin
);
737 * gfs2_meta_indirect_buffer - Get a metadata buffer
738 * @ip: The GFS2 inode
739 * @height: The level of this buf in the metadata (indir addr) tree (if any)
740 * @num: The block number (device relative) of the buffer
741 * @new: Non-zero if we may create a new buffer
742 * @bhp: the buffer is returned here
744 * Try to use the gfs2_inode's MRU metadata tree cache.
749 int gfs2_meta_indirect_buffer(struct gfs2_inode
*ip
, int height
, uint64_t num
,
750 int new, struct buffer_head
**bhp
)
752 struct buffer_head
*bh
, **bh_slot
= ip
->i_cache
+ height
;
755 spin_lock(&ip
->i_spin
);
758 if (bh
->b_blocknr
== num
)
763 spin_unlock(&ip
->i_spin
);
769 error
= gfs2_meta_reread(GFS2_SB(&ip
->i_inode
), bh
,
770 DIO_START
| DIO_WAIT
);
778 bh
= gfs2_meta_new(ip
->i_gl
, num
);
780 error
= gfs2_meta_read(ip
->i_gl
, num
,
781 DIO_START
| DIO_WAIT
, &bh
);
786 spin_lock(&ip
->i_spin
);
787 if (*bh_slot
!= bh
) {
792 spin_unlock(&ip
->i_spin
);
796 if (gfs2_assert_warn(GFS2_SB(&ip
->i_inode
), height
)) {
800 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
801 gfs2_metatype_set(bh
, GFS2_METATYPE_IN
, GFS2_FORMAT_IN
);
802 gfs2_buffer_clear_tail(bh
, sizeof(struct gfs2_meta_header
));
804 } else if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), bh
,
805 (height
) ? GFS2_METATYPE_IN
: GFS2_METATYPE_DI
)) {
816 * gfs2_meta_ra - start readahead on an extent of a file
817 * @gl: the glock the blocks belong to
818 * @dblock: the starting disk block
819 * @extlen: the number of blocks in the extent
823 void gfs2_meta_ra(struct gfs2_glock
*gl
, uint64_t dblock
, uint32_t extlen
)
825 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
826 struct inode
*aspace
= gl
->gl_aspace
;
827 struct buffer_head
*first_bh
, *bh
;
828 uint32_t max_ra
= gfs2_tune_get(sdp
, gt_max_readahead
) >>
829 sdp
->sd_sb
.sb_bsize_shift
;
832 if (!extlen
|| !max_ra
)
837 first_bh
= getbuf(sdp
, aspace
, dblock
, CREATE
);
839 if (buffer_uptodate(first_bh
))
841 if (!buffer_locked(first_bh
)) {
842 error
= gfs2_meta_reread(sdp
, first_bh
, DIO_START
);
851 bh
= getbuf(sdp
, aspace
, dblock
, CREATE
);
853 if (!buffer_uptodate(bh
) && !buffer_locked(bh
)) {
854 error
= gfs2_meta_reread(sdp
, bh
, DIO_START
);
864 if (buffer_uptodate(first_bh
))
873 * gfs2_meta_syncfs - sync all the buffers in a filesystem
874 * @sdp: the filesystem
878 void gfs2_meta_syncfs(struct gfs2_sbd
*sdp
)
880 gfs2_log_flush(sdp
, NULL
);
882 gfs2_ail1_start(sdp
, DIO_ALL
);
883 if (gfs2_ail1_empty(sdp
, DIO_ALL
))