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> 21b3b94faaSDavid Teigland 22b3b94faaSDavid Teigland #include "gfs2.h" 235c676f6dSSteven Whitehouse #include "incore.h" 24b3b94faaSDavid Teigland #include "bmap.h" 25b3b94faaSDavid Teigland #include "glock.h" 26b3b94faaSDavid Teigland #include "log.h" 27b3b94faaSDavid Teigland #include "lops.h" 28b3b94faaSDavid Teigland #include "meta_io.h" 295c676f6dSSteven Whitehouse #include "util.h" 3071b86f56SSteven Whitehouse #include "dir.h" 3163997775SSteven Whitehouse #include "trace_gfs2.h" 32b3b94faaSDavid Teigland 33b3b94faaSDavid Teigland #define PULL 1 34b3b94faaSDavid Teigland 35b3b94faaSDavid Teigland /** 36b3b94faaSDavid Teigland * gfs2_struct2blk - compute stuff 37b3b94faaSDavid Teigland * @sdp: the filesystem 38b3b94faaSDavid Teigland * @nstruct: the number of structures 39b3b94faaSDavid Teigland * @ssize: the size of the structures 40b3b94faaSDavid Teigland * 41b3b94faaSDavid Teigland * Compute the number of log descriptor blocks needed to hold a certain number 42b3b94faaSDavid Teigland * of structures of a certain size. 43b3b94faaSDavid Teigland * 44b3b94faaSDavid Teigland * Returns: the number of blocks needed (minimum is always 1) 45b3b94faaSDavid Teigland */ 46b3b94faaSDavid Teigland 47b3b94faaSDavid Teigland unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct, 48b3b94faaSDavid Teigland unsigned int ssize) 49b3b94faaSDavid Teigland { 50b3b94faaSDavid Teigland unsigned int blks; 51b3b94faaSDavid Teigland unsigned int first, second; 52b3b94faaSDavid Teigland 53b3b94faaSDavid Teigland blks = 1; 54faa31ce8SSteven Whitehouse first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize; 55b3b94faaSDavid Teigland 56b3b94faaSDavid Teigland if (nstruct > first) { 57568f4c96SSteven Whitehouse second = (sdp->sd_sb.sb_bsize - 58568f4c96SSteven Whitehouse sizeof(struct gfs2_meta_header)) / ssize; 595c676f6dSSteven Whitehouse blks += DIV_ROUND_UP(nstruct - first, second); 60b3b94faaSDavid Teigland } 61b3b94faaSDavid Teigland 62b3b94faaSDavid Teigland return blks; 63b3b94faaSDavid Teigland } 64b3b94faaSDavid Teigland 65ddacfaf7SSteven Whitehouse /** 661e1a3d03SSteven Whitehouse * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters 671e1a3d03SSteven Whitehouse * @mapping: The associated mapping (maybe NULL) 681e1a3d03SSteven Whitehouse * @bd: The gfs2_bufdata to remove 691e1a3d03SSteven Whitehouse * 70c618e87aSSteven Whitehouse * The ail lock _must_ be held when calling this function 711e1a3d03SSteven Whitehouse * 721e1a3d03SSteven Whitehouse */ 731e1a3d03SSteven Whitehouse 74f91a0d3eSSteven Whitehouse void gfs2_remove_from_ail(struct gfs2_bufdata *bd) 751e1a3d03SSteven Whitehouse { 761e1a3d03SSteven Whitehouse bd->bd_ail = NULL; 771ad38c43SSteven Whitehouse list_del_init(&bd->bd_ail_st_list); 781ad38c43SSteven Whitehouse list_del_init(&bd->bd_ail_gl_list); 791e1a3d03SSteven Whitehouse atomic_dec(&bd->bd_gl->gl_ail_count); 801e1a3d03SSteven Whitehouse brelse(bd->bd_bh); 811e1a3d03SSteven Whitehouse } 821e1a3d03SSteven Whitehouse 831e1a3d03SSteven Whitehouse /** 84ddacfaf7SSteven Whitehouse * gfs2_ail1_start_one - Start I/O on a part of the AIL 85ddacfaf7SSteven Whitehouse * @sdp: the filesystem 86ddacfaf7SSteven Whitehouse * @tr: the part of the AIL 87ddacfaf7SSteven Whitehouse * 88ddacfaf7SSteven Whitehouse */ 89ddacfaf7SSteven Whitehouse 90ddacfaf7SSteven Whitehouse static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) 91d6a079e8SDave Chinner __releases(&sdp->sd_ail_lock) 92d6a079e8SDave Chinner __acquires(&sdp->sd_ail_lock) 93ddacfaf7SSteven Whitehouse { 94*5ac048bbSSteven Whitehouse struct gfs2_glock *gl = NULL; 95ddacfaf7SSteven Whitehouse struct gfs2_bufdata *bd, *s; 96ddacfaf7SSteven Whitehouse struct buffer_head *bh; 97ddacfaf7SSteven Whitehouse int retry; 98ddacfaf7SSteven Whitehouse 99ddacfaf7SSteven Whitehouse do { 100ddacfaf7SSteven Whitehouse retry = 0; 101ddacfaf7SSteven Whitehouse 102ddacfaf7SSteven Whitehouse list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, 103ddacfaf7SSteven Whitehouse 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; 117*5ac048bbSSteven Whitehouse if (gl == bd->bd_gl) 118*5ac048bbSSteven Whitehouse continue; 119*5ac048bbSSteven Whitehouse gl = bd->bd_gl; 120ddacfaf7SSteven Whitehouse list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list); 121ddacfaf7SSteven Whitehouse 122d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 123*5ac048bbSSteven Whitehouse filemap_fdatawrite(gfs2_glock2aspace(gl)); 124d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 125ddacfaf7SSteven Whitehouse 126ddacfaf7SSteven Whitehouse retry = 1; 127ddacfaf7SSteven Whitehouse break; 128ddacfaf7SSteven Whitehouse } 129ddacfaf7SSteven Whitehouse } while (retry); 130ddacfaf7SSteven Whitehouse } 131ddacfaf7SSteven Whitehouse 132ddacfaf7SSteven Whitehouse /** 133ddacfaf7SSteven Whitehouse * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced 134ddacfaf7SSteven Whitehouse * @sdp: the filesystem 135ddacfaf7SSteven Whitehouse * @ai: the AIL entry 136ddacfaf7SSteven Whitehouse * 137ddacfaf7SSteven Whitehouse */ 138ddacfaf7SSteven Whitehouse 139ddacfaf7SSteven Whitehouse static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags) 140ddacfaf7SSteven Whitehouse { 141ddacfaf7SSteven Whitehouse struct gfs2_bufdata *bd, *s; 142ddacfaf7SSteven Whitehouse struct buffer_head *bh; 143ddacfaf7SSteven Whitehouse 144ddacfaf7SSteven Whitehouse list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, 145ddacfaf7SSteven Whitehouse bd_ail_st_list) { 146ddacfaf7SSteven Whitehouse bh = bd->bd_bh; 147ddacfaf7SSteven Whitehouse 148ddacfaf7SSteven Whitehouse gfs2_assert(sdp, bd->bd_ail == ai); 149ddacfaf7SSteven Whitehouse 150ddacfaf7SSteven Whitehouse if (buffer_busy(bh)) { 151ddacfaf7SSteven Whitehouse if (flags & DIO_ALL) 152ddacfaf7SSteven Whitehouse continue; 153ddacfaf7SSteven Whitehouse else 154ddacfaf7SSteven Whitehouse break; 155ddacfaf7SSteven Whitehouse } 156ddacfaf7SSteven Whitehouse 157ddacfaf7SSteven Whitehouse if (!buffer_uptodate(bh)) 158ddacfaf7SSteven Whitehouse gfs2_io_error_bh(sdp, bh); 159ddacfaf7SSteven Whitehouse 160ddacfaf7SSteven Whitehouse list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list); 161ddacfaf7SSteven Whitehouse } 162ddacfaf7SSteven Whitehouse 163ddacfaf7SSteven Whitehouse return list_empty(&ai->ai_ail1_list); 164ddacfaf7SSteven Whitehouse } 165ddacfaf7SSteven Whitehouse 1665e687eacSBenjamin Marzinski static void gfs2_ail1_start(struct gfs2_sbd *sdp) 167b3b94faaSDavid Teigland { 168693ddeabSBob Peterson struct list_head *head; 169cd915493SSteven Whitehouse u64 sync_gen; 1705e687eacSBenjamin Marzinski struct gfs2_ail *ai; 17174669416SSteven Whitehouse int done = 0; 172b3b94faaSDavid Teigland 173d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 174693ddeabSBob Peterson head = &sdp->sd_ail1_list; 175b3b94faaSDavid Teigland if (list_empty(head)) { 176d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 177b3b94faaSDavid Teigland return; 178b3b94faaSDavid Teigland } 179b3b94faaSDavid Teigland sync_gen = sdp->sd_ail_sync_gen++; 180b3b94faaSDavid Teigland 18174669416SSteven Whitehouse while(!done) { 18274669416SSteven Whitehouse done = 1; 1835e687eacSBenjamin Marzinski list_for_each_entry_reverse(ai, head, ai_list) { 184b3b94faaSDavid Teigland if (ai->ai_sync_gen >= sync_gen) 185b3b94faaSDavid Teigland continue; 186b3b94faaSDavid Teigland ai->ai_sync_gen = sync_gen; 187d6a079e8SDave Chinner gfs2_ail1_start_one(sdp, ai); /* This may drop ail lock */ 18874669416SSteven Whitehouse done = 0; 189b3b94faaSDavid Teigland break; 190b3b94faaSDavid Teigland } 191b3b94faaSDavid Teigland } 192b3b94faaSDavid Teigland 193d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 194b3b94faaSDavid Teigland } 195b3b94faaSDavid Teigland 196ec69b188SSteven Whitehouse static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags) 197b3b94faaSDavid Teigland { 198b3b94faaSDavid Teigland struct gfs2_ail *ai, *s; 199b3b94faaSDavid Teigland int ret; 200b3b94faaSDavid Teigland 201d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 202b3b94faaSDavid Teigland 203b3b94faaSDavid Teigland list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) { 204b3b94faaSDavid Teigland if (gfs2_ail1_empty_one(sdp, ai, flags)) 205b3b94faaSDavid Teigland list_move(&ai->ai_list, &sdp->sd_ail2_list); 206b3b94faaSDavid Teigland else if (!(flags & DIO_ALL)) 207b3b94faaSDavid Teigland break; 208b3b94faaSDavid Teigland } 209b3b94faaSDavid Teigland 210b3b94faaSDavid Teigland ret = list_empty(&sdp->sd_ail1_list); 211b3b94faaSDavid Teigland 212d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 213b3b94faaSDavid Teigland 214b3b94faaSDavid Teigland return ret; 215b3b94faaSDavid Teigland } 216b3b94faaSDavid Teigland 217ddacfaf7SSteven Whitehouse 218ddacfaf7SSteven Whitehouse /** 219ddacfaf7SSteven Whitehouse * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced 220ddacfaf7SSteven Whitehouse * @sdp: the filesystem 221ddacfaf7SSteven Whitehouse * @ai: the AIL entry 222ddacfaf7SSteven Whitehouse * 223ddacfaf7SSteven Whitehouse */ 224ddacfaf7SSteven Whitehouse 225ddacfaf7SSteven Whitehouse static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) 226ddacfaf7SSteven Whitehouse { 227ddacfaf7SSteven Whitehouse struct list_head *head = &ai->ai_ail2_list; 228ddacfaf7SSteven Whitehouse struct gfs2_bufdata *bd; 229ddacfaf7SSteven Whitehouse 230ddacfaf7SSteven Whitehouse while (!list_empty(head)) { 231ddacfaf7SSteven Whitehouse bd = list_entry(head->prev, struct gfs2_bufdata, 232ddacfaf7SSteven Whitehouse bd_ail_st_list); 233ddacfaf7SSteven Whitehouse gfs2_assert(sdp, bd->bd_ail == ai); 234f91a0d3eSSteven Whitehouse gfs2_remove_from_ail(bd); 235ddacfaf7SSteven Whitehouse } 236ddacfaf7SSteven Whitehouse } 237ddacfaf7SSteven Whitehouse 238b3b94faaSDavid Teigland static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail) 239b3b94faaSDavid Teigland { 240b3b94faaSDavid Teigland struct gfs2_ail *ai, *safe; 241b3b94faaSDavid Teigland unsigned int old_tail = sdp->sd_log_tail; 242b3b94faaSDavid Teigland int wrap = (new_tail < old_tail); 243b3b94faaSDavid Teigland int a, b, rm; 244b3b94faaSDavid Teigland 245d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 246b3b94faaSDavid Teigland 247b3b94faaSDavid Teigland list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) { 248b3b94faaSDavid Teigland a = (old_tail <= ai->ai_first); 249b3b94faaSDavid Teigland b = (ai->ai_first < new_tail); 250b3b94faaSDavid Teigland rm = (wrap) ? (a || b) : (a && b); 251b3b94faaSDavid Teigland if (!rm) 252b3b94faaSDavid Teigland continue; 253b3b94faaSDavid Teigland 254b3b94faaSDavid Teigland gfs2_ail2_empty_one(sdp, ai); 255b3b94faaSDavid Teigland list_del(&ai->ai_list); 256b3b94faaSDavid Teigland gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list)); 257b3b94faaSDavid Teigland gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list)); 258b3b94faaSDavid Teigland kfree(ai); 259b3b94faaSDavid Teigland } 260b3b94faaSDavid Teigland 261d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 262b3b94faaSDavid Teigland } 263b3b94faaSDavid Teigland 264b3b94faaSDavid Teigland /** 265b3b94faaSDavid Teigland * gfs2_log_reserve - Make a log reservation 266b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 267b3b94faaSDavid Teigland * @blks: The number of blocks to reserve 268b3b94faaSDavid Teigland * 26989918647SSteven Whitehouse * Note that we never give out the last few blocks of the journal. Thats 2702332c443SRobert Peterson * due to the fact that there is a small number of header blocks 271b004157aSSteven Whitehouse * associated with each log flush. The exact number can't be known until 272b004157aSSteven Whitehouse * flush time, so we ensure that we have just enough free blocks at all 273b004157aSSteven Whitehouse * times to avoid running out during a log flush. 274b004157aSSteven Whitehouse * 2755e687eacSBenjamin Marzinski * We no longer flush the log here, instead we wake up logd to do that 2765e687eacSBenjamin Marzinski * for us. To avoid the thundering herd and to ensure that we deal fairly 2775e687eacSBenjamin Marzinski * with queued waiters, we use an exclusive wait. This means that when we 2785e687eacSBenjamin Marzinski * get woken with enough journal space to get our reservation, we need to 2795e687eacSBenjamin Marzinski * wake the next waiter on the list. 2805e687eacSBenjamin Marzinski * 281b3b94faaSDavid Teigland * Returns: errno 282b3b94faaSDavid Teigland */ 283b3b94faaSDavid Teigland 284b3b94faaSDavid Teigland int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks) 285b3b94faaSDavid Teigland { 28689918647SSteven Whitehouse unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize); 2875e687eacSBenjamin Marzinski unsigned wanted = blks + reserved_blks; 2885e687eacSBenjamin Marzinski DEFINE_WAIT(wait); 2895e687eacSBenjamin Marzinski int did_wait = 0; 2905e687eacSBenjamin Marzinski unsigned int free_blocks; 291b3b94faaSDavid Teigland 292b3b94faaSDavid Teigland if (gfs2_assert_warn(sdp, blks) || 293b3b94faaSDavid Teigland gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks)) 294b3b94faaSDavid Teigland return -EINVAL; 2955e687eacSBenjamin Marzinski retry: 2965e687eacSBenjamin Marzinski free_blocks = atomic_read(&sdp->sd_log_blks_free); 2975e687eacSBenjamin Marzinski if (unlikely(free_blocks <= wanted)) { 2985e687eacSBenjamin Marzinski do { 2995e687eacSBenjamin Marzinski prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait, 3005e687eacSBenjamin Marzinski TASK_UNINTERRUPTIBLE); 3015e687eacSBenjamin Marzinski wake_up(&sdp->sd_logd_waitq); 3025e687eacSBenjamin Marzinski did_wait = 1; 3035e687eacSBenjamin Marzinski if (atomic_read(&sdp->sd_log_blks_free) <= wanted) 3045e687eacSBenjamin Marzinski io_schedule(); 3055e687eacSBenjamin Marzinski free_blocks = atomic_read(&sdp->sd_log_blks_free); 3065e687eacSBenjamin Marzinski } while(free_blocks <= wanted); 3075e687eacSBenjamin Marzinski finish_wait(&sdp->sd_log_waitq, &wait); 308b3b94faaSDavid Teigland } 3095e687eacSBenjamin Marzinski if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks, 3105e687eacSBenjamin Marzinski free_blocks - blks) != free_blocks) 3115e687eacSBenjamin Marzinski goto retry; 31263997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, -blks); 3135e687eacSBenjamin Marzinski 3145e687eacSBenjamin Marzinski /* 3155e687eacSBenjamin Marzinski * If we waited, then so might others, wake them up _after_ we get 3165e687eacSBenjamin Marzinski * our share of the log. 3175e687eacSBenjamin Marzinski */ 3185e687eacSBenjamin Marzinski if (unlikely(did_wait)) 3195e687eacSBenjamin Marzinski wake_up(&sdp->sd_log_waitq); 320484adff8SSteven Whitehouse 321484adff8SSteven Whitehouse down_read(&sdp->sd_log_flush_lock); 322b3b94faaSDavid Teigland 323b3b94faaSDavid Teigland return 0; 324b3b94faaSDavid Teigland } 325b3b94faaSDavid Teigland 326cd915493SSteven Whitehouse static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn) 327b3b94faaSDavid Teigland { 328da6dd40dSBob Peterson struct gfs2_journal_extent *je; 329b3b94faaSDavid Teigland 330da6dd40dSBob Peterson list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) { 331da6dd40dSBob Peterson if (lbn >= je->lblock && lbn < je->lblock + je->blocks) 332ff91cc9bSSteven Whitehouse return je->dblock + lbn - je->lblock; 333da6dd40dSBob Peterson } 334b3b94faaSDavid Teigland 335da6dd40dSBob Peterson return -1; 336b3b94faaSDavid Teigland } 337b3b94faaSDavid Teigland 338b3b94faaSDavid Teigland /** 339b3b94faaSDavid Teigland * log_distance - Compute distance between two journal blocks 340b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 341b3b94faaSDavid Teigland * @newer: The most recent journal block of the pair 342b3b94faaSDavid Teigland * @older: The older journal block of the pair 343b3b94faaSDavid Teigland * 344b3b94faaSDavid Teigland * Compute the distance (in the journal direction) between two 345b3b94faaSDavid Teigland * blocks in the journal 346b3b94faaSDavid Teigland * 347b3b94faaSDavid Teigland * Returns: the distance in blocks 348b3b94faaSDavid Teigland */ 349b3b94faaSDavid Teigland 350faa31ce8SSteven Whitehouse static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer, 351b3b94faaSDavid Teigland unsigned int older) 352b3b94faaSDavid Teigland { 353b3b94faaSDavid Teigland int dist; 354b3b94faaSDavid Teigland 355b3b94faaSDavid Teigland dist = newer - older; 356b3b94faaSDavid Teigland if (dist < 0) 357b3b94faaSDavid Teigland dist += sdp->sd_jdesc->jd_blocks; 358b3b94faaSDavid Teigland 359b3b94faaSDavid Teigland return dist; 360b3b94faaSDavid Teigland } 361b3b94faaSDavid Teigland 3622332c443SRobert Peterson /** 3632332c443SRobert Peterson * calc_reserved - Calculate the number of blocks to reserve when 3642332c443SRobert Peterson * refunding a transaction's unused buffers. 3652332c443SRobert Peterson * @sdp: The GFS2 superblock 3662332c443SRobert Peterson * 3672332c443SRobert Peterson * This is complex. We need to reserve room for all our currently used 3682332c443SRobert Peterson * metadata buffers (e.g. normal file I/O rewriting file time stamps) and 3692332c443SRobert Peterson * all our journaled data buffers for journaled files (e.g. files in the 3702332c443SRobert Peterson * meta_fs like rindex, or files for which chattr +j was done.) 3712332c443SRobert Peterson * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush 3722332c443SRobert Peterson * will count it as free space (sd_log_blks_free) and corruption will follow. 3732332c443SRobert Peterson * 3742332c443SRobert Peterson * We can have metadata bufs and jdata bufs in the same journal. So each 3752332c443SRobert Peterson * type gets its own log header, for which we need to reserve a block. 3762332c443SRobert Peterson * In fact, each type has the potential for needing more than one header 3772332c443SRobert Peterson * in cases where we have more buffers than will fit on a journal page. 3782332c443SRobert Peterson * Metadata journal entries take up half the space of journaled buffer entries. 3792332c443SRobert Peterson * Thus, metadata entries have buf_limit (502) and journaled buffers have 3802332c443SRobert Peterson * databuf_limit (251) before they cause a wrap around. 3812332c443SRobert Peterson * 3822332c443SRobert Peterson * Also, we need to reserve blocks for revoke journal entries and one for an 3832332c443SRobert Peterson * overall header for the lot. 3842332c443SRobert Peterson * 3852332c443SRobert Peterson * Returns: the number of blocks reserved 3862332c443SRobert Peterson */ 3872332c443SRobert Peterson static unsigned int calc_reserved(struct gfs2_sbd *sdp) 3882332c443SRobert Peterson { 3892332c443SRobert Peterson unsigned int reserved = 0; 3902332c443SRobert Peterson unsigned int mbuf_limit, metabufhdrs_needed; 3912332c443SRobert Peterson unsigned int dbuf_limit, databufhdrs_needed; 3922332c443SRobert Peterson unsigned int revokes = 0; 3932332c443SRobert Peterson 3942332c443SRobert Peterson mbuf_limit = buf_limit(sdp); 3952332c443SRobert Peterson metabufhdrs_needed = (sdp->sd_log_commited_buf + 3962332c443SRobert Peterson (mbuf_limit - 1)) / mbuf_limit; 3972332c443SRobert Peterson dbuf_limit = databuf_limit(sdp); 3982332c443SRobert Peterson databufhdrs_needed = (sdp->sd_log_commited_databuf + 3992332c443SRobert Peterson (dbuf_limit - 1)) / dbuf_limit; 4002332c443SRobert Peterson 4012e95e3f6SBenjamin Marzinski if (sdp->sd_log_commited_revoke > 0) 4022332c443SRobert Peterson revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke, 4032332c443SRobert Peterson sizeof(u64)); 4042332c443SRobert Peterson 4052332c443SRobert Peterson reserved = sdp->sd_log_commited_buf + metabufhdrs_needed + 4062332c443SRobert Peterson sdp->sd_log_commited_databuf + databufhdrs_needed + 4072332c443SRobert Peterson revokes; 4082332c443SRobert Peterson /* One for the overall header */ 4092332c443SRobert Peterson if (reserved) 4102332c443SRobert Peterson reserved++; 4112332c443SRobert Peterson return reserved; 4122332c443SRobert Peterson } 4132332c443SRobert Peterson 414b3b94faaSDavid Teigland static unsigned int current_tail(struct gfs2_sbd *sdp) 415b3b94faaSDavid Teigland { 416b3b94faaSDavid Teigland struct gfs2_ail *ai; 417b3b94faaSDavid Teigland unsigned int tail; 418b3b94faaSDavid Teigland 419d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 420b3b94faaSDavid Teigland 421faa31ce8SSteven Whitehouse if (list_empty(&sdp->sd_ail1_list)) { 422b3b94faaSDavid Teigland tail = sdp->sd_log_head; 423faa31ce8SSteven Whitehouse } else { 424faa31ce8SSteven Whitehouse ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list); 425b3b94faaSDavid Teigland tail = ai->ai_first; 426b3b94faaSDavid Teigland } 427b3b94faaSDavid Teigland 428d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 429b3b94faaSDavid Teigland 430b3b94faaSDavid Teigland return tail; 431b3b94faaSDavid Teigland } 432b3b94faaSDavid Teigland 43316615be1SSteven Whitehouse void gfs2_log_incr_head(struct gfs2_sbd *sdp) 434b3b94faaSDavid Teigland { 435b3b94faaSDavid Teigland if (sdp->sd_log_flush_head == sdp->sd_log_tail) 43616615be1SSteven Whitehouse BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head); 437b3b94faaSDavid Teigland 438b3b94faaSDavid Teigland if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) { 439b3b94faaSDavid Teigland sdp->sd_log_flush_head = 0; 440b3b94faaSDavid Teigland sdp->sd_log_flush_wrapped = 1; 441b3b94faaSDavid Teigland } 442b3b94faaSDavid Teigland } 443b3b94faaSDavid Teigland 444b3b94faaSDavid Teigland /** 44516615be1SSteven Whitehouse * gfs2_log_write_endio - End of I/O for a log buffer 44616615be1SSteven Whitehouse * @bh: The buffer head 44716615be1SSteven Whitehouse * @uptodate: I/O Status 44816615be1SSteven Whitehouse * 44916615be1SSteven Whitehouse */ 45016615be1SSteven Whitehouse 45116615be1SSteven Whitehouse static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate) 45216615be1SSteven Whitehouse { 45316615be1SSteven Whitehouse struct gfs2_sbd *sdp = bh->b_private; 45416615be1SSteven Whitehouse bh->b_private = NULL; 45516615be1SSteven Whitehouse 45616615be1SSteven Whitehouse end_buffer_write_sync(bh, uptodate); 45716615be1SSteven Whitehouse if (atomic_dec_and_test(&sdp->sd_log_in_flight)) 45816615be1SSteven Whitehouse wake_up(&sdp->sd_log_flush_wait); 45916615be1SSteven Whitehouse } 46016615be1SSteven Whitehouse 46116615be1SSteven Whitehouse /** 462b3b94faaSDavid Teigland * gfs2_log_get_buf - Get and initialize a buffer to use for log control data 463b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 464b3b94faaSDavid Teigland * 465b3b94faaSDavid Teigland * Returns: the buffer_head 466b3b94faaSDavid Teigland */ 467b3b94faaSDavid Teigland 468b3b94faaSDavid Teigland struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp) 469b3b94faaSDavid Teigland { 470cd915493SSteven Whitehouse u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); 471b3b94faaSDavid Teigland struct buffer_head *bh; 472b3b94faaSDavid Teigland 47316615be1SSteven Whitehouse bh = sb_getblk(sdp->sd_vfs, blkno); 474b3b94faaSDavid Teigland lock_buffer(bh); 475b3b94faaSDavid Teigland memset(bh->b_data, 0, bh->b_size); 476b3b94faaSDavid Teigland set_buffer_uptodate(bh); 477b3b94faaSDavid Teigland clear_buffer_dirty(bh); 47816615be1SSteven Whitehouse gfs2_log_incr_head(sdp); 47916615be1SSteven Whitehouse atomic_inc(&sdp->sd_log_in_flight); 48016615be1SSteven Whitehouse bh->b_private = sdp; 48116615be1SSteven Whitehouse bh->b_end_io = gfs2_log_write_endio; 482b3b94faaSDavid Teigland 483b3b94faaSDavid Teigland return bh; 484b3b94faaSDavid Teigland } 485b3b94faaSDavid Teigland 486b3b94faaSDavid Teigland /** 48716615be1SSteven Whitehouse * gfs2_fake_write_endio - 48816615be1SSteven Whitehouse * @bh: The buffer head 48916615be1SSteven Whitehouse * @uptodate: The I/O Status 49016615be1SSteven Whitehouse * 49116615be1SSteven Whitehouse */ 49216615be1SSteven Whitehouse 49316615be1SSteven Whitehouse static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate) 49416615be1SSteven Whitehouse { 49516615be1SSteven Whitehouse struct buffer_head *real_bh = bh->b_private; 4965a60c532SSteven Whitehouse struct gfs2_bufdata *bd = real_bh->b_private; 4975a60c532SSteven Whitehouse struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd; 49816615be1SSteven Whitehouse 49916615be1SSteven Whitehouse end_buffer_write_sync(bh, uptodate); 50016615be1SSteven Whitehouse free_buffer_head(bh); 50116615be1SSteven Whitehouse unlock_buffer(real_bh); 50216615be1SSteven Whitehouse brelse(real_bh); 50316615be1SSteven Whitehouse if (atomic_dec_and_test(&sdp->sd_log_in_flight)) 50416615be1SSteven Whitehouse wake_up(&sdp->sd_log_flush_wait); 50516615be1SSteven Whitehouse } 50616615be1SSteven Whitehouse 50716615be1SSteven Whitehouse /** 508b3b94faaSDavid Teigland * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log 509b3b94faaSDavid Teigland * @sdp: the filesystem 510b3b94faaSDavid Teigland * @data: the data the buffer_head should point to 511b3b94faaSDavid Teigland * 512b3b94faaSDavid Teigland * Returns: the log buffer descriptor 513b3b94faaSDavid Teigland */ 514b3b94faaSDavid Teigland 515b3b94faaSDavid Teigland struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp, 516b3b94faaSDavid Teigland struct buffer_head *real) 517b3b94faaSDavid Teigland { 518cd915493SSteven Whitehouse u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); 519b3b94faaSDavid Teigland struct buffer_head *bh; 520b3b94faaSDavid Teigland 52116615be1SSteven Whitehouse bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL); 522b3b94faaSDavid Teigland atomic_set(&bh->b_count, 1); 52316615be1SSteven Whitehouse bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock); 52418ec7d5cSSteven Whitehouse set_bh_page(bh, real->b_page, bh_offset(real)); 525b3b94faaSDavid Teigland bh->b_blocknr = blkno; 526b3b94faaSDavid Teigland bh->b_size = sdp->sd_sb.sb_bsize; 527b3b94faaSDavid Teigland bh->b_bdev = sdp->sd_vfs->s_bdev; 52816615be1SSteven Whitehouse bh->b_private = real; 52916615be1SSteven Whitehouse bh->b_end_io = gfs2_fake_write_endio; 530b3b94faaSDavid Teigland 53116615be1SSteven Whitehouse gfs2_log_incr_head(sdp); 53216615be1SSteven Whitehouse atomic_inc(&sdp->sd_log_in_flight); 533b3b94faaSDavid Teigland 534b3b94faaSDavid Teigland return bh; 535b3b94faaSDavid Teigland } 536b3b94faaSDavid Teigland 5372332c443SRobert Peterson static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail) 538b3b94faaSDavid Teigland { 539b3b94faaSDavid Teigland unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail); 540b3b94faaSDavid Teigland 541b3b94faaSDavid Teigland ail2_empty(sdp, new_tail); 542b3b94faaSDavid Teigland 543fd041f0bSSteven Whitehouse atomic_add(dist, &sdp->sd_log_blks_free); 54463997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, dist); 5455e687eacSBenjamin Marzinski gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= 5465e687eacSBenjamin Marzinski sdp->sd_jdesc->jd_blocks); 547b3b94faaSDavid Teigland 548b3b94faaSDavid Teigland sdp->sd_log_tail = new_tail; 549b3b94faaSDavid Teigland } 550b3b94faaSDavid Teigland 551b3b94faaSDavid Teigland /** 552b3b94faaSDavid Teigland * log_write_header - Get and initialize a journal header buffer 553b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 554b3b94faaSDavid Teigland * 555b3b94faaSDavid Teigland * Returns: the initialized log buffer descriptor 556b3b94faaSDavid Teigland */ 557b3b94faaSDavid Teigland 558cd915493SSteven Whitehouse static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull) 559b3b94faaSDavid Teigland { 560cd915493SSteven Whitehouse u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); 561b3b94faaSDavid Teigland struct buffer_head *bh; 562b3b94faaSDavid Teigland struct gfs2_log_header *lh; 563b3b94faaSDavid Teigland unsigned int tail; 564cd915493SSteven Whitehouse u32 hash; 565b3b94faaSDavid Teigland 566b3b94faaSDavid Teigland bh = sb_getblk(sdp->sd_vfs, blkno); 567b3b94faaSDavid Teigland lock_buffer(bh); 568b3b94faaSDavid Teigland memset(bh->b_data, 0, bh->b_size); 569b3b94faaSDavid Teigland set_buffer_uptodate(bh); 570b3b94faaSDavid Teigland clear_buffer_dirty(bh); 571b3b94faaSDavid Teigland 572b3b94faaSDavid Teigland gfs2_ail1_empty(sdp, 0); 573b3b94faaSDavid Teigland tail = current_tail(sdp); 574b3b94faaSDavid Teigland 575b3b94faaSDavid Teigland lh = (struct gfs2_log_header *)bh->b_data; 576b3b94faaSDavid Teigland memset(lh, 0, sizeof(struct gfs2_log_header)); 577b3b94faaSDavid Teigland lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC); 578e3167dedSSteven Whitehouse lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH); 5790ab7d13fSSteven Whitehouse lh->lh_header.__pad0 = cpu_to_be64(0); 580e3167dedSSteven Whitehouse lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH); 5810ab7d13fSSteven Whitehouse lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid); 582e0f2bf78SSteven Whitehouse lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++); 583e0f2bf78SSteven Whitehouse lh->lh_flags = cpu_to_be32(flags); 584e0f2bf78SSteven Whitehouse lh->lh_tail = cpu_to_be32(tail); 585e0f2bf78SSteven Whitehouse lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head); 586b3b94faaSDavid Teigland hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header)); 587b3b94faaSDavid Teigland lh->lh_hash = cpu_to_be32(hash); 588b3b94faaSDavid Teigland 589254db57fSSteven Whitehouse bh->b_end_io = end_buffer_write_sync; 590f1e4d518SChristoph Hellwig get_bh(bh); 591254db57fSSteven Whitehouse if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) 5927b6d91daSChristoph Hellwig submit_bh(WRITE_SYNC | REQ_META, bh); 593f1e4d518SChristoph Hellwig else 594f1e4d518SChristoph Hellwig submit_bh(WRITE_FLUSH_FUA | REQ_META, bh); 595254db57fSSteven Whitehouse wait_on_buffer(bh); 596f1e4d518SChristoph Hellwig 597254db57fSSteven Whitehouse if (!buffer_uptodate(bh)) 598b3b94faaSDavid Teigland gfs2_io_error_bh(sdp, bh); 599b3b94faaSDavid Teigland brelse(bh); 600b3b94faaSDavid Teigland 601b3b94faaSDavid Teigland if (sdp->sd_log_tail != tail) 6022332c443SRobert Peterson log_pull_tail(sdp, tail); 603b3b94faaSDavid Teigland else 604b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !pull); 605b3b94faaSDavid Teigland 606b3b94faaSDavid Teigland sdp->sd_log_idle = (tail == sdp->sd_log_flush_head); 60716615be1SSteven Whitehouse gfs2_log_incr_head(sdp); 608b3b94faaSDavid Teigland } 609b3b94faaSDavid Teigland 610b3b94faaSDavid Teigland static void log_flush_commit(struct gfs2_sbd *sdp) 611b3b94faaSDavid Teigland { 61216615be1SSteven Whitehouse DEFINE_WAIT(wait); 613b3b94faaSDavid Teigland 61416615be1SSteven Whitehouse if (atomic_read(&sdp->sd_log_in_flight)) { 61516615be1SSteven Whitehouse do { 61616615be1SSteven Whitehouse prepare_to_wait(&sdp->sd_log_flush_wait, &wait, 61716615be1SSteven Whitehouse TASK_UNINTERRUPTIBLE); 61816615be1SSteven Whitehouse if (atomic_read(&sdp->sd_log_in_flight)) 61916615be1SSteven Whitehouse io_schedule(); 62016615be1SSteven Whitehouse } while(atomic_read(&sdp->sd_log_in_flight)); 62116615be1SSteven Whitehouse finish_wait(&sdp->sd_log_flush_wait, &wait); 622b3b94faaSDavid Teigland } 623b3b94faaSDavid Teigland 624b3b94faaSDavid Teigland log_write_header(sdp, 0, 0); 625b3b94faaSDavid Teigland } 626b3b94faaSDavid Teigland 627d7b616e2SSteven Whitehouse static void gfs2_ordered_write(struct gfs2_sbd *sdp) 628d7b616e2SSteven Whitehouse { 629d7b616e2SSteven Whitehouse struct gfs2_bufdata *bd; 630d7b616e2SSteven Whitehouse struct buffer_head *bh; 631d7b616e2SSteven Whitehouse LIST_HEAD(written); 632d7b616e2SSteven Whitehouse 633d7b616e2SSteven Whitehouse gfs2_log_lock(sdp); 634d7b616e2SSteven Whitehouse while (!list_empty(&sdp->sd_log_le_ordered)) { 635d7b616e2SSteven Whitehouse bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list); 636d7b616e2SSteven Whitehouse list_move(&bd->bd_le.le_list, &written); 637d7b616e2SSteven Whitehouse bh = bd->bd_bh; 638d7b616e2SSteven Whitehouse if (!buffer_dirty(bh)) 639d7b616e2SSteven Whitehouse continue; 640d7b616e2SSteven Whitehouse get_bh(bh); 641d7b616e2SSteven Whitehouse gfs2_log_unlock(sdp); 642d7b616e2SSteven Whitehouse lock_buffer(bh); 643b8e7cbb6SSteven Whitehouse if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) { 644d7b616e2SSteven Whitehouse bh->b_end_io = end_buffer_write_sync; 645721a9602SJens Axboe submit_bh(WRITE_SYNC, bh); 646d7b616e2SSteven Whitehouse } else { 647d7b616e2SSteven Whitehouse unlock_buffer(bh); 648d7b616e2SSteven Whitehouse brelse(bh); 649d7b616e2SSteven Whitehouse } 650d7b616e2SSteven Whitehouse gfs2_log_lock(sdp); 651d7b616e2SSteven Whitehouse } 652d7b616e2SSteven Whitehouse list_splice(&written, &sdp->sd_log_le_ordered); 653d7b616e2SSteven Whitehouse gfs2_log_unlock(sdp); 654d7b616e2SSteven Whitehouse } 655d7b616e2SSteven Whitehouse 656d7b616e2SSteven Whitehouse static void gfs2_ordered_wait(struct gfs2_sbd *sdp) 657d7b616e2SSteven Whitehouse { 658d7b616e2SSteven Whitehouse struct gfs2_bufdata *bd; 659d7b616e2SSteven Whitehouse struct buffer_head *bh; 660d7b616e2SSteven Whitehouse 661d7b616e2SSteven Whitehouse gfs2_log_lock(sdp); 662d7b616e2SSteven Whitehouse while (!list_empty(&sdp->sd_log_le_ordered)) { 663d7b616e2SSteven Whitehouse bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list); 664d7b616e2SSteven Whitehouse bh = bd->bd_bh; 665d7b616e2SSteven Whitehouse if (buffer_locked(bh)) { 666d7b616e2SSteven Whitehouse get_bh(bh); 667d7b616e2SSteven Whitehouse gfs2_log_unlock(sdp); 668d7b616e2SSteven Whitehouse wait_on_buffer(bh); 669d7b616e2SSteven Whitehouse brelse(bh); 670d7b616e2SSteven Whitehouse gfs2_log_lock(sdp); 671d7b616e2SSteven Whitehouse continue; 672d7b616e2SSteven Whitehouse } 673d7b616e2SSteven Whitehouse list_del_init(&bd->bd_le.le_list); 674d7b616e2SSteven Whitehouse } 675d7b616e2SSteven Whitehouse gfs2_log_unlock(sdp); 676d7b616e2SSteven Whitehouse } 677d7b616e2SSteven Whitehouse 678b3b94faaSDavid Teigland /** 679b09e593dSSteven Whitehouse * gfs2_log_flush - flush incore transaction(s) 680b3b94faaSDavid Teigland * @sdp: the filesystem 681b3b94faaSDavid Teigland * @gl: The glock structure to flush. If NULL, flush the whole incore log 682b3b94faaSDavid Teigland * 683b3b94faaSDavid Teigland */ 684b3b94faaSDavid Teigland 685ed4878e8SBob Peterson void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl) 686b3b94faaSDavid Teigland { 687b3b94faaSDavid Teigland struct gfs2_ail *ai; 688b3b94faaSDavid Teigland 689484adff8SSteven Whitehouse down_write(&sdp->sd_log_flush_lock); 690f55ab26aSSteven Whitehouse 6912bcd610dSSteven Whitehouse /* Log might have been flushed while we waited for the flush lock */ 6922bcd610dSSteven Whitehouse if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) { 693484adff8SSteven Whitehouse up_write(&sdp->sd_log_flush_lock); 694f55ab26aSSteven Whitehouse return; 695f55ab26aSSteven Whitehouse } 69663997775SSteven Whitehouse trace_gfs2_log_flush(sdp, 1); 697f55ab26aSSteven Whitehouse 698b09e593dSSteven Whitehouse ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL); 699b09e593dSSteven Whitehouse INIT_LIST_HEAD(&ai->ai_ail1_list); 700b09e593dSSteven Whitehouse INIT_LIST_HEAD(&ai->ai_ail2_list); 701b3b94faaSDavid Teigland 70216615be1SSteven Whitehouse if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) { 70316615be1SSteven Whitehouse printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf, 70416615be1SSteven Whitehouse sdp->sd_log_commited_buf); 70516615be1SSteven Whitehouse gfs2_assert_withdraw(sdp, 0); 70616615be1SSteven Whitehouse } 70716615be1SSteven Whitehouse if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) { 70816615be1SSteven Whitehouse printk(KERN_INFO "GFS2: log databuf %u %u\n", 70916615be1SSteven Whitehouse sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf); 71016615be1SSteven Whitehouse gfs2_assert_withdraw(sdp, 0); 71116615be1SSteven Whitehouse } 712b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, 713b3b94faaSDavid Teigland sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke); 714b3b94faaSDavid Teigland 715b3b94faaSDavid Teigland sdp->sd_log_flush_head = sdp->sd_log_head; 716b3b94faaSDavid Teigland sdp->sd_log_flush_wrapped = 0; 717b3b94faaSDavid Teigland ai->ai_first = sdp->sd_log_flush_head; 718b3b94faaSDavid Teigland 719d7b616e2SSteven Whitehouse gfs2_ordered_write(sdp); 720b3b94faaSDavid Teigland lops_before_commit(sdp); 721d7b616e2SSteven Whitehouse gfs2_ordered_wait(sdp); 722d7b616e2SSteven Whitehouse 72316615be1SSteven Whitehouse if (sdp->sd_log_head != sdp->sd_log_flush_head) 724b3b94faaSDavid Teigland log_flush_commit(sdp); 7252332c443SRobert Peterson else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){ 7262332c443SRobert Peterson gfs2_log_lock(sdp); 727fd041f0bSSteven Whitehouse atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */ 72863997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, -1); 7292332c443SRobert Peterson gfs2_log_unlock(sdp); 730b3b94faaSDavid Teigland log_write_header(sdp, 0, PULL); 7312332c443SRobert Peterson } 732b3b94faaSDavid Teigland lops_after_commit(sdp, ai); 733fe1a698fSSteven Whitehouse 734fe1a698fSSteven Whitehouse gfs2_log_lock(sdp); 735b3b94faaSDavid Teigland sdp->sd_log_head = sdp->sd_log_flush_head; 736faa31ce8SSteven Whitehouse sdp->sd_log_blks_reserved = 0; 737faa31ce8SSteven Whitehouse sdp->sd_log_commited_buf = 0; 7382332c443SRobert Peterson sdp->sd_log_commited_databuf = 0; 739b3b94faaSDavid Teigland sdp->sd_log_commited_revoke = 0; 740b3b94faaSDavid Teigland 741d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 742b3b94faaSDavid Teigland if (!list_empty(&ai->ai_ail1_list)) { 743b3b94faaSDavid Teigland list_add(&ai->ai_list, &sdp->sd_ail1_list); 744b3b94faaSDavid Teigland ai = NULL; 745b3b94faaSDavid Teigland } 746d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 747b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 74863997775SSteven Whitehouse trace_gfs2_log_flush(sdp, 0); 749484adff8SSteven Whitehouse up_write(&sdp->sd_log_flush_lock); 750b3b94faaSDavid Teigland 751b3b94faaSDavid Teigland kfree(ai); 752b3b94faaSDavid Teigland } 753b3b94faaSDavid Teigland 754b3b94faaSDavid Teigland static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 755b3b94faaSDavid Teigland { 7562332c443SRobert Peterson unsigned int reserved; 757ac39aaddSSteven Whitehouse unsigned int unused; 758b3b94faaSDavid Teigland 759b3b94faaSDavid Teigland gfs2_log_lock(sdp); 760b3b94faaSDavid Teigland 761b3b94faaSDavid Teigland sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm; 7622332c443SRobert Peterson sdp->sd_log_commited_databuf += tr->tr_num_databuf_new - 7632332c443SRobert Peterson tr->tr_num_databuf_rm; 7642332c443SRobert Peterson gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) || 7652332c443SRobert Peterson (((int)sdp->sd_log_commited_databuf) >= 0)); 766b3b94faaSDavid Teigland sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm; 7672332c443SRobert Peterson reserved = calc_reserved(sdp); 76862be1f71SRoel Kluin gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved); 769ac39aaddSSteven Whitehouse unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved; 770ac39aaddSSteven Whitehouse atomic_add(unused, &sdp->sd_log_blks_free); 77163997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, unused); 772fd041f0bSSteven Whitehouse gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= 7732332c443SRobert Peterson sdp->sd_jdesc->jd_blocks); 774b3b94faaSDavid Teigland sdp->sd_log_blks_reserved = reserved; 775b3b94faaSDavid Teigland 776b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 777b3b94faaSDavid Teigland } 778b3b94faaSDavid Teigland 779d0109bfaSBob Peterson static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 780d0109bfaSBob Peterson { 781d0109bfaSBob Peterson struct list_head *head = &tr->tr_list_buf; 782d0109bfaSBob Peterson struct gfs2_bufdata *bd; 783d0109bfaSBob Peterson 784d0109bfaSBob Peterson gfs2_log_lock(sdp); 785d0109bfaSBob Peterson while (!list_empty(head)) { 786d0109bfaSBob Peterson bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr); 787d0109bfaSBob Peterson list_del_init(&bd->bd_list_tr); 788d0109bfaSBob Peterson tr->tr_num_buf--; 789d0109bfaSBob Peterson } 790d0109bfaSBob Peterson gfs2_log_unlock(sdp); 791d0109bfaSBob Peterson gfs2_assert_warn(sdp, !tr->tr_num_buf); 792d0109bfaSBob Peterson } 793d0109bfaSBob Peterson 794b3b94faaSDavid Teigland /** 795b3b94faaSDavid Teigland * gfs2_log_commit - Commit a transaction to the log 796b3b94faaSDavid Teigland * @sdp: the filesystem 797b3b94faaSDavid Teigland * @tr: the transaction 798b3b94faaSDavid Teigland * 7995e687eacSBenjamin Marzinski * We wake up gfs2_logd if the number of pinned blocks exceed thresh1 8005e687eacSBenjamin Marzinski * or the total number of used blocks (pinned blocks plus AIL blocks) 8015e687eacSBenjamin Marzinski * is greater than thresh2. 8025e687eacSBenjamin Marzinski * 8035e687eacSBenjamin Marzinski * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of 8045e687eacSBenjamin Marzinski * journal size. 8055e687eacSBenjamin Marzinski * 806b3b94faaSDavid Teigland * Returns: errno 807b3b94faaSDavid Teigland */ 808b3b94faaSDavid Teigland 809b3b94faaSDavid Teigland void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 810b3b94faaSDavid Teigland { 811b3b94faaSDavid Teigland log_refund(sdp, tr); 812d0109bfaSBob Peterson buf_lo_incore_commit(sdp, tr); 813b3b94faaSDavid Teigland 814484adff8SSteven Whitehouse up_read(&sdp->sd_log_flush_lock); 815b3b94faaSDavid Teigland 8165e687eacSBenjamin Marzinski if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) || 8175e687eacSBenjamin Marzinski ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) > 8185e687eacSBenjamin Marzinski atomic_read(&sdp->sd_log_thresh2))) 8195e687eacSBenjamin Marzinski wake_up(&sdp->sd_logd_waitq); 820faa31ce8SSteven Whitehouse } 821b3b94faaSDavid Teigland 822b3b94faaSDavid Teigland /** 823b3b94faaSDavid Teigland * gfs2_log_shutdown - write a shutdown header into a journal 824b3b94faaSDavid Teigland * @sdp: the filesystem 825b3b94faaSDavid Teigland * 826b3b94faaSDavid Teigland */ 827b3b94faaSDavid Teigland 828b3b94faaSDavid Teigland void gfs2_log_shutdown(struct gfs2_sbd *sdp) 829b3b94faaSDavid Teigland { 830484adff8SSteven Whitehouse down_write(&sdp->sd_log_flush_lock); 831b3b94faaSDavid Teigland 832b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved); 833b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf); 834b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); 835b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg); 836b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf); 837b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list)); 838b3b94faaSDavid Teigland 839b3b94faaSDavid Teigland sdp->sd_log_flush_head = sdp->sd_log_head; 840b3b94faaSDavid Teigland sdp->sd_log_flush_wrapped = 0; 841b3b94faaSDavid Teigland 8422332c443SRobert Peterson log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 8432332c443SRobert Peterson (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL); 844b3b94faaSDavid Teigland 845fd041f0bSSteven Whitehouse gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks); 846a74604beSSteven Whitehouse gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail); 847a74604beSSteven Whitehouse gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list)); 848b3b94faaSDavid Teigland 849b3b94faaSDavid Teigland sdp->sd_log_head = sdp->sd_log_flush_head; 850b3b94faaSDavid Teigland sdp->sd_log_tail = sdp->sd_log_head; 851b3b94faaSDavid Teigland 852484adff8SSteven Whitehouse up_write(&sdp->sd_log_flush_lock); 853b3b94faaSDavid Teigland } 854b3b94faaSDavid Teigland 855a25311c8SSteven Whitehouse 856a25311c8SSteven Whitehouse /** 857a25311c8SSteven Whitehouse * gfs2_meta_syncfs - sync all the buffers in a filesystem 858a25311c8SSteven Whitehouse * @sdp: the filesystem 859a25311c8SSteven Whitehouse * 860a25311c8SSteven Whitehouse */ 861a25311c8SSteven Whitehouse 862a25311c8SSteven Whitehouse void gfs2_meta_syncfs(struct gfs2_sbd *sdp) 863a25311c8SSteven Whitehouse { 864a25311c8SSteven Whitehouse gfs2_log_flush(sdp, NULL); 865a25311c8SSteven Whitehouse for (;;) { 8665e687eacSBenjamin Marzinski gfs2_ail1_start(sdp); 867a25311c8SSteven Whitehouse if (gfs2_ail1_empty(sdp, DIO_ALL)) 868a25311c8SSteven Whitehouse break; 869a25311c8SSteven Whitehouse msleep(10); 870a25311c8SSteven Whitehouse } 871a25311c8SSteven Whitehouse } 872a25311c8SSteven Whitehouse 8735e687eacSBenjamin Marzinski static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp) 8745e687eacSBenjamin Marzinski { 8755e687eacSBenjamin Marzinski return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1)); 8765e687eacSBenjamin Marzinski } 8775e687eacSBenjamin Marzinski 8785e687eacSBenjamin Marzinski static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp) 8795e687eacSBenjamin Marzinski { 8805e687eacSBenjamin Marzinski unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free); 8815e687eacSBenjamin Marzinski return used_blocks >= atomic_read(&sdp->sd_log_thresh2); 8825e687eacSBenjamin Marzinski } 883ec69b188SSteven Whitehouse 884ec69b188SSteven Whitehouse /** 885ec69b188SSteven Whitehouse * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks 886ec69b188SSteven Whitehouse * @sdp: Pointer to GFS2 superblock 887ec69b188SSteven Whitehouse * 888ec69b188SSteven Whitehouse * Also, periodically check to make sure that we're using the most recent 889ec69b188SSteven Whitehouse * journal index. 890ec69b188SSteven Whitehouse */ 891ec69b188SSteven Whitehouse 892ec69b188SSteven Whitehouse int gfs2_logd(void *data) 893ec69b188SSteven Whitehouse { 894ec69b188SSteven Whitehouse struct gfs2_sbd *sdp = data; 8955e687eacSBenjamin Marzinski unsigned long t = 1; 8965e687eacSBenjamin Marzinski DEFINE_WAIT(wait); 8975e687eacSBenjamin Marzinski unsigned preflush; 898ec69b188SSteven Whitehouse 899ec69b188SSteven Whitehouse while (!kthread_should_stop()) { 900ec69b188SSteven Whitehouse 9015e687eacSBenjamin Marzinski preflush = atomic_read(&sdp->sd_log_pinned); 9025e687eacSBenjamin Marzinski if (gfs2_jrnl_flush_reqd(sdp) || t == 0) { 903ec69b188SSteven Whitehouse gfs2_ail1_empty(sdp, DIO_ALL); 904ec69b188SSteven Whitehouse gfs2_log_flush(sdp, NULL); 9055e687eacSBenjamin Marzinski gfs2_ail1_empty(sdp, DIO_ALL); 906ec69b188SSteven Whitehouse } 907ec69b188SSteven Whitehouse 9085e687eacSBenjamin Marzinski if (gfs2_ail_flush_reqd(sdp)) { 9095e687eacSBenjamin Marzinski gfs2_ail1_start(sdp); 9105e687eacSBenjamin Marzinski io_schedule(); 9115e687eacSBenjamin Marzinski gfs2_ail1_empty(sdp, 0); 9125e687eacSBenjamin Marzinski gfs2_log_flush(sdp, NULL); 9135e687eacSBenjamin Marzinski gfs2_ail1_empty(sdp, DIO_ALL); 9145e687eacSBenjamin Marzinski } 9155e687eacSBenjamin Marzinski 9165e687eacSBenjamin Marzinski wake_up(&sdp->sd_log_waitq); 917ec69b188SSteven Whitehouse t = gfs2_tune_get(sdp, gt_logd_secs) * HZ; 918ec69b188SSteven Whitehouse if (freezing(current)) 919ec69b188SSteven Whitehouse refrigerator(); 9205e687eacSBenjamin Marzinski 9215e687eacSBenjamin Marzinski do { 9225e687eacSBenjamin Marzinski prepare_to_wait(&sdp->sd_logd_waitq, &wait, 9235f487490SSteven Whitehouse TASK_INTERRUPTIBLE); 9245e687eacSBenjamin Marzinski if (!gfs2_ail_flush_reqd(sdp) && 9255e687eacSBenjamin Marzinski !gfs2_jrnl_flush_reqd(sdp) && 9265e687eacSBenjamin Marzinski !kthread_should_stop()) 9275e687eacSBenjamin Marzinski t = schedule_timeout(t); 9285e687eacSBenjamin Marzinski } while(t && !gfs2_ail_flush_reqd(sdp) && 9295e687eacSBenjamin Marzinski !gfs2_jrnl_flush_reqd(sdp) && 9305e687eacSBenjamin Marzinski !kthread_should_stop()); 9315e687eacSBenjamin Marzinski finish_wait(&sdp->sd_logd_waitq, &wait); 932ec69b188SSteven Whitehouse } 933ec69b188SSteven Whitehouse 934ec69b188SSteven Whitehouse return 0; 935ec69b188SSteven Whitehouse } 936ec69b188SSteven Whitehouse 937