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.
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>
21 #include <linux/lm_interface.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);
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,
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)
67 * Make sure pages/buffers in this aspace aren't in high memory.
72 struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
76 aspace = new_inode(sdp->sd_vfs);
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);
87 void gfs2_aspace_put(struct inode *aspace)
89 remove_inode_hash(aspace);
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;
106 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
111 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
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);
123 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
127 if (!buffer_dirty(bh))
130 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
132 gfs2_log_unlock(sdp);
134 ll_rw_block(WRITE, 1, &bh);
144 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
145 * @sdp: the filesystem
150 int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
152 struct gfs2_bufdata *bd, *s;
153 struct buffer_head *bh;
155 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
159 gfs2_assert(sdp, bd->bd_ail == ai);
161 if (buffer_busy(bh)) {
168 if (!buffer_uptodate(bh))
169 gfs2_io_error_bh(sdp, bh);
171 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
174 return list_empty(&ai->ai_ail1_list);
178 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
179 * @sdp: the filesystem
184 void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
186 struct list_head *head = &ai->ai_ail2_list;
187 struct gfs2_bufdata *bd;
189 while (!list_empty(head)) {
190 bd = list_entry(head->prev, struct gfs2_bufdata,
192 gfs2_assert(sdp, bd->bd_ail == ai);
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);
202 * ail_empty_gl - remove all buffers for a given lock from the AIL
205 * None of the buffers should be dirty, locked, or pinned.
208 void gfs2_ail_empty_gl(struct gfs2_glock *gl)
210 struct gfs2_sbd *sdp = gl->gl_sbd;
212 struct list_head *head = &gl->gl_ail_list;
213 struct gfs2_bufdata *bd;
214 struct buffer_head *bh;
218 blocks = atomic_read(&gl->gl_ail_count);
222 error = gfs2_trans_begin(sdp, 0, blocks);
223 if (gfs2_assert_withdraw(sdp, !error))
227 while (!list_empty(head)) {
228 bd = list_entry(head->next, struct gfs2_bufdata,
231 blkno = bh->b_blocknr;
232 gfs2_assert_withdraw(sdp, !buffer_busy(bh));
235 list_del(&bd->bd_ail_st_list);
236 list_del(&bd->bd_ail_gl_list);
237 atomic_dec(&gl->gl_ail_count);
239 gfs2_log_unlock(sdp);
241 gfs2_trans_add_revoke(sdp, blkno);
245 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
246 gfs2_log_unlock(sdp);
249 gfs2_log_flush(sdp, NULL);
253 * gfs2_meta_inval - Invalidate all buffers associated with a glock
258 void gfs2_meta_inval(struct gfs2_glock *gl)
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;
264 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
266 atomic_inc(&aspace->i_writecount);
267 truncate_inode_pages(mapping, 0);
268 atomic_dec(&aspace->i_writecount);
270 gfs2_assert_withdraw(sdp, !mapping->nrpages);
274 * gfs2_meta_sync - Sync all buffers associated with a glock
276 * @flags: DIO_START | DIO_WAIT
280 void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
282 struct address_space *mapping = gl->gl_aspace->i_mapping;
285 if (flags & DIO_START)
286 filemap_fdatawrite(mapping);
287 if (!error && (flags & DIO_WAIT))
288 error = filemap_fdatawait(mapping);
291 gfs2_io_error(gl->gl_sbd);
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
301 * Returns: the buffer
304 static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
305 u64 blkno, int create)
308 struct buffer_head *bh;
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 */
319 page = grab_cache_page(aspace->i_mapping, index);
325 page = find_lock_page(aspace->i_mapping, index);
330 if (!page_has_buffers(page))
331 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
333 /* Locate header for our buffer within our page */
334 for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
338 if (!buffer_mapped(bh))
339 map_bh(bh, sdp->sd_vfs, blkno);
342 mark_page_accessed(page);
343 page_cache_release(page);
348 static void meta_prep_new(struct buffer_head *bh)
350 struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
353 clear_buffer_dirty(bh);
354 set_buffer_uptodate(bh);
357 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
361 * gfs2_meta_new - Get a block
362 * @gl: The glock associated with this block
363 * @blkno: The block number
365 * Returns: The buffer
368 struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
370 struct buffer_head *bh;
371 bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
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)
386 int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
387 struct buffer_head **bhp)
391 *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
392 error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags);
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
408 int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
410 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
413 if ((flags & DIO_START) && !buffer_uptodate(bh))
414 ll_rw_block(READ, 1, &bh);
416 if (flags & DIO_WAIT) {
419 if (!buffer_uptodate(bh)) {
420 struct gfs2_trans *tr = current->journal_info;
421 if (tr && tr->tr_touched)
422 gfs2_io_error_bh(sdp, bh);
425 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
433 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
434 * @gl: the glock the buffer belongs to
435 * @bh: The buffer to be attached to
436 * @meta: Flag to indicate whether its metadata or not
439 void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
442 struct gfs2_bufdata *bd;
445 lock_page(bh->b_page);
449 unlock_page(bh->b_page);
453 bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
454 memset(bd, 0, sizeof(struct gfs2_bufdata));
458 INIT_LIST_HEAD(&bd->bd_list_tr);
460 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
462 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
466 unlock_page(bh->b_page);
470 * gfs2_pin - Pin a buffer in memory
471 * @sdp: the filesystem the buffer belongs to
472 * @bh: The buffer to be pinned
476 void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
478 struct gfs2_bufdata *bd = bh->b_private;
480 gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
482 if (test_set_buffer_pinned(bh))
483 gfs2_assert_withdraw(sdp, 0);
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. */
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);
495 clear_buffer_dirty(bh);
498 if (!buffer_uptodate(bh))
499 gfs2_io_error_bh(sdp, bh);
505 * gfs2_unpin - Unpin a buffer
506 * @sdp: the filesystem the buffer belongs to
507 * @bh: The buffer to unpin
512 void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
515 struct gfs2_bufdata *bd = bh->b_private;
517 gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
519 if (!buffer_pinned(bh))
520 gfs2_assert_withdraw(sdp, 0);
522 mark_buffer_dirty(bh);
523 clear_buffer_pinned(bh);
527 list_del(&bd->bd_ail_st_list);
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);
535 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
536 gfs2_log_unlock(sdp);
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
547 void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
549 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
550 struct inode *aspace = ip->i_gl->gl_aspace;
551 struct buffer_head *bh;
554 bh = getbuf(sdp, aspace, bstart, NO_CREATE);
556 struct gfs2_bufdata *bd = bh->b_private;
558 if (test_clear_buffer_pinned(bh)) {
559 struct gfs2_trans *tr = current->journal_info;
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);
571 u64 blkno = bh->b_blocknr;
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);
577 gfs2_log_unlock(sdp);
578 gfs2_trans_add_revoke(sdp, blkno);
580 gfs2_log_unlock(sdp);
584 clear_buffer_dirty(bh);
585 clear_buffer_uptodate(bh);
597 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
598 * @ip: The GFS2 inode
600 * This releases buffers that are in the most-recently-used array of
601 * blocks used for indirect block addressing for this inode.
604 void gfs2_meta_cache_flush(struct gfs2_inode *ip)
606 struct buffer_head **bh_slot;
609 spin_lock(&ip->i_spin);
611 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
612 bh_slot = &ip->i_cache[x];
619 spin_unlock(&ip->i_spin);
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
630 * Try to use the gfs2_inode's MRU metadata tree cache.
635 int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
636 int new, struct buffer_head **bhp)
638 struct buffer_head *bh, **bh_slot = ip->i_cache + height;
641 spin_lock(&ip->i_spin);
644 if (bh->b_blocknr == num)
649 spin_unlock(&ip->i_spin);
655 error = gfs2_meta_reread(GFS2_SB(&ip->i_inode), bh,
656 DIO_START | DIO_WAIT);
664 bh = gfs2_meta_new(ip->i_gl, num);
666 error = gfs2_meta_read(ip->i_gl, num,
667 DIO_START | DIO_WAIT, &bh);
672 spin_lock(&ip->i_spin);
673 if (*bh_slot != bh) {
678 spin_unlock(&ip->i_spin);
682 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), height)) {
686 gfs2_trans_add_bh(ip->i_gl, bh, 1);
687 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
688 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
690 } else if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh,
691 (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
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
709 void gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
711 struct gfs2_sbd *sdp = gl->gl_sbd;
712 struct inode *aspace = gl->gl_aspace;
713 struct buffer_head *first_bh, *bh;
714 u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
715 sdp->sd_sb.sb_bsize_shift;
718 if (!extlen || !max_ra)
723 first_bh = getbuf(sdp, aspace, dblock, CREATE);
725 if (buffer_uptodate(first_bh))
727 if (!buffer_locked(first_bh)) {
728 error = gfs2_meta_reread(sdp, first_bh, DIO_START);
737 bh = getbuf(sdp, aspace, dblock, CREATE);
739 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
740 error = gfs2_meta_reread(sdp, bh, DIO_START);
750 if (buffer_uptodate(first_bh))
759 * gfs2_meta_syncfs - sync all the buffers in a filesystem
760 * @sdp: the filesystem
764 void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
766 gfs2_log_flush(sdp, NULL);
768 gfs2_ail1_start(sdp, DIO_ALL);
769 if (gfs2_ail1_empty(sdp, DIO_ALL))