2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 <asm/semaphore.h>
32 #define buffer_busy(bh) \
33 ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned)))
34 #define buffer_in_io(bh) \
35 ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock)))
37 static int aspace_get_block(struct inode *inode, sector_t lblock,
38 struct buffer_head *bh_result, int create)
40 gfs2_assert_warn(get_v2sdp(inode->i_sb), 0);
44 static int gfs2_aspace_writepage(struct page *page,
45 struct writeback_control *wbc)
47 return block_write_full_page(page, aspace_get_block, wbc);
51 * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out.
52 * @bh: the buffer we're stuck on
56 static void stuck_releasepage(struct buffer_head *bh)
58 struct gfs2_sbd *sdp = get_v2sdp(bh->b_page->mapping->host->i_sb);
59 struct gfs2_bufdata *bd = get_v2bd(bh);
60 struct gfs2_glock *gl;
62 fs_warn(sdp, "stuck in gfs2_releasepage()\n");
63 fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
64 (uint64_t)bh->b_blocknr, atomic_read(&bh->b_count));
65 fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
66 fs_warn(sdp, "get_v2bd(bh) = %s\n", (bd) ? "!NULL" : "NULL");
73 fs_warn(sdp, "gl = (%u, %llu)\n",
74 gl->gl_name.ln_type, gl->gl_name.ln_number);
76 fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
77 (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
78 (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
80 if (gl->gl_ops == &gfs2_inode_glops) {
81 struct gfs2_inode *ip = get_gl2ip(gl);
87 fs_warn(sdp, "ip = %llu %llu\n",
88 ip->i_num.no_formal_ino, ip->i_num.no_addr);
89 fs_warn(sdp, "ip->i_count = %d, ip->i_vnode = %s\n",
90 atomic_read(&ip->i_count),
91 (ip->i_vnode) ? "!NULL" : "NULL");
93 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
94 fs_warn(sdp, "ip->i_cache[%u] = %s\n",
95 x, (ip->i_cache[x]) ? "!NULL" : "NULL");
100 * gfs2_aspace_releasepage - free the metadata associated with a page
101 * @page: the page that's being released
102 * @gfp_mask: passed from Linux VFS, ignored by us
104 * Call try_to_free_buffers() if the buffers in this page can be
110 static int gfs2_aspace_releasepage(struct page *page, gfp_t gfp_mask)
112 struct inode *aspace = page->mapping->host;
113 struct gfs2_sbd *sdp = get_v2sdp(aspace->i_sb);
114 struct buffer_head *bh, *head;
115 struct gfs2_bufdata *bd;
118 if (!page_has_buffers(page))
121 head = bh = page_buffers(page);
125 while (atomic_read(&bh->b_count)) {
126 if (atomic_read(&aspace->i_writecount)) {
127 if (time_after_eq(jiffies, t +
128 gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
129 stuck_releasepage(bh);
140 gfs2_assert_warn(sdp, !buffer_pinned(bh));
144 gfs2_assert_warn(sdp, bd->bd_bh == bh);
145 gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
146 gfs2_assert_warn(sdp, list_empty(&bd->bd_le.le_list));
147 gfs2_assert_warn(sdp, !bd->bd_ail);
148 kmem_cache_free(gfs2_bufdata_cachep, bd);
149 atomic_dec(&sdp->sd_bufdata_count);
153 bh = bh->b_this_page;
158 return try_to_free_buffers(page);
161 static struct address_space_operations aspace_aops = {
162 .writepage = gfs2_aspace_writepage,
163 .releasepage = gfs2_aspace_releasepage,
167 * gfs2_aspace_get - Create and initialize a struct inode structure
168 * @sdp: the filesystem the aspace is in
170 * Right now a struct inode is just a struct inode. Maybe Linux
171 * will supply a more lightweight address space construct (that works)
174 * Make sure pages/buffers in this aspace aren't in high memory.
176 * Returns: the aspace
179 struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
181 struct inode *aspace;
183 aspace = new_inode(sdp->sd_vfs);
185 mapping_set_gfp_mask(aspace->i_mapping, GFP_KERNEL);
186 aspace->i_mapping->a_ops = &aspace_aops;
187 aspace->i_size = ~0ULL;
188 set_v2ip(aspace, NULL);
189 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;
217 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
221 gfs2_assert(sdp, bd->bd_ail == ai);
223 if (!buffer_busy(bh)) {
224 if (!buffer_uptodate(bh))
225 gfs2_io_error_bh(sdp, bh);
226 list_move(&bd->bd_ail_st_list,
231 if (!buffer_dirty(bh))
234 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
236 gfs2_log_unlock(sdp);
238 ll_rw_block(WRITE, 1, &bh);
248 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
249 * @sdp: the filesystem
254 int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
256 struct gfs2_bufdata *bd, *s;
257 struct buffer_head *bh;
259 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
263 gfs2_assert(sdp, bd->bd_ail == ai);
265 if (buffer_busy(bh)) {
272 if (!buffer_uptodate(bh))
273 gfs2_io_error_bh(sdp, bh);
275 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
278 return list_empty(&ai->ai_ail1_list);
282 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
283 * @sdp: the filesystem
288 void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
290 struct list_head *head = &ai->ai_ail2_list;
291 struct gfs2_bufdata *bd;
293 while (!list_empty(head)) {
294 bd = list_entry(head->prev, struct gfs2_bufdata,
296 gfs2_assert(sdp, bd->bd_ail == ai);
298 list_del(&bd->bd_ail_st_list);
299 list_del(&bd->bd_ail_gl_list);
300 atomic_dec(&bd->bd_gl->gl_ail_count);
306 * ail_empty_gl - remove all buffers for a given lock from the AIL
309 * None of the buffers should be dirty, locked, or pinned.
312 void gfs2_ail_empty_gl(struct gfs2_glock *gl)
314 struct gfs2_sbd *sdp = gl->gl_sbd;
316 struct list_head *head = &gl->gl_ail_list;
317 struct gfs2_bufdata *bd;
318 struct buffer_head *bh;
322 blocks = atomic_read(&gl->gl_ail_count);
326 error = gfs2_trans_begin(sdp, 0, blocks);
327 if (gfs2_assert_withdraw(sdp, !error))
331 while (!list_empty(head)) {
332 bd = list_entry(head->next, struct gfs2_bufdata,
335 blkno = bh->b_blocknr;
336 gfs2_assert_withdraw(sdp, !buffer_busy(bh));
339 list_del(&bd->bd_ail_st_list);
340 list_del(&bd->bd_ail_gl_list);
341 atomic_dec(&gl->gl_ail_count);
343 gfs2_log_unlock(sdp);
345 gfs2_trans_add_revoke(sdp, blkno);
349 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
350 gfs2_log_unlock(sdp);
357 * gfs2_meta_inval - Invalidate all buffers associated with a glock
362 void gfs2_meta_inval(struct gfs2_glock *gl)
364 struct gfs2_sbd *sdp = gl->gl_sbd;
365 struct inode *aspace = gl->gl_aspace;
366 struct address_space *mapping = gl->gl_aspace->i_mapping;
368 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
370 atomic_inc(&aspace->i_writecount);
371 truncate_inode_pages(mapping, 0);
372 atomic_dec(&aspace->i_writecount);
374 gfs2_assert_withdraw(sdp, !mapping->nrpages);
378 * gfs2_meta_sync - Sync all buffers associated with a glock
380 * @flags: DIO_START | DIO_WAIT
384 void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
386 struct address_space *mapping = gl->gl_aspace->i_mapping;
389 if (flags & DIO_START)
390 filemap_fdatawrite(mapping);
391 if (!error && (flags & DIO_WAIT))
392 error = filemap_fdatawait(mapping);
395 gfs2_io_error(gl->gl_sbd);
399 * getbuf - Get a buffer with a given address space
400 * @sdp: the filesystem
401 * @aspace: the address space
402 * @blkno: the block number (filesystem scope)
403 * @create: 1 if the buffer should be created
405 * Returns: the buffer
408 static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
409 uint64_t blkno, int create)
412 struct buffer_head *bh;
417 shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
418 index = blkno >> shift; /* convert block to page */
419 bufnum = blkno - (index << shift); /* block buf index within page */
423 page = grab_cache_page(aspace->i_mapping, index);
429 page = find_lock_page(aspace->i_mapping, index);
434 if (!page_has_buffers(page))
435 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
437 /* Locate header for our buffer within our page */
438 for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
442 if (!buffer_mapped(bh))
443 map_bh(bh, sdp->sd_vfs, blkno);
446 mark_page_accessed(page);
447 page_cache_release(page);
452 static void meta_prep_new(struct buffer_head *bh)
454 struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
457 clear_buffer_dirty(bh);
458 set_buffer_uptodate(bh);
461 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
465 * gfs2_meta_new - Get a block
466 * @gl: The glock associated with this block
467 * @blkno: The block number
469 * Returns: The buffer
472 struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, uint64_t blkno)
474 struct buffer_head *bh;
475 bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
481 * gfs2_meta_read - Read a block from disk
482 * @gl: The glock covering the block
483 * @blkno: The block number
484 * @flags: flags to gfs2_dreread()
485 * @bhp: the place where the buffer is returned (NULL on failure)
490 int gfs2_meta_read(struct gfs2_glock *gl, uint64_t blkno, int flags,
491 struct buffer_head **bhp)
495 *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
496 error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags);
504 * gfs2_meta_reread - Reread a block from disk
505 * @sdp: the filesystem
506 * @bh: The block to read
507 * @flags: Flags that control the read
512 int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
514 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
517 if (flags & DIO_FORCE)
518 clear_buffer_uptodate(bh);
520 if ((flags & DIO_START) && !buffer_uptodate(bh))
521 ll_rw_block(READ, 1, &bh);
523 if (flags & DIO_WAIT) {
526 if (!buffer_uptodate(bh)) {
527 struct gfs2_trans *tr = get_transaction;
528 if (tr && tr->tr_touched)
529 gfs2_io_error_bh(sdp, bh);
532 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
540 * gfs2_meta_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
541 * @gl: the glock the buffer belongs to
542 * @bh: The buffer to be attached to
546 void gfs2_meta_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh)
548 struct gfs2_bufdata *bd;
550 lock_page(bh->b_page);
553 unlock_page(bh->b_page);
557 bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_KERNEL | __GFP_NOFAIL),
558 atomic_inc(&gl->gl_sbd->sd_bufdata_count);
560 memset(bd, 0, sizeof(struct gfs2_bufdata));
565 INIT_LIST_HEAD(&bd->bd_list_tr);
566 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
570 unlock_page(bh->b_page);
574 * gfs2_meta_pin - Pin a metadata buffer in memory
575 * @sdp: the filesystem the buffer belongs to
576 * @bh: The buffer to be pinned
580 void gfs2_meta_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
582 struct gfs2_bufdata *bd = get_v2bd(bh);
584 gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
586 if (test_set_buffer_pinned(bh))
587 gfs2_assert_withdraw(sdp, 0);
591 /* If this buffer is in the AIL and it has already been written
592 to in-place disk block, remove it from the AIL. */
595 if (bd->bd_ail && !buffer_in_io(bh))
596 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
597 gfs2_log_unlock(sdp);
599 clear_buffer_dirty(bh);
602 if (!buffer_uptodate(bh))
603 gfs2_io_error_bh(sdp, bh);
609 * gfs2_meta_unpin - Unpin a buffer
610 * @sdp: the filesystem the buffer belongs to
611 * @bh: The buffer to unpin
616 void gfs2_meta_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
619 struct gfs2_bufdata *bd = get_v2bd(bh);
621 gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
623 if (!buffer_pinned(bh))
624 gfs2_assert_withdraw(sdp, 0);
626 mark_buffer_dirty(bh);
627 clear_buffer_pinned(bh);
631 list_del(&bd->bd_ail_st_list);
634 struct gfs2_glock *gl = bd->bd_gl;
635 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
636 atomic_inc(&gl->gl_ail_count);
639 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
640 gfs2_log_unlock(sdp);
644 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
645 * @ip: the inode who owns the buffers
646 * @bstart: the first buffer in the run
647 * @blen: the number of buffers in the run
651 void gfs2_meta_wipe(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
653 struct gfs2_sbd *sdp = ip->i_sbd;
654 struct inode *aspace = ip->i_gl->gl_aspace;
655 struct buffer_head *bh;
658 bh = getbuf(sdp, aspace, bstart, NO_CREATE);
660 struct gfs2_bufdata *bd = get_v2bd(bh);
662 if (test_clear_buffer_pinned(bh)) {
664 list_del_init(&bd->bd_le.le_list);
665 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
666 sdp->sd_log_num_buf--;
667 gfs2_log_unlock(sdp);
668 get_transaction->tr_num_buf_rm++;
674 uint64_t blkno = bh->b_blocknr;
676 list_del(&bd->bd_ail_st_list);
677 list_del(&bd->bd_ail_gl_list);
678 atomic_dec(&bd->bd_gl->gl_ail_count);
680 gfs2_log_unlock(sdp);
681 gfs2_trans_add_revoke(sdp, blkno);
683 gfs2_log_unlock(sdp);
687 clear_buffer_dirty(bh);
688 clear_buffer_uptodate(bh);
700 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
701 * @ip: The GFS2 inode
703 * This releases buffers that are in the most-recently-used array of
704 * blocks used for indirect block addressing for this inode.
707 void gfs2_meta_cache_flush(struct gfs2_inode *ip)
709 struct buffer_head **bh_slot;
712 spin_lock(&ip->i_spin);
714 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
715 bh_slot = &ip->i_cache[x];
722 spin_unlock(&ip->i_spin);
726 * gfs2_meta_indirect_buffer - Get a metadata buffer
727 * @ip: The GFS2 inode
728 * @height: The level of this buf in the metadata (indir addr) tree (if any)
729 * @num: The block number (device relative) of the buffer
730 * @new: Non-zero if we may create a new buffer
731 * @bhp: the buffer is returned here
733 * Try to use the gfs2_inode's MRU metadata tree cache.
738 int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, uint64_t num,
739 int new, struct buffer_head **bhp)
741 struct buffer_head *bh, **bh_slot = ip->i_cache + height;
744 spin_lock(&ip->i_spin);
747 if (bh->b_blocknr == num)
752 spin_unlock(&ip->i_spin);
758 error = gfs2_meta_reread(ip->i_sbd, bh,
759 DIO_START | DIO_WAIT);
767 bh = gfs2_meta_new(ip->i_gl, num);
769 error = gfs2_meta_read(ip->i_gl, num,
770 DIO_START | DIO_WAIT, &bh);
775 spin_lock(&ip->i_spin);
776 if (*bh_slot != bh) {
781 spin_unlock(&ip->i_spin);
785 if (gfs2_assert_warn(ip->i_sbd, height)) {
789 gfs2_trans_add_bh(ip->i_gl, bh, 1);
790 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
791 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
793 } else if (gfs2_metatype_check(ip->i_sbd, bh,
794 (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
805 * gfs2_meta_ra - start readahead on an extent of a file
806 * @gl: the glock the blocks belong to
807 * @dblock: the starting disk block
808 * @extlen: the number of blocks in the extent
812 void gfs2_meta_ra(struct gfs2_glock *gl, uint64_t dblock, uint32_t extlen)
814 struct gfs2_sbd *sdp = gl->gl_sbd;
815 struct inode *aspace = gl->gl_aspace;
816 struct buffer_head *first_bh, *bh;
817 uint32_t max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> sdp->sd_sb.sb_bsize_shift;
820 if (!extlen || !max_ra)
825 first_bh = getbuf(sdp, aspace, dblock, CREATE);
827 if (buffer_uptodate(first_bh))
829 if (!buffer_locked(first_bh)) {
830 error = gfs2_meta_reread(sdp, first_bh, DIO_START);
839 bh = getbuf(sdp, aspace, dblock, CREATE);
841 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
842 error = gfs2_meta_reread(sdp, bh, DIO_START);
852 if (buffer_uptodate(first_bh))
861 * gfs2_meta_syncfs - sync all the buffers in a filesystem
862 * @sdp: the filesystem
866 void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
870 gfs2_ail1_start(sdp, DIO_ALL);
871 if (gfs2_ail1_empty(sdp, DIO_ALL))