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);
152 bh = bh->b_this_page;
157 return try_to_free_buffers(page);
160 static struct address_space_operations aspace_aops = {
161 .writepage = gfs2_aspace_writepage,
162 .releasepage = gfs2_aspace_releasepage,
166 * gfs2_aspace_get - Create and initialize a struct inode structure
167 * @sdp: the filesystem the aspace is in
169 * Right now a struct inode is just a struct inode. Maybe Linux
170 * will supply a more lightweight address space construct (that works)
173 * Make sure pages/buffers in this aspace aren't in high memory.
175 * Returns: the aspace
178 struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
180 struct inode *aspace;
182 aspace = new_inode(sdp->sd_vfs);
184 mapping_set_gfp_mask(aspace->i_mapping, GFP_KERNEL);
185 aspace->i_mapping->a_ops = &aspace_aops;
186 aspace->i_size = ~0ULL;
187 set_v2ip(aspace, NULL);
188 insert_inode_hash(aspace);
194 void gfs2_aspace_put(struct inode *aspace)
196 remove_inode_hash(aspace);
201 * gfs2_ail1_start_one - Start I/O on a part of the AIL
202 * @sdp: the filesystem
203 * @tr: the part of the AIL
207 void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
209 struct gfs2_bufdata *bd, *s;
210 struct buffer_head *bh;
216 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
220 gfs2_assert(sdp, bd->bd_ail == ai);
222 if (!buffer_busy(bh)) {
223 if (!buffer_uptodate(bh))
224 gfs2_io_error_bh(sdp, bh);
225 list_move(&bd->bd_ail_st_list,
230 if (!buffer_dirty(bh))
233 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
235 gfs2_log_unlock(sdp);
237 ll_rw_block(WRITE, 1, &bh);
247 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
248 * @sdp: the filesystem
253 int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
255 struct gfs2_bufdata *bd, *s;
256 struct buffer_head *bh;
258 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
262 gfs2_assert(sdp, bd->bd_ail == ai);
264 if (buffer_busy(bh)) {
271 if (!buffer_uptodate(bh))
272 gfs2_io_error_bh(sdp, bh);
274 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
277 return list_empty(&ai->ai_ail1_list);
281 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
282 * @sdp: the filesystem
287 void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
289 struct list_head *head = &ai->ai_ail2_list;
290 struct gfs2_bufdata *bd;
292 while (!list_empty(head)) {
293 bd = list_entry(head->prev, struct gfs2_bufdata,
295 gfs2_assert(sdp, bd->bd_ail == ai);
297 list_del(&bd->bd_ail_st_list);
298 list_del(&bd->bd_ail_gl_list);
299 atomic_dec(&bd->bd_gl->gl_ail_count);
305 * ail_empty_gl - remove all buffers for a given lock from the AIL
308 * None of the buffers should be dirty, locked, or pinned.
311 void gfs2_ail_empty_gl(struct gfs2_glock *gl)
313 struct gfs2_sbd *sdp = gl->gl_sbd;
315 struct list_head *head = &gl->gl_ail_list;
316 struct gfs2_bufdata *bd;
317 struct buffer_head *bh;
321 blocks = atomic_read(&gl->gl_ail_count);
325 error = gfs2_trans_begin(sdp, 0, blocks);
326 if (gfs2_assert_withdraw(sdp, !error))
330 while (!list_empty(head)) {
331 bd = list_entry(head->next, struct gfs2_bufdata,
334 blkno = bh->b_blocknr;
335 gfs2_assert_withdraw(sdp, !buffer_busy(bh));
338 list_del(&bd->bd_ail_st_list);
339 list_del(&bd->bd_ail_gl_list);
340 atomic_dec(&gl->gl_ail_count);
342 gfs2_log_unlock(sdp);
344 gfs2_trans_add_revoke(sdp, blkno);
348 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
349 gfs2_log_unlock(sdp);
356 * gfs2_meta_inval - Invalidate all buffers associated with a glock
361 void gfs2_meta_inval(struct gfs2_glock *gl)
363 struct gfs2_sbd *sdp = gl->gl_sbd;
364 struct inode *aspace = gl->gl_aspace;
365 struct address_space *mapping = gl->gl_aspace->i_mapping;
367 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
369 atomic_inc(&aspace->i_writecount);
370 truncate_inode_pages(mapping, 0);
371 atomic_dec(&aspace->i_writecount);
373 gfs2_assert_withdraw(sdp, !mapping->nrpages);
377 * gfs2_meta_sync - Sync all buffers associated with a glock
379 * @flags: DIO_START | DIO_WAIT
383 void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
385 struct address_space *mapping = gl->gl_aspace->i_mapping;
388 if (flags & DIO_START)
389 filemap_fdatawrite(mapping);
390 if (!error && (flags & DIO_WAIT))
391 error = filemap_fdatawait(mapping);
394 gfs2_io_error(gl->gl_sbd);
398 * getbuf - Get a buffer with a given address space
399 * @sdp: the filesystem
400 * @aspace: the address space
401 * @blkno: the block number (filesystem scope)
402 * @create: 1 if the buffer should be created
404 * Returns: the buffer
407 static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
408 uint64_t blkno, int create)
411 struct buffer_head *bh;
416 shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
417 index = blkno >> shift; /* convert block to page */
418 bufnum = blkno - (index << shift); /* block buf index within page */
422 page = grab_cache_page(aspace->i_mapping, index);
428 page = find_lock_page(aspace->i_mapping, index);
433 if (!page_has_buffers(page))
434 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
436 /* Locate header for our buffer within our page */
437 for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
441 if (!buffer_mapped(bh))
442 map_bh(bh, sdp->sd_vfs, blkno);
445 mark_page_accessed(page);
446 page_cache_release(page);
451 static void meta_prep_new(struct buffer_head *bh)
453 struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
456 clear_buffer_dirty(bh);
457 set_buffer_uptodate(bh);
460 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
464 * gfs2_meta_new - Get a block
465 * @gl: The glock associated with this block
466 * @blkno: The block number
468 * Returns: The buffer
471 struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, uint64_t blkno)
473 struct buffer_head *bh;
474 bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
480 * gfs2_meta_read - Read a block from disk
481 * @gl: The glock covering the block
482 * @blkno: The block number
483 * @flags: flags to gfs2_dreread()
484 * @bhp: the place where the buffer is returned (NULL on failure)
489 int gfs2_meta_read(struct gfs2_glock *gl, uint64_t blkno, int flags,
490 struct buffer_head **bhp)
494 *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
495 error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags);
503 * gfs2_meta_reread - Reread a block from disk
504 * @sdp: the filesystem
505 * @bh: The block to read
506 * @flags: Flags that control the read
511 int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
513 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
516 if (flags & DIO_FORCE)
517 clear_buffer_uptodate(bh);
519 if ((flags & DIO_START) && !buffer_uptodate(bh))
520 ll_rw_block(READ, 1, &bh);
522 if (flags & DIO_WAIT) {
525 if (!buffer_uptodate(bh)) {
526 struct gfs2_trans *tr = get_transaction;
527 if (tr && tr->tr_touched)
528 gfs2_io_error_bh(sdp, bh);
531 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
539 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
540 * @gl: the glock the buffer belongs to
541 * @bh: The buffer to be attached to
542 * @meta: Flag to indicate whether its metadata or not
545 void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh, int meta)
547 struct gfs2_bufdata *bd;
550 lock_page(bh->b_page);
554 unlock_page(bh->b_page);
558 bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
559 memset(bd, 0, sizeof(struct gfs2_bufdata));
564 INIT_LIST_HEAD(&bd->bd_list_tr);
566 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
568 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
574 unlock_page(bh->b_page);
578 * gfs2_pin - Pin a buffer in memory
579 * @sdp: the filesystem the buffer belongs to
580 * @bh: The buffer to be pinned
584 void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
586 struct gfs2_bufdata *bd = get_v2bd(bh);
588 gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
590 if (test_set_buffer_pinned(bh))
591 gfs2_assert_withdraw(sdp, 0);
595 /* If this buffer is in the AIL and it has already been written
596 to in-place disk block, remove it from the AIL. */
599 if (bd->bd_ail && !buffer_in_io(bh))
600 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
601 gfs2_log_unlock(sdp);
603 clear_buffer_dirty(bh);
606 if (!buffer_uptodate(bh))
607 gfs2_io_error_bh(sdp, bh);
613 * gfs2_unpin - Unpin a buffer
614 * @sdp: the filesystem the buffer belongs to
615 * @bh: The buffer to unpin
620 void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
623 struct gfs2_bufdata *bd = get_v2bd(bh);
625 gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
627 if (!buffer_pinned(bh))
628 gfs2_assert_withdraw(sdp, 0);
630 mark_buffer_dirty(bh);
631 clear_buffer_pinned(bh);
635 list_del(&bd->bd_ail_st_list);
638 struct gfs2_glock *gl = bd->bd_gl;
639 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
640 atomic_inc(&gl->gl_ail_count);
643 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
644 gfs2_log_unlock(sdp);
648 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
649 * @ip: the inode who owns the buffers
650 * @bstart: the first buffer in the run
651 * @blen: the number of buffers in the run
655 void gfs2_meta_wipe(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
657 struct gfs2_sbd *sdp = ip->i_sbd;
658 struct inode *aspace = ip->i_gl->gl_aspace;
659 struct buffer_head *bh;
662 bh = getbuf(sdp, aspace, bstart, NO_CREATE);
664 struct gfs2_bufdata *bd = get_v2bd(bh);
666 if (test_clear_buffer_pinned(bh)) {
668 list_del_init(&bd->bd_le.le_list);
669 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
670 sdp->sd_log_num_buf--;
671 gfs2_log_unlock(sdp);
672 get_transaction->tr_num_buf_rm++;
678 uint64_t blkno = bh->b_blocknr;
680 list_del(&bd->bd_ail_st_list);
681 list_del(&bd->bd_ail_gl_list);
682 atomic_dec(&bd->bd_gl->gl_ail_count);
684 gfs2_log_unlock(sdp);
685 gfs2_trans_add_revoke(sdp, blkno);
687 gfs2_log_unlock(sdp);
691 clear_buffer_dirty(bh);
692 clear_buffer_uptodate(bh);
704 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
705 * @ip: The GFS2 inode
707 * This releases buffers that are in the most-recently-used array of
708 * blocks used for indirect block addressing for this inode.
711 void gfs2_meta_cache_flush(struct gfs2_inode *ip)
713 struct buffer_head **bh_slot;
716 spin_lock(&ip->i_spin);
718 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
719 bh_slot = &ip->i_cache[x];
726 spin_unlock(&ip->i_spin);
730 * gfs2_meta_indirect_buffer - Get a metadata buffer
731 * @ip: The GFS2 inode
732 * @height: The level of this buf in the metadata (indir addr) tree (if any)
733 * @num: The block number (device relative) of the buffer
734 * @new: Non-zero if we may create a new buffer
735 * @bhp: the buffer is returned here
737 * Try to use the gfs2_inode's MRU metadata tree cache.
742 int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, uint64_t num,
743 int new, struct buffer_head **bhp)
745 struct buffer_head *bh, **bh_slot = ip->i_cache + height;
748 spin_lock(&ip->i_spin);
751 if (bh->b_blocknr == num)
756 spin_unlock(&ip->i_spin);
762 error = gfs2_meta_reread(ip->i_sbd, bh,
763 DIO_START | DIO_WAIT);
771 bh = gfs2_meta_new(ip->i_gl, num);
773 error = gfs2_meta_read(ip->i_gl, num,
774 DIO_START | DIO_WAIT, &bh);
779 spin_lock(&ip->i_spin);
780 if (*bh_slot != bh) {
785 spin_unlock(&ip->i_spin);
789 if (gfs2_assert_warn(ip->i_sbd, height)) {
793 gfs2_trans_add_bh(ip->i_gl, bh, 1);
794 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
795 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
797 } else if (gfs2_metatype_check(ip->i_sbd, bh,
798 (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
809 * gfs2_meta_ra - start readahead on an extent of a file
810 * @gl: the glock the blocks belong to
811 * @dblock: the starting disk block
812 * @extlen: the number of blocks in the extent
816 void gfs2_meta_ra(struct gfs2_glock *gl, uint64_t dblock, uint32_t extlen)
818 struct gfs2_sbd *sdp = gl->gl_sbd;
819 struct inode *aspace = gl->gl_aspace;
820 struct buffer_head *first_bh, *bh;
821 uint32_t max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> sdp->sd_sb.sb_bsize_shift;
824 if (!extlen || !max_ra)
829 first_bh = getbuf(sdp, aspace, dblock, CREATE);
831 if (buffer_uptodate(first_bh))
833 if (!buffer_locked(first_bh)) {
834 error = gfs2_meta_reread(sdp, first_bh, DIO_START);
843 bh = getbuf(sdp, aspace, dblock, CREATE);
845 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
846 error = gfs2_meta_reread(sdp, bh, DIO_START);
856 if (buffer_uptodate(first_bh))
865 * gfs2_meta_syncfs - sync all the buffers in a filesystem
866 * @sdp: the filesystem
870 void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
874 gfs2_ail1_start(sdp, DIO_ALL);
875 if (gfs2_ail1_empty(sdp, DIO_ALL))