1b3b94faaSDavid Teigland /* 2b3b94faaSDavid Teigland * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 3da6dd40dSBob Peterson * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. 4b3b94faaSDavid Teigland * 5b3b94faaSDavid Teigland * This copyrighted material is made available to anyone wishing to use, 6b3b94faaSDavid Teigland * modify, copy, or redistribute it subject to the terms and conditions 7e9fc2aa0SSteven Whitehouse * of the GNU General Public License version 2. 8b3b94faaSDavid Teigland */ 9b3b94faaSDavid Teigland 10b3b94faaSDavid Teigland #include <linux/sched.h> 11b3b94faaSDavid Teigland #include <linux/slab.h> 12b3b94faaSDavid Teigland #include <linux/spinlock.h> 13b3b94faaSDavid Teigland #include <linux/completion.h> 14b3b94faaSDavid Teigland #include <linux/buffer_head.h> 155c676f6dSSteven Whitehouse #include <linux/gfs2_ondisk.h> 1671b86f56SSteven Whitehouse #include <linux/crc32.h> 17a25311c8SSteven Whitehouse #include <linux/delay.h> 18ec69b188SSteven Whitehouse #include <linux/kthread.h> 19ec69b188SSteven Whitehouse #include <linux/freezer.h> 20254db57fSSteven Whitehouse #include <linux/bio.h> 214667a0ecSSteven Whitehouse #include <linux/writeback.h> 22b3b94faaSDavid Teigland 23b3b94faaSDavid Teigland #include "gfs2.h" 245c676f6dSSteven Whitehouse #include "incore.h" 25b3b94faaSDavid Teigland #include "bmap.h" 26b3b94faaSDavid Teigland #include "glock.h" 27b3b94faaSDavid Teigland #include "log.h" 28b3b94faaSDavid Teigland #include "lops.h" 29b3b94faaSDavid Teigland #include "meta_io.h" 305c676f6dSSteven Whitehouse #include "util.h" 3171b86f56SSteven Whitehouse #include "dir.h" 3263997775SSteven Whitehouse #include "trace_gfs2.h" 33b3b94faaSDavid Teigland 34b3b94faaSDavid Teigland #define PULL 1 35b3b94faaSDavid Teigland 36b3b94faaSDavid Teigland /** 37b3b94faaSDavid Teigland * gfs2_struct2blk - compute stuff 38b3b94faaSDavid Teigland * @sdp: the filesystem 39b3b94faaSDavid Teigland * @nstruct: the number of structures 40b3b94faaSDavid Teigland * @ssize: the size of the structures 41b3b94faaSDavid Teigland * 42b3b94faaSDavid Teigland * Compute the number of log descriptor blocks needed to hold a certain number 43b3b94faaSDavid Teigland * of structures of a certain size. 44b3b94faaSDavid Teigland * 45b3b94faaSDavid Teigland * Returns: the number of blocks needed (minimum is always 1) 46b3b94faaSDavid Teigland */ 47b3b94faaSDavid Teigland 48b3b94faaSDavid Teigland unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct, 49b3b94faaSDavid Teigland unsigned int ssize) 50b3b94faaSDavid Teigland { 51b3b94faaSDavid Teigland unsigned int blks; 52b3b94faaSDavid Teigland unsigned int first, second; 53b3b94faaSDavid Teigland 54b3b94faaSDavid Teigland blks = 1; 55faa31ce8SSteven Whitehouse first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize; 56b3b94faaSDavid Teigland 57b3b94faaSDavid Teigland if (nstruct > first) { 58568f4c96SSteven Whitehouse second = (sdp->sd_sb.sb_bsize - 59568f4c96SSteven Whitehouse sizeof(struct gfs2_meta_header)) / ssize; 605c676f6dSSteven Whitehouse blks += DIV_ROUND_UP(nstruct - first, second); 61b3b94faaSDavid Teigland } 62b3b94faaSDavid Teigland 63b3b94faaSDavid Teigland return blks; 64b3b94faaSDavid Teigland } 65b3b94faaSDavid Teigland 66ddacfaf7SSteven Whitehouse /** 671e1a3d03SSteven Whitehouse * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters 681e1a3d03SSteven Whitehouse * @mapping: The associated mapping (maybe NULL) 691e1a3d03SSteven Whitehouse * @bd: The gfs2_bufdata to remove 701e1a3d03SSteven Whitehouse * 71c618e87aSSteven Whitehouse * The ail lock _must_ be held when calling this function 721e1a3d03SSteven Whitehouse * 731e1a3d03SSteven Whitehouse */ 741e1a3d03SSteven Whitehouse 75f91a0d3eSSteven Whitehouse void gfs2_remove_from_ail(struct gfs2_bufdata *bd) 761e1a3d03SSteven Whitehouse { 771e1a3d03SSteven Whitehouse bd->bd_ail = NULL; 781ad38c43SSteven Whitehouse list_del_init(&bd->bd_ail_st_list); 791ad38c43SSteven Whitehouse list_del_init(&bd->bd_ail_gl_list); 801e1a3d03SSteven Whitehouse atomic_dec(&bd->bd_gl->gl_ail_count); 811e1a3d03SSteven Whitehouse brelse(bd->bd_bh); 821e1a3d03SSteven Whitehouse } 831e1a3d03SSteven Whitehouse 841e1a3d03SSteven Whitehouse /** 85ddacfaf7SSteven Whitehouse * gfs2_ail1_start_one - Start I/O on a part of the AIL 86ddacfaf7SSteven Whitehouse * @sdp: the filesystem 874667a0ecSSteven Whitehouse * @wbc: The writeback control structure 884667a0ecSSteven Whitehouse * @ai: The ail structure 89ddacfaf7SSteven Whitehouse * 90ddacfaf7SSteven Whitehouse */ 91ddacfaf7SSteven Whitehouse 924f1de018SSteven Whitehouse static int gfs2_ail1_start_one(struct gfs2_sbd *sdp, 934667a0ecSSteven Whitehouse struct writeback_control *wbc, 944667a0ecSSteven Whitehouse struct gfs2_ail *ai) 95d6a079e8SDave Chinner __releases(&sdp->sd_ail_lock) 96d6a079e8SDave Chinner __acquires(&sdp->sd_ail_lock) 97ddacfaf7SSteven Whitehouse { 985ac048bbSSteven Whitehouse struct gfs2_glock *gl = NULL; 994667a0ecSSteven Whitehouse struct address_space *mapping; 100ddacfaf7SSteven Whitehouse struct gfs2_bufdata *bd, *s; 101ddacfaf7SSteven Whitehouse struct buffer_head *bh; 102ddacfaf7SSteven Whitehouse 1034667a0ecSSteven Whitehouse list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, bd_ail_st_list) { 104ddacfaf7SSteven Whitehouse bh = bd->bd_bh; 105ddacfaf7SSteven Whitehouse 106ddacfaf7SSteven Whitehouse gfs2_assert(sdp, bd->bd_ail == ai); 107ddacfaf7SSteven Whitehouse 108ddacfaf7SSteven Whitehouse if (!buffer_busy(bh)) { 10916615be1SSteven Whitehouse if (!buffer_uptodate(bh)) 110ddacfaf7SSteven Whitehouse gfs2_io_error_bh(sdp, bh); 111ddacfaf7SSteven Whitehouse list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list); 112ddacfaf7SSteven Whitehouse continue; 113ddacfaf7SSteven Whitehouse } 114ddacfaf7SSteven Whitehouse 115ddacfaf7SSteven Whitehouse if (!buffer_dirty(bh)) 116ddacfaf7SSteven Whitehouse continue; 1175ac048bbSSteven Whitehouse if (gl == bd->bd_gl) 1185ac048bbSSteven Whitehouse continue; 1195ac048bbSSteven Whitehouse gl = bd->bd_gl; 120ddacfaf7SSteven Whitehouse list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list); 1214667a0ecSSteven Whitehouse mapping = bh->b_page->mapping; 1224f1de018SSteven Whitehouse if (!mapping) 1234f1de018SSteven Whitehouse continue; 124d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 1254667a0ecSSteven Whitehouse generic_writepages(mapping, wbc); 126d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 1274667a0ecSSteven Whitehouse if (wbc->nr_to_write <= 0) 128ddacfaf7SSteven Whitehouse break; 1294f1de018SSteven Whitehouse return 1; 130ddacfaf7SSteven Whitehouse } 1314f1de018SSteven Whitehouse 1324f1de018SSteven Whitehouse return 0; 1334667a0ecSSteven Whitehouse } 1344667a0ecSSteven Whitehouse 1354667a0ecSSteven Whitehouse 1364667a0ecSSteven Whitehouse /** 1374667a0ecSSteven Whitehouse * gfs2_ail1_flush - start writeback of some ail1 entries 1384667a0ecSSteven Whitehouse * @sdp: The super block 1394667a0ecSSteven Whitehouse * @wbc: The writeback control structure 1404667a0ecSSteven Whitehouse * 1414667a0ecSSteven Whitehouse * Writes back some ail1 entries, according to the limits in the 1424667a0ecSSteven Whitehouse * writeback control structure 1434667a0ecSSteven Whitehouse */ 1444667a0ecSSteven Whitehouse 1454667a0ecSSteven Whitehouse void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc) 1464667a0ecSSteven Whitehouse { 1474667a0ecSSteven Whitehouse struct list_head *head = &sdp->sd_ail1_list; 1484667a0ecSSteven Whitehouse struct gfs2_ail *ai; 1494667a0ecSSteven Whitehouse 150c83ae9caSSteven Whitehouse trace_gfs2_ail_flush(sdp, wbc, 1); 1514667a0ecSSteven Whitehouse spin_lock(&sdp->sd_ail_lock); 1524f1de018SSteven Whitehouse restart: 1534667a0ecSSteven Whitehouse list_for_each_entry_reverse(ai, head, ai_list) { 1544667a0ecSSteven Whitehouse if (wbc->nr_to_write <= 0) 1554667a0ecSSteven Whitehouse break; 1564f1de018SSteven Whitehouse if (gfs2_ail1_start_one(sdp, wbc, ai)) 1574f1de018SSteven Whitehouse goto restart; 1584667a0ecSSteven Whitehouse } 1594667a0ecSSteven Whitehouse spin_unlock(&sdp->sd_ail_lock); 160c83ae9caSSteven Whitehouse trace_gfs2_ail_flush(sdp, wbc, 0); 1614667a0ecSSteven Whitehouse } 1624667a0ecSSteven Whitehouse 1634667a0ecSSteven Whitehouse /** 1644667a0ecSSteven Whitehouse * gfs2_ail1_start - start writeback of all ail1 entries 1654667a0ecSSteven Whitehouse * @sdp: The superblock 1664667a0ecSSteven Whitehouse */ 1674667a0ecSSteven Whitehouse 1684667a0ecSSteven Whitehouse static void gfs2_ail1_start(struct gfs2_sbd *sdp) 1694667a0ecSSteven Whitehouse { 1704667a0ecSSteven Whitehouse struct writeback_control wbc = { 1714667a0ecSSteven Whitehouse .sync_mode = WB_SYNC_NONE, 1724667a0ecSSteven Whitehouse .nr_to_write = LONG_MAX, 1734667a0ecSSteven Whitehouse .range_start = 0, 1744667a0ecSSteven Whitehouse .range_end = LLONG_MAX, 1754667a0ecSSteven Whitehouse }; 1764667a0ecSSteven Whitehouse 1774667a0ecSSteven Whitehouse return gfs2_ail1_flush(sdp, &wbc); 178ddacfaf7SSteven Whitehouse } 179ddacfaf7SSteven Whitehouse 180ddacfaf7SSteven Whitehouse /** 181ddacfaf7SSteven Whitehouse * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced 182ddacfaf7SSteven Whitehouse * @sdp: the filesystem 183ddacfaf7SSteven Whitehouse * @ai: the AIL entry 184ddacfaf7SSteven Whitehouse * 185ddacfaf7SSteven Whitehouse */ 186ddacfaf7SSteven Whitehouse 1874667a0ecSSteven Whitehouse static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) 188ddacfaf7SSteven Whitehouse { 189ddacfaf7SSteven Whitehouse struct gfs2_bufdata *bd, *s; 190ddacfaf7SSteven Whitehouse struct buffer_head *bh; 191ddacfaf7SSteven Whitehouse 192ddacfaf7SSteven Whitehouse list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, 193ddacfaf7SSteven Whitehouse bd_ail_st_list) { 194ddacfaf7SSteven Whitehouse bh = bd->bd_bh; 195ddacfaf7SSteven Whitehouse gfs2_assert(sdp, bd->bd_ail == ai); 1964667a0ecSSteven Whitehouse if (buffer_busy(bh)) 197ddacfaf7SSteven Whitehouse continue; 198ddacfaf7SSteven Whitehouse if (!buffer_uptodate(bh)) 199ddacfaf7SSteven Whitehouse gfs2_io_error_bh(sdp, bh); 200ddacfaf7SSteven Whitehouse list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list); 201ddacfaf7SSteven Whitehouse } 202ddacfaf7SSteven Whitehouse 203ddacfaf7SSteven Whitehouse } 204ddacfaf7SSteven Whitehouse 2054667a0ecSSteven Whitehouse /** 2064667a0ecSSteven Whitehouse * gfs2_ail1_empty - Try to empty the ail1 lists 2074667a0ecSSteven Whitehouse * @sdp: The superblock 2084667a0ecSSteven Whitehouse * 2094667a0ecSSteven Whitehouse * Tries to empty the ail1 lists, starting with the oldest first 2104667a0ecSSteven Whitehouse */ 211b3b94faaSDavid Teigland 2124667a0ecSSteven Whitehouse static int gfs2_ail1_empty(struct gfs2_sbd *sdp) 213b3b94faaSDavid Teigland { 214b3b94faaSDavid Teigland struct gfs2_ail *ai, *s; 215b3b94faaSDavid Teigland int ret; 216b3b94faaSDavid Teigland 217d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 218b3b94faaSDavid Teigland list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) { 2194667a0ecSSteven Whitehouse gfs2_ail1_empty_one(sdp, ai); 2204667a0ecSSteven Whitehouse if (list_empty(&ai->ai_ail1_list)) 221b3b94faaSDavid Teigland list_move(&ai->ai_list, &sdp->sd_ail2_list); 2224667a0ecSSteven Whitehouse else 223b3b94faaSDavid Teigland break; 224b3b94faaSDavid Teigland } 225b3b94faaSDavid Teigland ret = list_empty(&sdp->sd_ail1_list); 226d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 227b3b94faaSDavid Teigland 228b3b94faaSDavid Teigland return ret; 229b3b94faaSDavid Teigland } 230b3b94faaSDavid Teigland 23126b06a69SSteven Whitehouse static void gfs2_ail1_wait(struct gfs2_sbd *sdp) 23226b06a69SSteven Whitehouse { 23326b06a69SSteven Whitehouse struct gfs2_ail *ai; 23426b06a69SSteven Whitehouse struct gfs2_bufdata *bd; 23526b06a69SSteven Whitehouse struct buffer_head *bh; 23626b06a69SSteven Whitehouse 23726b06a69SSteven Whitehouse spin_lock(&sdp->sd_ail_lock); 23826b06a69SSteven Whitehouse list_for_each_entry_reverse(ai, &sdp->sd_ail1_list, ai_list) { 23926b06a69SSteven Whitehouse list_for_each_entry(bd, &ai->ai_ail1_list, bd_ail_st_list) { 24026b06a69SSteven Whitehouse bh = bd->bd_bh; 24126b06a69SSteven Whitehouse if (!buffer_locked(bh)) 24226b06a69SSteven Whitehouse continue; 24326b06a69SSteven Whitehouse get_bh(bh); 24426b06a69SSteven Whitehouse spin_unlock(&sdp->sd_ail_lock); 24526b06a69SSteven Whitehouse wait_on_buffer(bh); 24626b06a69SSteven Whitehouse brelse(bh); 24726b06a69SSteven Whitehouse return; 24826b06a69SSteven Whitehouse } 24926b06a69SSteven Whitehouse } 25026b06a69SSteven Whitehouse spin_unlock(&sdp->sd_ail_lock); 25126b06a69SSteven Whitehouse } 252ddacfaf7SSteven Whitehouse 253ddacfaf7SSteven Whitehouse /** 254ddacfaf7SSteven Whitehouse * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced 255ddacfaf7SSteven Whitehouse * @sdp: the filesystem 256ddacfaf7SSteven Whitehouse * @ai: the AIL entry 257ddacfaf7SSteven Whitehouse * 258ddacfaf7SSteven Whitehouse */ 259ddacfaf7SSteven Whitehouse 260ddacfaf7SSteven Whitehouse static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) 261ddacfaf7SSteven Whitehouse { 262ddacfaf7SSteven Whitehouse struct list_head *head = &ai->ai_ail2_list; 263ddacfaf7SSteven Whitehouse struct gfs2_bufdata *bd; 264ddacfaf7SSteven Whitehouse 265ddacfaf7SSteven Whitehouse while (!list_empty(head)) { 266ddacfaf7SSteven Whitehouse bd = list_entry(head->prev, struct gfs2_bufdata, 267ddacfaf7SSteven Whitehouse bd_ail_st_list); 268ddacfaf7SSteven Whitehouse gfs2_assert(sdp, bd->bd_ail == ai); 269f91a0d3eSSteven Whitehouse gfs2_remove_from_ail(bd); 270ddacfaf7SSteven Whitehouse } 271ddacfaf7SSteven Whitehouse } 272ddacfaf7SSteven Whitehouse 273b3b94faaSDavid Teigland static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail) 274b3b94faaSDavid Teigland { 275b3b94faaSDavid Teigland struct gfs2_ail *ai, *safe; 276b3b94faaSDavid Teigland unsigned int old_tail = sdp->sd_log_tail; 277b3b94faaSDavid Teigland int wrap = (new_tail < old_tail); 278b3b94faaSDavid Teigland int a, b, rm; 279b3b94faaSDavid Teigland 280d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 281b3b94faaSDavid Teigland 282b3b94faaSDavid Teigland list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) { 283b3b94faaSDavid Teigland a = (old_tail <= ai->ai_first); 284b3b94faaSDavid Teigland b = (ai->ai_first < new_tail); 285b3b94faaSDavid Teigland rm = (wrap) ? (a || b) : (a && b); 286b3b94faaSDavid Teigland if (!rm) 287b3b94faaSDavid Teigland continue; 288b3b94faaSDavid Teigland 289b3b94faaSDavid Teigland gfs2_ail2_empty_one(sdp, ai); 290b3b94faaSDavid Teigland list_del(&ai->ai_list); 291b3b94faaSDavid Teigland gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list)); 292b3b94faaSDavid Teigland gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list)); 293b3b94faaSDavid Teigland kfree(ai); 294b3b94faaSDavid Teigland } 295b3b94faaSDavid Teigland 296d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 297b3b94faaSDavid Teigland } 298b3b94faaSDavid Teigland 299b3b94faaSDavid Teigland /** 300b3b94faaSDavid Teigland * gfs2_log_reserve - Make a log reservation 301b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 302b3b94faaSDavid Teigland * @blks: The number of blocks to reserve 303b3b94faaSDavid Teigland * 30489918647SSteven Whitehouse * Note that we never give out the last few blocks of the journal. Thats 3052332c443SRobert Peterson * due to the fact that there is a small number of header blocks 306b004157aSSteven Whitehouse * associated with each log flush. The exact number can't be known until 307b004157aSSteven Whitehouse * flush time, so we ensure that we have just enough free blocks at all 308b004157aSSteven Whitehouse * times to avoid running out during a log flush. 309b004157aSSteven Whitehouse * 3105e687eacSBenjamin Marzinski * We no longer flush the log here, instead we wake up logd to do that 3115e687eacSBenjamin Marzinski * for us. To avoid the thundering herd and to ensure that we deal fairly 3125e687eacSBenjamin Marzinski * with queued waiters, we use an exclusive wait. This means that when we 3135e687eacSBenjamin Marzinski * get woken with enough journal space to get our reservation, we need to 3145e687eacSBenjamin Marzinski * wake the next waiter on the list. 3155e687eacSBenjamin Marzinski * 316b3b94faaSDavid Teigland * Returns: errno 317b3b94faaSDavid Teigland */ 318b3b94faaSDavid Teigland 319b3b94faaSDavid Teigland int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks) 320b3b94faaSDavid Teigland { 32189918647SSteven Whitehouse unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize); 3225e687eacSBenjamin Marzinski unsigned wanted = blks + reserved_blks; 3235e687eacSBenjamin Marzinski DEFINE_WAIT(wait); 3245e687eacSBenjamin Marzinski int did_wait = 0; 3255e687eacSBenjamin Marzinski unsigned int free_blocks; 326b3b94faaSDavid Teigland 327b3b94faaSDavid Teigland if (gfs2_assert_warn(sdp, blks) || 328b3b94faaSDavid Teigland gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks)) 329b3b94faaSDavid Teigland return -EINVAL; 3305e687eacSBenjamin Marzinski retry: 3315e687eacSBenjamin Marzinski free_blocks = atomic_read(&sdp->sd_log_blks_free); 3325e687eacSBenjamin Marzinski if (unlikely(free_blocks <= wanted)) { 3335e687eacSBenjamin Marzinski do { 3345e687eacSBenjamin Marzinski prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait, 3355e687eacSBenjamin Marzinski TASK_UNINTERRUPTIBLE); 3365e687eacSBenjamin Marzinski wake_up(&sdp->sd_logd_waitq); 3375e687eacSBenjamin Marzinski did_wait = 1; 3385e687eacSBenjamin Marzinski if (atomic_read(&sdp->sd_log_blks_free) <= wanted) 3395e687eacSBenjamin Marzinski io_schedule(); 3405e687eacSBenjamin Marzinski free_blocks = atomic_read(&sdp->sd_log_blks_free); 3415e687eacSBenjamin Marzinski } while(free_blocks <= wanted); 3425e687eacSBenjamin Marzinski finish_wait(&sdp->sd_log_waitq, &wait); 343b3b94faaSDavid Teigland } 3445e687eacSBenjamin Marzinski if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks, 3455e687eacSBenjamin Marzinski free_blocks - blks) != free_blocks) 3465e687eacSBenjamin Marzinski goto retry; 34763997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, -blks); 3485e687eacSBenjamin Marzinski 3495e687eacSBenjamin Marzinski /* 3505e687eacSBenjamin Marzinski * If we waited, then so might others, wake them up _after_ we get 3515e687eacSBenjamin Marzinski * our share of the log. 3525e687eacSBenjamin Marzinski */ 3535e687eacSBenjamin Marzinski if (unlikely(did_wait)) 3545e687eacSBenjamin Marzinski wake_up(&sdp->sd_log_waitq); 355484adff8SSteven Whitehouse 356484adff8SSteven Whitehouse down_read(&sdp->sd_log_flush_lock); 357b3b94faaSDavid Teigland 358b3b94faaSDavid Teigland return 0; 359b3b94faaSDavid Teigland } 360b3b94faaSDavid Teigland 361cd915493SSteven Whitehouse static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn) 362b3b94faaSDavid Teigland { 363da6dd40dSBob Peterson struct gfs2_journal_extent *je; 364b3b94faaSDavid Teigland 365da6dd40dSBob Peterson list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) { 366da6dd40dSBob Peterson if (lbn >= je->lblock && lbn < je->lblock + je->blocks) 367ff91cc9bSSteven Whitehouse return je->dblock + lbn - je->lblock; 368da6dd40dSBob Peterson } 369b3b94faaSDavid Teigland 370da6dd40dSBob Peterson return -1; 371b3b94faaSDavid Teigland } 372b3b94faaSDavid Teigland 373b3b94faaSDavid Teigland /** 374b3b94faaSDavid Teigland * log_distance - Compute distance between two journal blocks 375b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 376b3b94faaSDavid Teigland * @newer: The most recent journal block of the pair 377b3b94faaSDavid Teigland * @older: The older journal block of the pair 378b3b94faaSDavid Teigland * 379b3b94faaSDavid Teigland * Compute the distance (in the journal direction) between two 380b3b94faaSDavid Teigland * blocks in the journal 381b3b94faaSDavid Teigland * 382b3b94faaSDavid Teigland * Returns: the distance in blocks 383b3b94faaSDavid Teigland */ 384b3b94faaSDavid Teigland 385faa31ce8SSteven Whitehouse static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer, 386b3b94faaSDavid Teigland unsigned int older) 387b3b94faaSDavid Teigland { 388b3b94faaSDavid Teigland int dist; 389b3b94faaSDavid Teigland 390b3b94faaSDavid Teigland dist = newer - older; 391b3b94faaSDavid Teigland if (dist < 0) 392b3b94faaSDavid Teigland dist += sdp->sd_jdesc->jd_blocks; 393b3b94faaSDavid Teigland 394b3b94faaSDavid Teigland return dist; 395b3b94faaSDavid Teigland } 396b3b94faaSDavid Teigland 3972332c443SRobert Peterson /** 3982332c443SRobert Peterson * calc_reserved - Calculate the number of blocks to reserve when 3992332c443SRobert Peterson * refunding a transaction's unused buffers. 4002332c443SRobert Peterson * @sdp: The GFS2 superblock 4012332c443SRobert Peterson * 4022332c443SRobert Peterson * This is complex. We need to reserve room for all our currently used 4032332c443SRobert Peterson * metadata buffers (e.g. normal file I/O rewriting file time stamps) and 4042332c443SRobert Peterson * all our journaled data buffers for journaled files (e.g. files in the 4052332c443SRobert Peterson * meta_fs like rindex, or files for which chattr +j was done.) 4062332c443SRobert Peterson * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush 4072332c443SRobert Peterson * will count it as free space (sd_log_blks_free) and corruption will follow. 4082332c443SRobert Peterson * 4092332c443SRobert Peterson * We can have metadata bufs and jdata bufs in the same journal. So each 4102332c443SRobert Peterson * type gets its own log header, for which we need to reserve a block. 4112332c443SRobert Peterson * In fact, each type has the potential for needing more than one header 4122332c443SRobert Peterson * in cases where we have more buffers than will fit on a journal page. 4132332c443SRobert Peterson * Metadata journal entries take up half the space of journaled buffer entries. 4142332c443SRobert Peterson * Thus, metadata entries have buf_limit (502) and journaled buffers have 4152332c443SRobert Peterson * databuf_limit (251) before they cause a wrap around. 4162332c443SRobert Peterson * 4172332c443SRobert Peterson * Also, we need to reserve blocks for revoke journal entries and one for an 4182332c443SRobert Peterson * overall header for the lot. 4192332c443SRobert Peterson * 4202332c443SRobert Peterson * Returns: the number of blocks reserved 4212332c443SRobert Peterson */ 4222332c443SRobert Peterson static unsigned int calc_reserved(struct gfs2_sbd *sdp) 4232332c443SRobert Peterson { 4242332c443SRobert Peterson unsigned int reserved = 0; 4252332c443SRobert Peterson unsigned int mbuf_limit, metabufhdrs_needed; 4262332c443SRobert Peterson unsigned int dbuf_limit, databufhdrs_needed; 4272332c443SRobert Peterson unsigned int revokes = 0; 4282332c443SRobert Peterson 4292332c443SRobert Peterson mbuf_limit = buf_limit(sdp); 4302332c443SRobert Peterson metabufhdrs_needed = (sdp->sd_log_commited_buf + 4312332c443SRobert Peterson (mbuf_limit - 1)) / mbuf_limit; 4322332c443SRobert Peterson dbuf_limit = databuf_limit(sdp); 4332332c443SRobert Peterson databufhdrs_needed = (sdp->sd_log_commited_databuf + 4342332c443SRobert Peterson (dbuf_limit - 1)) / dbuf_limit; 4352332c443SRobert Peterson 4362e95e3f6SBenjamin Marzinski if (sdp->sd_log_commited_revoke > 0) 4372332c443SRobert Peterson revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke, 4382332c443SRobert Peterson sizeof(u64)); 4392332c443SRobert Peterson 4402332c443SRobert Peterson reserved = sdp->sd_log_commited_buf + metabufhdrs_needed + 4412332c443SRobert Peterson sdp->sd_log_commited_databuf + databufhdrs_needed + 4422332c443SRobert Peterson revokes; 4432332c443SRobert Peterson /* One for the overall header */ 4442332c443SRobert Peterson if (reserved) 4452332c443SRobert Peterson reserved++; 4462332c443SRobert Peterson return reserved; 4472332c443SRobert Peterson } 4482332c443SRobert Peterson 449b3b94faaSDavid Teigland static unsigned int current_tail(struct gfs2_sbd *sdp) 450b3b94faaSDavid Teigland { 451b3b94faaSDavid Teigland struct gfs2_ail *ai; 452b3b94faaSDavid Teigland unsigned int tail; 453b3b94faaSDavid Teigland 454d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 455b3b94faaSDavid Teigland 456faa31ce8SSteven Whitehouse if (list_empty(&sdp->sd_ail1_list)) { 457b3b94faaSDavid Teigland tail = sdp->sd_log_head; 458faa31ce8SSteven Whitehouse } else { 459faa31ce8SSteven Whitehouse ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list); 460b3b94faaSDavid Teigland tail = ai->ai_first; 461b3b94faaSDavid Teigland } 462b3b94faaSDavid Teigland 463d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 464b3b94faaSDavid Teigland 465b3b94faaSDavid Teigland return tail; 466b3b94faaSDavid Teigland } 467b3b94faaSDavid Teigland 46816615be1SSteven Whitehouse void gfs2_log_incr_head(struct gfs2_sbd *sdp) 469b3b94faaSDavid Teigland { 470b3b94faaSDavid Teigland if (sdp->sd_log_flush_head == sdp->sd_log_tail) 47116615be1SSteven Whitehouse BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head); 472b3b94faaSDavid Teigland 473b3b94faaSDavid Teigland if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) { 474b3b94faaSDavid Teigland sdp->sd_log_flush_head = 0; 475b3b94faaSDavid Teigland sdp->sd_log_flush_wrapped = 1; 476b3b94faaSDavid Teigland } 477b3b94faaSDavid Teigland } 478b3b94faaSDavid Teigland 479b3b94faaSDavid Teigland /** 48016615be1SSteven Whitehouse * gfs2_log_write_endio - End of I/O for a log buffer 48116615be1SSteven Whitehouse * @bh: The buffer head 48216615be1SSteven Whitehouse * @uptodate: I/O Status 48316615be1SSteven Whitehouse * 48416615be1SSteven Whitehouse */ 48516615be1SSteven Whitehouse 48616615be1SSteven Whitehouse static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate) 48716615be1SSteven Whitehouse { 48816615be1SSteven Whitehouse struct gfs2_sbd *sdp = bh->b_private; 48916615be1SSteven Whitehouse bh->b_private = NULL; 49016615be1SSteven Whitehouse 49116615be1SSteven Whitehouse end_buffer_write_sync(bh, uptodate); 49216615be1SSteven Whitehouse if (atomic_dec_and_test(&sdp->sd_log_in_flight)) 49316615be1SSteven Whitehouse wake_up(&sdp->sd_log_flush_wait); 49416615be1SSteven Whitehouse } 49516615be1SSteven Whitehouse 49616615be1SSteven Whitehouse /** 497b3b94faaSDavid Teigland * gfs2_log_get_buf - Get and initialize a buffer to use for log control data 498b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 499b3b94faaSDavid Teigland * 500b3b94faaSDavid Teigland * Returns: the buffer_head 501b3b94faaSDavid Teigland */ 502b3b94faaSDavid Teigland 503b3b94faaSDavid Teigland struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp) 504b3b94faaSDavid Teigland { 505cd915493SSteven Whitehouse u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); 506b3b94faaSDavid Teigland struct buffer_head *bh; 507b3b94faaSDavid Teigland 50816615be1SSteven Whitehouse bh = sb_getblk(sdp->sd_vfs, blkno); 509b3b94faaSDavid Teigland lock_buffer(bh); 510b3b94faaSDavid Teigland memset(bh->b_data, 0, bh->b_size); 511b3b94faaSDavid Teigland set_buffer_uptodate(bh); 512b3b94faaSDavid Teigland clear_buffer_dirty(bh); 51316615be1SSteven Whitehouse gfs2_log_incr_head(sdp); 51416615be1SSteven Whitehouse atomic_inc(&sdp->sd_log_in_flight); 51516615be1SSteven Whitehouse bh->b_private = sdp; 51616615be1SSteven Whitehouse bh->b_end_io = gfs2_log_write_endio; 517b3b94faaSDavid Teigland 518b3b94faaSDavid Teigland return bh; 519b3b94faaSDavid Teigland } 520b3b94faaSDavid Teigland 521b3b94faaSDavid Teigland /** 52216615be1SSteven Whitehouse * gfs2_fake_write_endio - 52316615be1SSteven Whitehouse * @bh: The buffer head 52416615be1SSteven Whitehouse * @uptodate: The I/O Status 52516615be1SSteven Whitehouse * 52616615be1SSteven Whitehouse */ 52716615be1SSteven Whitehouse 52816615be1SSteven Whitehouse static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate) 52916615be1SSteven Whitehouse { 53016615be1SSteven Whitehouse struct buffer_head *real_bh = bh->b_private; 5315a60c532SSteven Whitehouse struct gfs2_bufdata *bd = real_bh->b_private; 5325a60c532SSteven Whitehouse struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd; 53316615be1SSteven Whitehouse 53416615be1SSteven Whitehouse end_buffer_write_sync(bh, uptodate); 53516615be1SSteven Whitehouse free_buffer_head(bh); 53616615be1SSteven Whitehouse unlock_buffer(real_bh); 53716615be1SSteven Whitehouse brelse(real_bh); 53816615be1SSteven Whitehouse if (atomic_dec_and_test(&sdp->sd_log_in_flight)) 53916615be1SSteven Whitehouse wake_up(&sdp->sd_log_flush_wait); 54016615be1SSteven Whitehouse } 54116615be1SSteven Whitehouse 54216615be1SSteven Whitehouse /** 543b3b94faaSDavid Teigland * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log 544b3b94faaSDavid Teigland * @sdp: the filesystem 545b3b94faaSDavid Teigland * @data: the data the buffer_head should point to 546b3b94faaSDavid Teigland * 547b3b94faaSDavid Teigland * Returns: the log buffer descriptor 548b3b94faaSDavid Teigland */ 549b3b94faaSDavid Teigland 550b3b94faaSDavid Teigland struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp, 551b3b94faaSDavid Teigland struct buffer_head *real) 552b3b94faaSDavid Teigland { 553cd915493SSteven Whitehouse u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); 554b3b94faaSDavid Teigland struct buffer_head *bh; 555b3b94faaSDavid Teigland 55616615be1SSteven Whitehouse bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL); 557b3b94faaSDavid Teigland atomic_set(&bh->b_count, 1); 55816615be1SSteven Whitehouse bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock); 55918ec7d5cSSteven Whitehouse set_bh_page(bh, real->b_page, bh_offset(real)); 560b3b94faaSDavid Teigland bh->b_blocknr = blkno; 561b3b94faaSDavid Teigland bh->b_size = sdp->sd_sb.sb_bsize; 562b3b94faaSDavid Teigland bh->b_bdev = sdp->sd_vfs->s_bdev; 56316615be1SSteven Whitehouse bh->b_private = real; 56416615be1SSteven Whitehouse bh->b_end_io = gfs2_fake_write_endio; 565b3b94faaSDavid Teigland 56616615be1SSteven Whitehouse gfs2_log_incr_head(sdp); 56716615be1SSteven Whitehouse atomic_inc(&sdp->sd_log_in_flight); 568b3b94faaSDavid Teigland 569b3b94faaSDavid Teigland return bh; 570b3b94faaSDavid Teigland } 571b3b94faaSDavid Teigland 5722332c443SRobert Peterson static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail) 573b3b94faaSDavid Teigland { 574b3b94faaSDavid Teigland unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail); 575b3b94faaSDavid Teigland 576b3b94faaSDavid Teigland ail2_empty(sdp, new_tail); 577b3b94faaSDavid Teigland 578fd041f0bSSteven Whitehouse atomic_add(dist, &sdp->sd_log_blks_free); 57963997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, dist); 5805e687eacSBenjamin Marzinski gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= 5815e687eacSBenjamin Marzinski sdp->sd_jdesc->jd_blocks); 582b3b94faaSDavid Teigland 583b3b94faaSDavid Teigland sdp->sd_log_tail = new_tail; 584b3b94faaSDavid Teigland } 585b3b94faaSDavid Teigland 586b3b94faaSDavid Teigland /** 587b3b94faaSDavid Teigland * log_write_header - Get and initialize a journal header buffer 588b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 589b3b94faaSDavid Teigland * 590b3b94faaSDavid Teigland * Returns: the initialized log buffer descriptor 591b3b94faaSDavid Teigland */ 592b3b94faaSDavid Teigland 593cd915493SSteven Whitehouse static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull) 594b3b94faaSDavid Teigland { 595cd915493SSteven Whitehouse u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); 596b3b94faaSDavid Teigland struct buffer_head *bh; 597b3b94faaSDavid Teigland struct gfs2_log_header *lh; 598b3b94faaSDavid Teigland unsigned int tail; 599cd915493SSteven Whitehouse u32 hash; 600b3b94faaSDavid Teigland 601b3b94faaSDavid Teigland bh = sb_getblk(sdp->sd_vfs, blkno); 602b3b94faaSDavid Teigland lock_buffer(bh); 603b3b94faaSDavid Teigland memset(bh->b_data, 0, bh->b_size); 604b3b94faaSDavid Teigland set_buffer_uptodate(bh); 605b3b94faaSDavid Teigland clear_buffer_dirty(bh); 606b3b94faaSDavid Teigland 6074667a0ecSSteven Whitehouse gfs2_ail1_empty(sdp); 608b3b94faaSDavid Teigland tail = current_tail(sdp); 609b3b94faaSDavid Teigland 610b3b94faaSDavid Teigland lh = (struct gfs2_log_header *)bh->b_data; 611b3b94faaSDavid Teigland memset(lh, 0, sizeof(struct gfs2_log_header)); 612b3b94faaSDavid Teigland lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC); 613e3167dedSSteven Whitehouse lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH); 6140ab7d13fSSteven Whitehouse lh->lh_header.__pad0 = cpu_to_be64(0); 615e3167dedSSteven Whitehouse lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH); 6160ab7d13fSSteven Whitehouse lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid); 617e0f2bf78SSteven Whitehouse lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++); 618e0f2bf78SSteven Whitehouse lh->lh_flags = cpu_to_be32(flags); 619e0f2bf78SSteven Whitehouse lh->lh_tail = cpu_to_be32(tail); 620e0f2bf78SSteven Whitehouse lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head); 621b3b94faaSDavid Teigland hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header)); 622b3b94faaSDavid Teigland lh->lh_hash = cpu_to_be32(hash); 623b3b94faaSDavid Teigland 624254db57fSSteven Whitehouse bh->b_end_io = end_buffer_write_sync; 625f1e4d518SChristoph Hellwig get_bh(bh); 626254db57fSSteven Whitehouse if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) 62765299a3bSChristoph Hellwig submit_bh(WRITE_SYNC | REQ_META | REQ_PRIO, bh); 628f1e4d518SChristoph Hellwig else 62965299a3bSChristoph Hellwig submit_bh(WRITE_FLUSH_FUA | REQ_META | REQ_PRIO, bh); 630254db57fSSteven Whitehouse wait_on_buffer(bh); 631f1e4d518SChristoph Hellwig 632254db57fSSteven Whitehouse if (!buffer_uptodate(bh)) 633b3b94faaSDavid Teigland gfs2_io_error_bh(sdp, bh); 634b3b94faaSDavid Teigland brelse(bh); 635b3b94faaSDavid Teigland 636b3b94faaSDavid Teigland if (sdp->sd_log_tail != tail) 6372332c443SRobert Peterson log_pull_tail(sdp, tail); 638b3b94faaSDavid Teigland else 639b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !pull); 640b3b94faaSDavid Teigland 641b3b94faaSDavid Teigland sdp->sd_log_idle = (tail == sdp->sd_log_flush_head); 64216615be1SSteven Whitehouse gfs2_log_incr_head(sdp); 643b3b94faaSDavid Teigland } 644b3b94faaSDavid Teigland 645b3b94faaSDavid Teigland static void log_flush_commit(struct gfs2_sbd *sdp) 646b3b94faaSDavid Teigland { 64716615be1SSteven Whitehouse DEFINE_WAIT(wait); 648b3b94faaSDavid Teigland 64916615be1SSteven Whitehouse if (atomic_read(&sdp->sd_log_in_flight)) { 65016615be1SSteven Whitehouse do { 65116615be1SSteven Whitehouse prepare_to_wait(&sdp->sd_log_flush_wait, &wait, 65216615be1SSteven Whitehouse TASK_UNINTERRUPTIBLE); 65316615be1SSteven Whitehouse if (atomic_read(&sdp->sd_log_in_flight)) 65416615be1SSteven Whitehouse io_schedule(); 65516615be1SSteven Whitehouse } while(atomic_read(&sdp->sd_log_in_flight)); 65616615be1SSteven Whitehouse finish_wait(&sdp->sd_log_flush_wait, &wait); 657b3b94faaSDavid Teigland } 658b3b94faaSDavid Teigland 659b3b94faaSDavid Teigland log_write_header(sdp, 0, 0); 660b3b94faaSDavid Teigland } 661b3b94faaSDavid Teigland 662d7b616e2SSteven Whitehouse static void gfs2_ordered_write(struct gfs2_sbd *sdp) 663d7b616e2SSteven Whitehouse { 664d7b616e2SSteven Whitehouse struct gfs2_bufdata *bd; 665d7b616e2SSteven Whitehouse struct buffer_head *bh; 666d7b616e2SSteven Whitehouse LIST_HEAD(written); 667d7b616e2SSteven Whitehouse 668d7b616e2SSteven Whitehouse gfs2_log_lock(sdp); 669d7b616e2SSteven Whitehouse while (!list_empty(&sdp->sd_log_le_ordered)) { 670d7b616e2SSteven Whitehouse bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list); 671d7b616e2SSteven Whitehouse list_move(&bd->bd_le.le_list, &written); 672d7b616e2SSteven Whitehouse bh = bd->bd_bh; 673d7b616e2SSteven Whitehouse if (!buffer_dirty(bh)) 674d7b616e2SSteven Whitehouse continue; 675d7b616e2SSteven Whitehouse get_bh(bh); 676d7b616e2SSteven Whitehouse gfs2_log_unlock(sdp); 677d7b616e2SSteven Whitehouse lock_buffer(bh); 678b8e7cbb6SSteven Whitehouse if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) { 679d7b616e2SSteven Whitehouse bh->b_end_io = end_buffer_write_sync; 680721a9602SJens Axboe submit_bh(WRITE_SYNC, bh); 681d7b616e2SSteven Whitehouse } else { 682d7b616e2SSteven Whitehouse unlock_buffer(bh); 683d7b616e2SSteven Whitehouse brelse(bh); 684d7b616e2SSteven Whitehouse } 685d7b616e2SSteven Whitehouse gfs2_log_lock(sdp); 686d7b616e2SSteven Whitehouse } 687d7b616e2SSteven Whitehouse list_splice(&written, &sdp->sd_log_le_ordered); 688d7b616e2SSteven Whitehouse gfs2_log_unlock(sdp); 689d7b616e2SSteven Whitehouse } 690d7b616e2SSteven Whitehouse 691d7b616e2SSteven Whitehouse static void gfs2_ordered_wait(struct gfs2_sbd *sdp) 692d7b616e2SSteven Whitehouse { 693d7b616e2SSteven Whitehouse struct gfs2_bufdata *bd; 694d7b616e2SSteven Whitehouse struct buffer_head *bh; 695d7b616e2SSteven Whitehouse 696d7b616e2SSteven Whitehouse gfs2_log_lock(sdp); 697d7b616e2SSteven Whitehouse while (!list_empty(&sdp->sd_log_le_ordered)) { 698d7b616e2SSteven Whitehouse bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list); 699d7b616e2SSteven Whitehouse bh = bd->bd_bh; 700d7b616e2SSteven Whitehouse if (buffer_locked(bh)) { 701d7b616e2SSteven Whitehouse get_bh(bh); 702d7b616e2SSteven Whitehouse gfs2_log_unlock(sdp); 703d7b616e2SSteven Whitehouse wait_on_buffer(bh); 704d7b616e2SSteven Whitehouse brelse(bh); 705d7b616e2SSteven Whitehouse gfs2_log_lock(sdp); 706d7b616e2SSteven Whitehouse continue; 707d7b616e2SSteven Whitehouse } 708d7b616e2SSteven Whitehouse list_del_init(&bd->bd_le.le_list); 709d7b616e2SSteven Whitehouse } 710d7b616e2SSteven Whitehouse gfs2_log_unlock(sdp); 711d7b616e2SSteven Whitehouse } 712d7b616e2SSteven Whitehouse 713b3b94faaSDavid Teigland /** 714b09e593dSSteven Whitehouse * gfs2_log_flush - flush incore transaction(s) 715b3b94faaSDavid Teigland * @sdp: the filesystem 716b3b94faaSDavid Teigland * @gl: The glock structure to flush. If NULL, flush the whole incore log 717b3b94faaSDavid Teigland * 718b3b94faaSDavid Teigland */ 719b3b94faaSDavid Teigland 720ed4878e8SBob Peterson void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl) 721b3b94faaSDavid Teigland { 722b3b94faaSDavid Teigland struct gfs2_ail *ai; 723b3b94faaSDavid Teigland 724484adff8SSteven Whitehouse down_write(&sdp->sd_log_flush_lock); 725f55ab26aSSteven Whitehouse 7262bcd610dSSteven Whitehouse /* Log might have been flushed while we waited for the flush lock */ 7272bcd610dSSteven Whitehouse if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) { 728484adff8SSteven Whitehouse up_write(&sdp->sd_log_flush_lock); 729f55ab26aSSteven Whitehouse return; 730f55ab26aSSteven Whitehouse } 73163997775SSteven Whitehouse trace_gfs2_log_flush(sdp, 1); 732f55ab26aSSteven Whitehouse 733b09e593dSSteven Whitehouse ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL); 734b09e593dSSteven Whitehouse INIT_LIST_HEAD(&ai->ai_ail1_list); 735b09e593dSSteven Whitehouse INIT_LIST_HEAD(&ai->ai_ail2_list); 736b3b94faaSDavid Teigland 73716615be1SSteven Whitehouse if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) { 73816615be1SSteven Whitehouse printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf, 73916615be1SSteven Whitehouse sdp->sd_log_commited_buf); 74016615be1SSteven Whitehouse gfs2_assert_withdraw(sdp, 0); 74116615be1SSteven Whitehouse } 74216615be1SSteven Whitehouse if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) { 74316615be1SSteven Whitehouse printk(KERN_INFO "GFS2: log databuf %u %u\n", 74416615be1SSteven Whitehouse sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf); 74516615be1SSteven Whitehouse gfs2_assert_withdraw(sdp, 0); 74616615be1SSteven Whitehouse } 747b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, 748b3b94faaSDavid Teigland sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke); 749b3b94faaSDavid Teigland 750b3b94faaSDavid Teigland sdp->sd_log_flush_head = sdp->sd_log_head; 751b3b94faaSDavid Teigland sdp->sd_log_flush_wrapped = 0; 752b3b94faaSDavid Teigland ai->ai_first = sdp->sd_log_flush_head; 753b3b94faaSDavid Teigland 754d7b616e2SSteven Whitehouse gfs2_ordered_write(sdp); 755b3b94faaSDavid Teigland lops_before_commit(sdp); 756d7b616e2SSteven Whitehouse gfs2_ordered_wait(sdp); 757d7b616e2SSteven Whitehouse 75816615be1SSteven Whitehouse if (sdp->sd_log_head != sdp->sd_log_flush_head) 759b3b94faaSDavid Teigland log_flush_commit(sdp); 7602332c443SRobert Peterson else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){ 7612332c443SRobert Peterson gfs2_log_lock(sdp); 762fd041f0bSSteven Whitehouse atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */ 76363997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, -1); 7642332c443SRobert Peterson gfs2_log_unlock(sdp); 765b3b94faaSDavid Teigland log_write_header(sdp, 0, PULL); 7662332c443SRobert Peterson } 767b3b94faaSDavid Teigland lops_after_commit(sdp, ai); 768fe1a698fSSteven Whitehouse 769fe1a698fSSteven Whitehouse gfs2_log_lock(sdp); 770b3b94faaSDavid Teigland sdp->sd_log_head = sdp->sd_log_flush_head; 771faa31ce8SSteven Whitehouse sdp->sd_log_blks_reserved = 0; 772faa31ce8SSteven Whitehouse sdp->sd_log_commited_buf = 0; 7732332c443SRobert Peterson sdp->sd_log_commited_databuf = 0; 774b3b94faaSDavid Teigland sdp->sd_log_commited_revoke = 0; 775b3b94faaSDavid Teigland 776d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 777b3b94faaSDavid Teigland if (!list_empty(&ai->ai_ail1_list)) { 778b3b94faaSDavid Teigland list_add(&ai->ai_list, &sdp->sd_ail1_list); 779b3b94faaSDavid Teigland ai = NULL; 780b3b94faaSDavid Teigland } 781d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 782b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 78363997775SSteven Whitehouse trace_gfs2_log_flush(sdp, 0); 784484adff8SSteven Whitehouse up_write(&sdp->sd_log_flush_lock); 785b3b94faaSDavid Teigland 786b3b94faaSDavid Teigland kfree(ai); 787b3b94faaSDavid Teigland } 788b3b94faaSDavid Teigland 789b3b94faaSDavid Teigland static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 790b3b94faaSDavid Teigland { 7912332c443SRobert Peterson unsigned int reserved; 792ac39aaddSSteven Whitehouse unsigned int unused; 793b3b94faaSDavid Teigland 794b3b94faaSDavid Teigland gfs2_log_lock(sdp); 795b3b94faaSDavid Teigland 796b3b94faaSDavid Teigland sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm; 7972332c443SRobert Peterson sdp->sd_log_commited_databuf += tr->tr_num_databuf_new - 7982332c443SRobert Peterson tr->tr_num_databuf_rm; 7992332c443SRobert Peterson gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) || 8002332c443SRobert Peterson (((int)sdp->sd_log_commited_databuf) >= 0)); 801b3b94faaSDavid Teigland sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm; 8022332c443SRobert Peterson reserved = calc_reserved(sdp); 80362be1f71SRoel Kluin gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved); 804ac39aaddSSteven Whitehouse unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved; 805ac39aaddSSteven Whitehouse atomic_add(unused, &sdp->sd_log_blks_free); 80663997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, unused); 807fd041f0bSSteven Whitehouse gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= 8082332c443SRobert Peterson sdp->sd_jdesc->jd_blocks); 809b3b94faaSDavid Teigland sdp->sd_log_blks_reserved = reserved; 810b3b94faaSDavid Teigland 811b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 812b3b94faaSDavid Teigland } 813b3b94faaSDavid Teigland 814d0109bfaSBob Peterson static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 815d0109bfaSBob Peterson { 816d0109bfaSBob Peterson struct list_head *head = &tr->tr_list_buf; 817d0109bfaSBob Peterson struct gfs2_bufdata *bd; 818d0109bfaSBob Peterson 819d0109bfaSBob Peterson gfs2_log_lock(sdp); 820d0109bfaSBob Peterson while (!list_empty(head)) { 821d0109bfaSBob Peterson bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr); 822d0109bfaSBob Peterson list_del_init(&bd->bd_list_tr); 823d0109bfaSBob Peterson tr->tr_num_buf--; 824d0109bfaSBob Peterson } 825d0109bfaSBob Peterson gfs2_log_unlock(sdp); 826d0109bfaSBob Peterson gfs2_assert_warn(sdp, !tr->tr_num_buf); 827d0109bfaSBob Peterson } 828d0109bfaSBob Peterson 829b3b94faaSDavid Teigland /** 830b3b94faaSDavid Teigland * gfs2_log_commit - Commit a transaction to the log 831b3b94faaSDavid Teigland * @sdp: the filesystem 832b3b94faaSDavid Teigland * @tr: the transaction 833b3b94faaSDavid Teigland * 8345e687eacSBenjamin Marzinski * We wake up gfs2_logd if the number of pinned blocks exceed thresh1 8355e687eacSBenjamin Marzinski * or the total number of used blocks (pinned blocks plus AIL blocks) 8365e687eacSBenjamin Marzinski * is greater than thresh2. 8375e687eacSBenjamin Marzinski * 8385e687eacSBenjamin Marzinski * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of 8395e687eacSBenjamin Marzinski * journal size. 8405e687eacSBenjamin Marzinski * 841b3b94faaSDavid Teigland * Returns: errno 842b3b94faaSDavid Teigland */ 843b3b94faaSDavid Teigland 844b3b94faaSDavid Teigland void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 845b3b94faaSDavid Teigland { 846b3b94faaSDavid Teigland log_refund(sdp, tr); 847d0109bfaSBob Peterson buf_lo_incore_commit(sdp, tr); 848b3b94faaSDavid Teigland 849484adff8SSteven Whitehouse up_read(&sdp->sd_log_flush_lock); 850b3b94faaSDavid Teigland 8515e687eacSBenjamin Marzinski if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) || 8525e687eacSBenjamin Marzinski ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) > 8535e687eacSBenjamin Marzinski atomic_read(&sdp->sd_log_thresh2))) 8545e687eacSBenjamin Marzinski wake_up(&sdp->sd_logd_waitq); 855faa31ce8SSteven Whitehouse } 856b3b94faaSDavid Teigland 857b3b94faaSDavid Teigland /** 858b3b94faaSDavid Teigland * gfs2_log_shutdown - write a shutdown header into a journal 859b3b94faaSDavid Teigland * @sdp: the filesystem 860b3b94faaSDavid Teigland * 861b3b94faaSDavid Teigland */ 862b3b94faaSDavid Teigland 863b3b94faaSDavid Teigland void gfs2_log_shutdown(struct gfs2_sbd *sdp) 864b3b94faaSDavid Teigland { 865484adff8SSteven Whitehouse down_write(&sdp->sd_log_flush_lock); 866b3b94faaSDavid Teigland 867b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved); 868b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf); 869b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); 870b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg); 871b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf); 872b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list)); 873b3b94faaSDavid Teigland 874b3b94faaSDavid Teigland sdp->sd_log_flush_head = sdp->sd_log_head; 875b3b94faaSDavid Teigland sdp->sd_log_flush_wrapped = 0; 876b3b94faaSDavid Teigland 8772332c443SRobert Peterson log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 8782332c443SRobert Peterson (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL); 879b3b94faaSDavid Teigland 880fd041f0bSSteven Whitehouse gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks); 881a74604beSSteven Whitehouse gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail); 882a74604beSSteven Whitehouse gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list)); 883b3b94faaSDavid Teigland 884b3b94faaSDavid Teigland sdp->sd_log_head = sdp->sd_log_flush_head; 885b3b94faaSDavid Teigland sdp->sd_log_tail = sdp->sd_log_head; 886b3b94faaSDavid Teigland 887484adff8SSteven Whitehouse up_write(&sdp->sd_log_flush_lock); 888b3b94faaSDavid Teigland } 889b3b94faaSDavid Teigland 890a25311c8SSteven Whitehouse 891a25311c8SSteven Whitehouse /** 892a25311c8SSteven Whitehouse * gfs2_meta_syncfs - sync all the buffers in a filesystem 893a25311c8SSteven Whitehouse * @sdp: the filesystem 894a25311c8SSteven Whitehouse * 895a25311c8SSteven Whitehouse */ 896a25311c8SSteven Whitehouse 897a25311c8SSteven Whitehouse void gfs2_meta_syncfs(struct gfs2_sbd *sdp) 898a25311c8SSteven Whitehouse { 899a25311c8SSteven Whitehouse gfs2_log_flush(sdp, NULL); 900a25311c8SSteven Whitehouse for (;;) { 9015e687eacSBenjamin Marzinski gfs2_ail1_start(sdp); 90226b06a69SSteven Whitehouse gfs2_ail1_wait(sdp); 9034667a0ecSSteven Whitehouse if (gfs2_ail1_empty(sdp)) 904a25311c8SSteven Whitehouse break; 905a25311c8SSteven Whitehouse } 906380f7c65SSteven Whitehouse gfs2_log_flush(sdp, NULL); 907a25311c8SSteven Whitehouse } 908a25311c8SSteven Whitehouse 9095e687eacSBenjamin Marzinski static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp) 9105e687eacSBenjamin Marzinski { 9115e687eacSBenjamin Marzinski return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1)); 9125e687eacSBenjamin Marzinski } 9135e687eacSBenjamin Marzinski 9145e687eacSBenjamin Marzinski static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp) 9155e687eacSBenjamin Marzinski { 9165e687eacSBenjamin Marzinski unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free); 9175e687eacSBenjamin Marzinski return used_blocks >= atomic_read(&sdp->sd_log_thresh2); 9185e687eacSBenjamin Marzinski } 919ec69b188SSteven Whitehouse 920ec69b188SSteven Whitehouse /** 921ec69b188SSteven Whitehouse * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks 922ec69b188SSteven Whitehouse * @sdp: Pointer to GFS2 superblock 923ec69b188SSteven Whitehouse * 924ec69b188SSteven Whitehouse * Also, periodically check to make sure that we're using the most recent 925ec69b188SSteven Whitehouse * journal index. 926ec69b188SSteven Whitehouse */ 927ec69b188SSteven Whitehouse 928ec69b188SSteven Whitehouse int gfs2_logd(void *data) 929ec69b188SSteven Whitehouse { 930ec69b188SSteven Whitehouse struct gfs2_sbd *sdp = data; 9315e687eacSBenjamin Marzinski unsigned long t = 1; 9325e687eacSBenjamin Marzinski DEFINE_WAIT(wait); 9335e687eacSBenjamin Marzinski unsigned preflush; 934ec69b188SSteven Whitehouse 935ec69b188SSteven Whitehouse while (!kthread_should_stop()) { 936ec69b188SSteven Whitehouse 9375e687eacSBenjamin Marzinski preflush = atomic_read(&sdp->sd_log_pinned); 9385e687eacSBenjamin Marzinski if (gfs2_jrnl_flush_reqd(sdp) || t == 0) { 9394667a0ecSSteven Whitehouse gfs2_ail1_empty(sdp); 940ec69b188SSteven Whitehouse gfs2_log_flush(sdp, NULL); 941ec69b188SSteven Whitehouse } 942ec69b188SSteven Whitehouse 9435e687eacSBenjamin Marzinski if (gfs2_ail_flush_reqd(sdp)) { 9445e687eacSBenjamin Marzinski gfs2_ail1_start(sdp); 94526b06a69SSteven Whitehouse gfs2_ail1_wait(sdp); 9464667a0ecSSteven Whitehouse gfs2_ail1_empty(sdp); 9475e687eacSBenjamin Marzinski gfs2_log_flush(sdp, NULL); 9485e687eacSBenjamin Marzinski } 9495e687eacSBenjamin Marzinski 95026b06a69SSteven Whitehouse if (!gfs2_ail_flush_reqd(sdp)) 9515e687eacSBenjamin Marzinski wake_up(&sdp->sd_log_waitq); 95226b06a69SSteven Whitehouse 953ec69b188SSteven Whitehouse t = gfs2_tune_get(sdp, gt_logd_secs) * HZ; 954*a0acae0eSTejun Heo 955*a0acae0eSTejun Heo try_to_freeze(); 9565e687eacSBenjamin Marzinski 9575e687eacSBenjamin Marzinski do { 9585e687eacSBenjamin Marzinski prepare_to_wait(&sdp->sd_logd_waitq, &wait, 9595f487490SSteven Whitehouse TASK_INTERRUPTIBLE); 9605e687eacSBenjamin Marzinski if (!gfs2_ail_flush_reqd(sdp) && 9615e687eacSBenjamin Marzinski !gfs2_jrnl_flush_reqd(sdp) && 9625e687eacSBenjamin Marzinski !kthread_should_stop()) 9635e687eacSBenjamin Marzinski t = schedule_timeout(t); 9645e687eacSBenjamin Marzinski } while(t && !gfs2_ail_flush_reqd(sdp) && 9655e687eacSBenjamin Marzinski !gfs2_jrnl_flush_reqd(sdp) && 9665e687eacSBenjamin Marzinski !kthread_should_stop()); 9675e687eacSBenjamin Marzinski finish_wait(&sdp->sd_logd_waitq, &wait); 968ec69b188SSteven Whitehouse } 969ec69b188SSteven Whitehouse 970ec69b188SSteven Whitehouse return 0; 971ec69b188SSteven Whitehouse } 972ec69b188SSteven Whitehouse 973