17336d0e6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2b3b94faaSDavid Teigland /* 3b3b94faaSDavid Teigland * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 4da6dd40dSBob Peterson * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. 5b3b94faaSDavid Teigland */ 6b3b94faaSDavid Teigland 7b3b94faaSDavid Teigland #include <linux/sched.h> 8b3b94faaSDavid Teigland #include <linux/slab.h> 9b3b94faaSDavid Teigland #include <linux/spinlock.h> 10b3b94faaSDavid Teigland #include <linux/completion.h> 11b3b94faaSDavid Teigland #include <linux/buffer_head.h> 125c676f6dSSteven Whitehouse #include <linux/gfs2_ondisk.h> 1371b86f56SSteven Whitehouse #include <linux/crc32.h> 14c1696fb8SBob Peterson #include <linux/crc32c.h> 15a25311c8SSteven Whitehouse #include <linux/delay.h> 16ec69b188SSteven Whitehouse #include <linux/kthread.h> 17ec69b188SSteven Whitehouse #include <linux/freezer.h> 18254db57fSSteven Whitehouse #include <linux/bio.h> 19885bcecaSSteven Whitehouse #include <linux/blkdev.h> 204667a0ecSSteven Whitehouse #include <linux/writeback.h> 214a36d08dSBob Peterson #include <linux/list_sort.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" 33b839dadaSBob Peterson #include "trans.h" 34b3b94faaSDavid Teigland 35feed98a8SBob Peterson static void gfs2_log_shutdown(struct gfs2_sbd *sdp); 36feed98a8SBob Peterson 37b3b94faaSDavid Teigland /** 38b3b94faaSDavid Teigland * gfs2_struct2blk - compute stuff 39b3b94faaSDavid Teigland * @sdp: the filesystem 40b3b94faaSDavid Teigland * @nstruct: the number of 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 482e9eeaa1SBob Peterson unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct) 49b3b94faaSDavid Teigland { 50b3b94faaSDavid Teigland unsigned int blks; 51b3b94faaSDavid Teigland unsigned int first, second; 52b3b94faaSDavid Teigland 536188e877SAndreas Gruenbacher /* The initial struct gfs2_log_descriptor block */ 54b3b94faaSDavid Teigland blks = 1; 552e9eeaa1SBob Peterson first = sdp->sd_ldptrs; 56b3b94faaSDavid Teigland 57b3b94faaSDavid Teigland if (nstruct > first) { 586188e877SAndreas Gruenbacher /* Subsequent struct gfs2_meta_header blocks */ 592e9eeaa1SBob Peterson second = sdp->sd_inptrs; 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 7568942870SBob Peterson void gfs2_remove_from_ail(struct gfs2_bufdata *bd) 761e1a3d03SSteven Whitehouse { 7716ca9412SBenjamin Marzinski bd->bd_tr = 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, 9469511080SBob Peterson struct gfs2_trans *tr) 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; 102b1676cbbSBob Peterson int ret = 0; 103ddacfaf7SSteven Whitehouse 10416ca9412SBenjamin Marzinski list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) { 105ddacfaf7SSteven Whitehouse bh = bd->bd_bh; 106ddacfaf7SSteven Whitehouse 10716ca9412SBenjamin Marzinski gfs2_assert(sdp, bd->bd_tr == tr); 108ddacfaf7SSteven Whitehouse 109ddacfaf7SSteven Whitehouse if (!buffer_busy(bh)) { 11030fe70a8SBob Peterson if (buffer_uptodate(bh)) { 11130fe70a8SBob Peterson list_move(&bd->bd_ail_st_list, 11230fe70a8SBob Peterson &tr->tr_ail2_list); 11330fe70a8SBob Peterson continue; 11430fe70a8SBob Peterson } 115036330c9SBob Peterson if (!cmpxchg(&sdp->sd_log_error, 0, -EIO)) { 116ddacfaf7SSteven Whitehouse gfs2_io_error_bh(sdp, bh); 11769511080SBob Peterson gfs2_withdraw_delayed(sdp); 1189e1a9ecdSAndreas Gruenbacher } 119ddacfaf7SSteven Whitehouse } 120ddacfaf7SSteven Whitehouse 12130fe70a8SBob Peterson if (gfs2_withdrawn(sdp)) { 12230fe70a8SBob Peterson gfs2_remove_from_ail(bd); 12330fe70a8SBob Peterson continue; 12430fe70a8SBob Peterson } 125ddacfaf7SSteven Whitehouse if (!buffer_dirty(bh)) 126ddacfaf7SSteven Whitehouse continue; 1275ac048bbSSteven Whitehouse if (gl == bd->bd_gl) 1285ac048bbSSteven Whitehouse continue; 1295ac048bbSSteven Whitehouse gl = bd->bd_gl; 13016ca9412SBenjamin Marzinski list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list); 1314667a0ecSSteven Whitehouse mapping = bh->b_page->mapping; 1324f1de018SSteven Whitehouse if (!mapping) 1334f1de018SSteven Whitehouse continue; 134d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 135b1676cbbSBob Peterson ret = generic_writepages(mapping, wbc); 136d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 1374e79e3f0SBob Peterson if (ret == -ENODATA) /* if a jdata write into a new hole */ 1384e79e3f0SBob Peterson ret = 0; /* ignore it */ 139b1676cbbSBob Peterson if (ret || wbc->nr_to_write <= 0) 140ddacfaf7SSteven Whitehouse break; 141b1676cbbSBob Peterson return -EBUSY; 142ddacfaf7SSteven Whitehouse } 1434f1de018SSteven Whitehouse 144b1676cbbSBob Peterson return ret; 1454667a0ecSSteven Whitehouse } 1464667a0ecSSteven Whitehouse 1479592ea80SBob Peterson static void dump_ail_list(struct gfs2_sbd *sdp) 1489592ea80SBob Peterson { 1499592ea80SBob Peterson struct gfs2_trans *tr; 1509592ea80SBob Peterson struct gfs2_bufdata *bd; 1519592ea80SBob Peterson struct buffer_head *bh; 1529592ea80SBob Peterson 1539592ea80SBob Peterson list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) { 1549592ea80SBob Peterson list_for_each_entry_reverse(bd, &tr->tr_ail1_list, 1559592ea80SBob Peterson bd_ail_st_list) { 1569592ea80SBob Peterson bh = bd->bd_bh; 1579592ea80SBob Peterson fs_err(sdp, "bd %p: blk:0x%llx bh=%p ", bd, 1589592ea80SBob Peterson (unsigned long long)bd->bd_blkno, bh); 1599592ea80SBob Peterson if (!bh) { 1609592ea80SBob Peterson fs_err(sdp, "\n"); 1619592ea80SBob Peterson continue; 1629592ea80SBob Peterson } 1639592ea80SBob Peterson fs_err(sdp, "0x%llx up2:%d dirt:%d lkd:%d req:%d " 1649592ea80SBob Peterson "map:%d new:%d ar:%d aw:%d delay:%d " 1659592ea80SBob Peterson "io err:%d unwritten:%d dfr:%d pin:%d esc:%d\n", 1669592ea80SBob Peterson (unsigned long long)bh->b_blocknr, 1679592ea80SBob Peterson buffer_uptodate(bh), buffer_dirty(bh), 1689592ea80SBob Peterson buffer_locked(bh), buffer_req(bh), 1699592ea80SBob Peterson buffer_mapped(bh), buffer_new(bh), 1709592ea80SBob Peterson buffer_async_read(bh), buffer_async_write(bh), 1719592ea80SBob Peterson buffer_delay(bh), buffer_write_io_error(bh), 1729592ea80SBob Peterson buffer_unwritten(bh), 1739592ea80SBob Peterson buffer_defer_completion(bh), 1749592ea80SBob Peterson buffer_pinned(bh), buffer_escaped(bh)); 1759592ea80SBob Peterson } 1769592ea80SBob Peterson } 1779592ea80SBob Peterson } 1784667a0ecSSteven Whitehouse 1794667a0ecSSteven Whitehouse /** 1804667a0ecSSteven Whitehouse * gfs2_ail1_flush - start writeback of some ail1 entries 1814667a0ecSSteven Whitehouse * @sdp: The super block 1824667a0ecSSteven Whitehouse * @wbc: The writeback control structure 1834667a0ecSSteven Whitehouse * 1844667a0ecSSteven Whitehouse * Writes back some ail1 entries, according to the limits in the 1854667a0ecSSteven Whitehouse * writeback control structure 1864667a0ecSSteven Whitehouse */ 1874667a0ecSSteven Whitehouse 1884667a0ecSSteven Whitehouse void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc) 1894667a0ecSSteven Whitehouse { 1904667a0ecSSteven Whitehouse struct list_head *head = &sdp->sd_ail1_list; 19116ca9412SBenjamin Marzinski struct gfs2_trans *tr; 192885bcecaSSteven Whitehouse struct blk_plug plug; 19375b46c43SBob Peterson int ret; 1949592ea80SBob Peterson unsigned long flush_start = jiffies; 1954667a0ecSSteven Whitehouse 196c83ae9caSSteven Whitehouse trace_gfs2_ail_flush(sdp, wbc, 1); 197885bcecaSSteven Whitehouse blk_start_plug(&plug); 1984667a0ecSSteven Whitehouse spin_lock(&sdp->sd_ail_lock); 1994f1de018SSteven Whitehouse restart: 20075b46c43SBob Peterson ret = 0; 2019592ea80SBob Peterson if (time_after(jiffies, flush_start + (HZ * 600))) { 202d5dc3d96SBob Peterson fs_err(sdp, "Error: In %s for ten minutes! t=%d\n", 203d5dc3d96SBob Peterson __func__, current->journal_info ? 1 : 0); 2049592ea80SBob Peterson dump_ail_list(sdp); 2059592ea80SBob Peterson goto out; 2069592ea80SBob Peterson } 20716ca9412SBenjamin Marzinski list_for_each_entry_reverse(tr, head, tr_list) { 2084667a0ecSSteven Whitehouse if (wbc->nr_to_write <= 0) 2094667a0ecSSteven Whitehouse break; 210b1676cbbSBob Peterson ret = gfs2_ail1_start_one(sdp, wbc, tr); 211b1676cbbSBob Peterson if (ret) { 212b1676cbbSBob Peterson if (ret == -EBUSY) 2134f1de018SSteven Whitehouse goto restart; 214b1676cbbSBob Peterson break; 215b1676cbbSBob Peterson } 2164667a0ecSSteven Whitehouse } 2179592ea80SBob Peterson out: 2184667a0ecSSteven Whitehouse spin_unlock(&sdp->sd_ail_lock); 219885bcecaSSteven Whitehouse blk_finish_plug(&plug); 22049003128SBob Peterson if (ret) { 22149003128SBob Peterson gfs2_lm(sdp, "gfs2_ail1_start_one (generic_writepages) " 22249003128SBob Peterson "returned: %d\n", ret); 223badb55ecSAndreas Gruenbacher gfs2_withdraw(sdp); 22449003128SBob Peterson } 225c83ae9caSSteven Whitehouse trace_gfs2_ail_flush(sdp, wbc, 0); 2264667a0ecSSteven Whitehouse } 2274667a0ecSSteven Whitehouse 2284667a0ecSSteven Whitehouse /** 2294667a0ecSSteven Whitehouse * gfs2_ail1_start - start writeback of all ail1 entries 2304667a0ecSSteven Whitehouse * @sdp: The superblock 2314667a0ecSSteven Whitehouse */ 2324667a0ecSSteven Whitehouse 2334667a0ecSSteven Whitehouse static void gfs2_ail1_start(struct gfs2_sbd *sdp) 2344667a0ecSSteven Whitehouse { 2354667a0ecSSteven Whitehouse struct writeback_control wbc = { 2364667a0ecSSteven Whitehouse .sync_mode = WB_SYNC_NONE, 2374667a0ecSSteven Whitehouse .nr_to_write = LONG_MAX, 2384667a0ecSSteven Whitehouse .range_start = 0, 2394667a0ecSSteven Whitehouse .range_end = LLONG_MAX, 2404667a0ecSSteven Whitehouse }; 2414667a0ecSSteven Whitehouse 2424667a0ecSSteven Whitehouse return gfs2_ail1_flush(sdp, &wbc); 243ddacfaf7SSteven Whitehouse } 244ddacfaf7SSteven Whitehouse 245ddacfaf7SSteven Whitehouse /** 246ddacfaf7SSteven Whitehouse * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced 247ddacfaf7SSteven Whitehouse * @sdp: the filesystem 2485e4c7632SBob Peterson * @tr: the transaction 2495e4c7632SBob Peterson * @max_revokes: If nonzero, issue revokes for the bd items for written buffers 250ddacfaf7SSteven Whitehouse * 25136c78309SBob Peterson * returns: the transaction's count of remaining active items 252ddacfaf7SSteven Whitehouse */ 253ddacfaf7SSteven Whitehouse 25436c78309SBob Peterson static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr, 2555e4c7632SBob Peterson int *max_revokes) 256ddacfaf7SSteven Whitehouse { 257ddacfaf7SSteven Whitehouse struct gfs2_bufdata *bd, *s; 258ddacfaf7SSteven Whitehouse struct buffer_head *bh; 25936c78309SBob Peterson int active_count = 0; 260ddacfaf7SSteven Whitehouse 26116ca9412SBenjamin Marzinski list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, 262ddacfaf7SSteven Whitehouse bd_ail_st_list) { 263ddacfaf7SSteven Whitehouse bh = bd->bd_bh; 26416ca9412SBenjamin Marzinski gfs2_assert(sdp, bd->bd_tr == tr); 265036330c9SBob Peterson /* 266036330c9SBob Peterson * If another process flagged an io error, e.g. writing to the 267036330c9SBob Peterson * journal, error all other bhs and move them off the ail1 to 268036330c9SBob Peterson * prevent a tight loop when unmount tries to flush ail1, 269036330c9SBob Peterson * regardless of whether they're still busy. If no outside 270036330c9SBob Peterson * errors were found and the buffer is busy, move to the next. 271036330c9SBob Peterson * If the ail buffer is not busy and caught an error, flag it 272036330c9SBob Peterson * for others. 273036330c9SBob Peterson */ 27436c78309SBob Peterson if (!sdp->sd_log_error && buffer_busy(bh)) { 27536c78309SBob Peterson active_count++; 276ddacfaf7SSteven Whitehouse continue; 27736c78309SBob Peterson } 278b524abccSBob Peterson if (!buffer_uptodate(bh) && 279036330c9SBob Peterson !cmpxchg(&sdp->sd_log_error, 0, -EIO)) { 280ddacfaf7SSteven Whitehouse gfs2_io_error_bh(sdp, bh); 28169511080SBob Peterson gfs2_withdraw_delayed(sdp); 2829e1a9ecdSAndreas Gruenbacher } 2835e4c7632SBob Peterson /* 2845e4c7632SBob Peterson * If we have space for revokes and the bd is no longer on any 2855e4c7632SBob Peterson * buf list, we can just add a revoke for it immediately and 2865e4c7632SBob Peterson * avoid having to put it on the ail2 list, where it would need 2875e4c7632SBob Peterson * to be revoked later. 2885e4c7632SBob Peterson */ 2895e4c7632SBob Peterson if (*max_revokes && list_empty(&bd->bd_list)) { 2905e4c7632SBob Peterson gfs2_add_revoke(sdp, bd); 2915e4c7632SBob Peterson (*max_revokes)--; 2925e4c7632SBob Peterson continue; 2935e4c7632SBob Peterson } 29416ca9412SBenjamin Marzinski list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list); 295ddacfaf7SSteven Whitehouse } 29636c78309SBob Peterson return active_count; 297ddacfaf7SSteven Whitehouse } 298ddacfaf7SSteven Whitehouse 2994667a0ecSSteven Whitehouse /** 3004667a0ecSSteven Whitehouse * gfs2_ail1_empty - Try to empty the ail1 lists 3014667a0ecSSteven Whitehouse * @sdp: The superblock 3025e4c7632SBob Peterson * @max_revokes: If non-zero, add revokes where appropriate 3034667a0ecSSteven Whitehouse * 3044667a0ecSSteven Whitehouse * Tries to empty the ail1 lists, starting with the oldest first 3054667a0ecSSteven Whitehouse */ 306b3b94faaSDavid Teigland 3075e4c7632SBob Peterson static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int max_revokes) 308b3b94faaSDavid Teigland { 30916ca9412SBenjamin Marzinski struct gfs2_trans *tr, *s; 3105d054964SBenjamin Marzinski int oldest_tr = 1; 311b3b94faaSDavid Teigland int ret; 312b3b94faaSDavid Teigland 313d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 31416ca9412SBenjamin Marzinski list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) { 31536c78309SBob Peterson if (!gfs2_ail1_empty_one(sdp, tr, &max_revokes) && oldest_tr) 31616ca9412SBenjamin Marzinski list_move(&tr->tr_list, &sdp->sd_ail2_list); 3174667a0ecSSteven Whitehouse else 3185d054964SBenjamin Marzinski oldest_tr = 0; 319b3b94faaSDavid Teigland } 320b3b94faaSDavid Teigland ret = list_empty(&sdp->sd_ail1_list); 321d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 322b3b94faaSDavid Teigland 32369511080SBob Peterson if (test_bit(SDF_WITHDRAWING, &sdp->sd_flags)) { 324badb55ecSAndreas Gruenbacher gfs2_lm(sdp, "fatal: I/O error(s)\n"); 325badb55ecSAndreas Gruenbacher gfs2_withdraw(sdp); 326badb55ecSAndreas Gruenbacher } 3279e1a9ecdSAndreas Gruenbacher 328b3b94faaSDavid Teigland return ret; 329b3b94faaSDavid Teigland } 330b3b94faaSDavid Teigland 33126b06a69SSteven Whitehouse static void gfs2_ail1_wait(struct gfs2_sbd *sdp) 33226b06a69SSteven Whitehouse { 33316ca9412SBenjamin Marzinski struct gfs2_trans *tr; 33426b06a69SSteven Whitehouse struct gfs2_bufdata *bd; 33526b06a69SSteven Whitehouse struct buffer_head *bh; 33626b06a69SSteven Whitehouse 33726b06a69SSteven Whitehouse spin_lock(&sdp->sd_ail_lock); 33816ca9412SBenjamin Marzinski list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) { 33916ca9412SBenjamin Marzinski list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) { 34026b06a69SSteven Whitehouse bh = bd->bd_bh; 34126b06a69SSteven Whitehouse if (!buffer_locked(bh)) 34226b06a69SSteven Whitehouse continue; 34326b06a69SSteven Whitehouse get_bh(bh); 34426b06a69SSteven Whitehouse spin_unlock(&sdp->sd_ail_lock); 34526b06a69SSteven Whitehouse wait_on_buffer(bh); 34626b06a69SSteven Whitehouse brelse(bh); 34726b06a69SSteven Whitehouse return; 34826b06a69SSteven Whitehouse } 34926b06a69SSteven Whitehouse } 35026b06a69SSteven Whitehouse spin_unlock(&sdp->sd_ail_lock); 35126b06a69SSteven Whitehouse } 352ddacfaf7SSteven Whitehouse 353ddacfaf7SSteven Whitehouse /** 3542ca0c2fbSBob Peterson * gfs2_ail_empty_tr - empty one of the ail lists for a transaction 355ddacfaf7SSteven Whitehouse */ 356ddacfaf7SSteven Whitehouse 3572ca0c2fbSBob Peterson static void gfs2_ail_empty_tr(struct gfs2_sbd *sdp, struct gfs2_trans *tr, 3582ca0c2fbSBob Peterson struct list_head *head) 359ddacfaf7SSteven Whitehouse { 360ddacfaf7SSteven Whitehouse struct gfs2_bufdata *bd; 361ddacfaf7SSteven Whitehouse 362ddacfaf7SSteven Whitehouse while (!list_empty(head)) { 3632ca0c2fbSBob Peterson bd = list_first_entry(head, struct gfs2_bufdata, 364ddacfaf7SSteven Whitehouse bd_ail_st_list); 36516ca9412SBenjamin Marzinski gfs2_assert(sdp, bd->bd_tr == tr); 366f91a0d3eSSteven Whitehouse gfs2_remove_from_ail(bd); 367ddacfaf7SSteven Whitehouse } 368ddacfaf7SSteven Whitehouse } 369ddacfaf7SSteven Whitehouse 3706e80674aSAndreas Gruenbacher static void __ail2_empty(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 371b3b94faaSDavid Teigland { 3722ca0c2fbSBob Peterson gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list); 37316ca9412SBenjamin Marzinski list_del(&tr->tr_list); 37416ca9412SBenjamin Marzinski gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list)); 37516ca9412SBenjamin Marzinski gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list)); 376b839dadaSBob Peterson gfs2_trans_free(sdp, tr); 377b3b94faaSDavid Teigland } 378b3b94faaSDavid Teigland 3796e80674aSAndreas Gruenbacher static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail) 3806e80674aSAndreas Gruenbacher { 3816e80674aSAndreas Gruenbacher struct list_head *ail2_list = &sdp->sd_ail2_list; 3826e80674aSAndreas Gruenbacher unsigned int old_tail = sdp->sd_log_tail; 3836e80674aSAndreas Gruenbacher struct gfs2_trans *tr, *safe; 3846e80674aSAndreas Gruenbacher 3856e80674aSAndreas Gruenbacher spin_lock(&sdp->sd_ail_lock); 3866e80674aSAndreas Gruenbacher if (old_tail <= new_tail) { 3876e80674aSAndreas Gruenbacher list_for_each_entry_safe(tr, safe, ail2_list, tr_list) { 3886e80674aSAndreas Gruenbacher if (old_tail <= tr->tr_first && tr->tr_first < new_tail) 3896e80674aSAndreas Gruenbacher __ail2_empty(sdp, tr); 3906e80674aSAndreas Gruenbacher } 3916e80674aSAndreas Gruenbacher } else { 3926e80674aSAndreas Gruenbacher list_for_each_entry_safe(tr, safe, ail2_list, tr_list) { 3936e80674aSAndreas Gruenbacher if (old_tail <= tr->tr_first || tr->tr_first < new_tail) 3946e80674aSAndreas Gruenbacher __ail2_empty(sdp, tr); 3956e80674aSAndreas Gruenbacher } 3966e80674aSAndreas Gruenbacher } 397d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 398b3b94faaSDavid Teigland } 399b3b94faaSDavid Teigland 400b3b94faaSDavid Teigland /** 40124972557SBenjamin Marzinski * gfs2_log_release - Release a given number of log blocks 40224972557SBenjamin Marzinski * @sdp: The GFS2 superblock 40324972557SBenjamin Marzinski * @blks: The number of blocks 40424972557SBenjamin Marzinski * 40524972557SBenjamin Marzinski */ 40624972557SBenjamin Marzinski 40724972557SBenjamin Marzinski void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks) 40824972557SBenjamin Marzinski { 40924972557SBenjamin Marzinski 41024972557SBenjamin Marzinski atomic_add(blks, &sdp->sd_log_blks_free); 41124972557SBenjamin Marzinski trace_gfs2_log_blocks(sdp, blks); 41224972557SBenjamin Marzinski gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= 41324972557SBenjamin Marzinski sdp->sd_jdesc->jd_blocks); 41424972557SBenjamin Marzinski up_read(&sdp->sd_log_flush_lock); 41524972557SBenjamin Marzinski } 41624972557SBenjamin Marzinski 41724972557SBenjamin Marzinski /** 418b3b94faaSDavid Teigland * gfs2_log_reserve - Make a log reservation 419b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 420b3b94faaSDavid Teigland * @blks: The number of blocks to reserve 421b3b94faaSDavid Teigland * 42289918647SSteven Whitehouse * Note that we never give out the last few blocks of the journal. Thats 4232332c443SRobert Peterson * due to the fact that there is a small number of header blocks 424b004157aSSteven Whitehouse * associated with each log flush. The exact number can't be known until 425b004157aSSteven Whitehouse * flush time, so we ensure that we have just enough free blocks at all 426b004157aSSteven Whitehouse * times to avoid running out during a log flush. 427b004157aSSteven Whitehouse * 4285e687eacSBenjamin Marzinski * We no longer flush the log here, instead we wake up logd to do that 4295e687eacSBenjamin Marzinski * for us. To avoid the thundering herd and to ensure that we deal fairly 4305e687eacSBenjamin Marzinski * with queued waiters, we use an exclusive wait. This means that when we 4315e687eacSBenjamin Marzinski * get woken with enough journal space to get our reservation, we need to 4325e687eacSBenjamin Marzinski * wake the next waiter on the list. 4335e687eacSBenjamin Marzinski * 434b3b94faaSDavid Teigland * Returns: errno 435b3b94faaSDavid Teigland */ 436b3b94faaSDavid Teigland 437b3b94faaSDavid Teigland int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks) 438b3b94faaSDavid Teigland { 4392e60d768SBenjamin Marzinski int ret = 0; 4405d054964SBenjamin Marzinski unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize); 4415e687eacSBenjamin Marzinski unsigned wanted = blks + reserved_blks; 4425e687eacSBenjamin Marzinski DEFINE_WAIT(wait); 4435e687eacSBenjamin Marzinski int did_wait = 0; 4445e687eacSBenjamin Marzinski unsigned int free_blocks; 445b3b94faaSDavid Teigland 446b3b94faaSDavid Teigland if (gfs2_assert_warn(sdp, blks) || 447b3b94faaSDavid Teigland gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks)) 448b3b94faaSDavid Teigland return -EINVAL; 449f07b3520SBob Peterson atomic_add(blks, &sdp->sd_log_blks_needed); 4505e687eacSBenjamin Marzinski retry: 4515e687eacSBenjamin Marzinski free_blocks = atomic_read(&sdp->sd_log_blks_free); 4525e687eacSBenjamin Marzinski if (unlikely(free_blocks <= wanted)) { 4535e687eacSBenjamin Marzinski do { 4545e687eacSBenjamin Marzinski prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait, 4555e687eacSBenjamin Marzinski TASK_UNINTERRUPTIBLE); 4565e687eacSBenjamin Marzinski wake_up(&sdp->sd_logd_waitq); 4575e687eacSBenjamin Marzinski did_wait = 1; 4585e687eacSBenjamin Marzinski if (atomic_read(&sdp->sd_log_blks_free) <= wanted) 4595e687eacSBenjamin Marzinski io_schedule(); 4605e687eacSBenjamin Marzinski free_blocks = atomic_read(&sdp->sd_log_blks_free); 4615e687eacSBenjamin Marzinski } while(free_blocks <= wanted); 4625e687eacSBenjamin Marzinski finish_wait(&sdp->sd_log_waitq, &wait); 463b3b94faaSDavid Teigland } 4642e60d768SBenjamin Marzinski atomic_inc(&sdp->sd_reserving_log); 4655e687eacSBenjamin Marzinski if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks, 4662e60d768SBenjamin Marzinski free_blocks - blks) != free_blocks) { 4672e60d768SBenjamin Marzinski if (atomic_dec_and_test(&sdp->sd_reserving_log)) 4682e60d768SBenjamin Marzinski wake_up(&sdp->sd_reserving_log_wait); 4695e687eacSBenjamin Marzinski goto retry; 4702e60d768SBenjamin Marzinski } 471f07b3520SBob Peterson atomic_sub(blks, &sdp->sd_log_blks_needed); 47263997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, -blks); 4735e687eacSBenjamin Marzinski 4745e687eacSBenjamin Marzinski /* 4755e687eacSBenjamin Marzinski * If we waited, then so might others, wake them up _after_ we get 4765e687eacSBenjamin Marzinski * our share of the log. 4775e687eacSBenjamin Marzinski */ 4785e687eacSBenjamin Marzinski if (unlikely(did_wait)) 4795e687eacSBenjamin Marzinski wake_up(&sdp->sd_log_waitq); 480484adff8SSteven Whitehouse 481484adff8SSteven Whitehouse down_read(&sdp->sd_log_flush_lock); 48224972557SBenjamin Marzinski if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) { 48324972557SBenjamin Marzinski gfs2_log_release(sdp, blks); 4842e60d768SBenjamin Marzinski ret = -EROFS; 48524972557SBenjamin Marzinski } 4862e60d768SBenjamin Marzinski if (atomic_dec_and_test(&sdp->sd_reserving_log)) 4872e60d768SBenjamin Marzinski wake_up(&sdp->sd_reserving_log_wait); 4882e60d768SBenjamin Marzinski return ret; 489b3b94faaSDavid Teigland } 490b3b94faaSDavid Teigland 491b3b94faaSDavid Teigland /** 492b3b94faaSDavid Teigland * log_distance - Compute distance between two journal blocks 493b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 494b3b94faaSDavid Teigland * @newer: The most recent journal block of the pair 495b3b94faaSDavid Teigland * @older: The older journal block of the pair 496b3b94faaSDavid Teigland * 497b3b94faaSDavid Teigland * Compute the distance (in the journal direction) between two 498b3b94faaSDavid Teigland * blocks in the journal 499b3b94faaSDavid Teigland * 500b3b94faaSDavid Teigland * Returns: the distance in blocks 501b3b94faaSDavid Teigland */ 502b3b94faaSDavid Teigland 503faa31ce8SSteven Whitehouse static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer, 504b3b94faaSDavid Teigland unsigned int older) 505b3b94faaSDavid Teigland { 506b3b94faaSDavid Teigland int dist; 507b3b94faaSDavid Teigland 508b3b94faaSDavid Teigland dist = newer - older; 509b3b94faaSDavid Teigland if (dist < 0) 510b3b94faaSDavid Teigland dist += sdp->sd_jdesc->jd_blocks; 511b3b94faaSDavid Teigland 512b3b94faaSDavid Teigland return dist; 513b3b94faaSDavid Teigland } 514b3b94faaSDavid Teigland 5152332c443SRobert Peterson /** 5166188e877SAndreas Gruenbacher * calc_reserved - Calculate the number of blocks to keep reserved 5172332c443SRobert Peterson * @sdp: The GFS2 superblock 5182332c443SRobert Peterson * 5192332c443SRobert Peterson * This is complex. We need to reserve room for all our currently used 5206188e877SAndreas Gruenbacher * metadata blocks (e.g. normal file I/O rewriting file time stamps) and 5216188e877SAndreas Gruenbacher * all our journaled data blocks for journaled files (e.g. files in the 5222332c443SRobert Peterson * meta_fs like rindex, or files for which chattr +j was done.) 5236188e877SAndreas Gruenbacher * If we don't reserve enough space, corruption will follow. 5242332c443SRobert Peterson * 5256188e877SAndreas Gruenbacher * We can have metadata blocks and jdata blocks in the same journal. Each 5266188e877SAndreas Gruenbacher * type gets its own log descriptor, for which we need to reserve a block. 5276188e877SAndreas Gruenbacher * In fact, each type has the potential for needing more than one log descriptor 5286188e877SAndreas Gruenbacher * in cases where we have more blocks than will fit in a log descriptor. 5292332c443SRobert Peterson * Metadata journal entries take up half the space of journaled buffer entries. 5302332c443SRobert Peterson * 5312332c443SRobert Peterson * Also, we need to reserve blocks for revoke journal entries and one for an 5322332c443SRobert Peterson * overall header for the lot. 5332332c443SRobert Peterson * 5342332c443SRobert Peterson * Returns: the number of blocks reserved 5352332c443SRobert Peterson */ 5362332c443SRobert Peterson static unsigned int calc_reserved(struct gfs2_sbd *sdp) 5372332c443SRobert Peterson { 5382332c443SRobert Peterson unsigned int reserved = 0; 539022ef4feSSteven Whitehouse unsigned int mbuf; 540022ef4feSSteven Whitehouse unsigned int dbuf; 541022ef4feSSteven Whitehouse struct gfs2_trans *tr = sdp->sd_log_tr; 5422332c443SRobert Peterson 543022ef4feSSteven Whitehouse if (tr) { 544022ef4feSSteven Whitehouse mbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm; 545022ef4feSSteven Whitehouse dbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm; 546022ef4feSSteven Whitehouse reserved = mbuf + dbuf; 547022ef4feSSteven Whitehouse /* Account for header blocks */ 548022ef4feSSteven Whitehouse reserved += DIV_ROUND_UP(mbuf, buf_limit(sdp)); 549022ef4feSSteven Whitehouse reserved += DIV_ROUND_UP(dbuf, databuf_limit(sdp)); 550022ef4feSSteven Whitehouse } 5512332c443SRobert Peterson 5525d439758SAndreas Gruenbacher if (sdp->sd_log_committed_revoke > 0) 5535d439758SAndreas Gruenbacher reserved += gfs2_struct2blk(sdp, sdp->sd_log_committed_revoke); 5542332c443SRobert Peterson /* One for the overall header */ 5552332c443SRobert Peterson if (reserved) 5562332c443SRobert Peterson reserved++; 5572332c443SRobert Peterson return reserved; 5582332c443SRobert Peterson } 5592332c443SRobert Peterson 560b3b94faaSDavid Teigland static unsigned int current_tail(struct gfs2_sbd *sdp) 561b3b94faaSDavid Teigland { 56216ca9412SBenjamin Marzinski struct gfs2_trans *tr; 563b3b94faaSDavid Teigland unsigned int tail; 564b3b94faaSDavid Teigland 565d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 566b3b94faaSDavid Teigland 567faa31ce8SSteven Whitehouse if (list_empty(&sdp->sd_ail1_list)) { 568b3b94faaSDavid Teigland tail = sdp->sd_log_head; 569faa31ce8SSteven Whitehouse } else { 570969183bcSAndreas Gruenbacher tr = list_last_entry(&sdp->sd_ail1_list, struct gfs2_trans, 57116ca9412SBenjamin Marzinski tr_list); 57216ca9412SBenjamin Marzinski tail = tr->tr_first; 573b3b94faaSDavid Teigland } 574b3b94faaSDavid Teigland 575d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 576b3b94faaSDavid Teigland 577b3b94faaSDavid Teigland return tail; 578b3b94faaSDavid Teigland } 579b3b94faaSDavid Teigland 5802332c443SRobert Peterson static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail) 581b3b94faaSDavid Teigland { 582b3b94faaSDavid Teigland unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail); 583b3b94faaSDavid Teigland 584b3b94faaSDavid Teigland ail2_empty(sdp, new_tail); 585b3b94faaSDavid Teigland 586fd041f0bSSteven Whitehouse atomic_add(dist, &sdp->sd_log_blks_free); 58763997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, dist); 5885e687eacSBenjamin Marzinski gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= 5895e687eacSBenjamin Marzinski sdp->sd_jdesc->jd_blocks); 590b3b94faaSDavid Teigland 591b3b94faaSDavid Teigland sdp->sd_log_tail = new_tail; 592b3b94faaSDavid Teigland } 593b3b94faaSDavid Teigland 594b3b94faaSDavid Teigland 5959ff78289SBob Peterson void log_flush_wait(struct gfs2_sbd *sdp) 596b3b94faaSDavid Teigland { 59716615be1SSteven Whitehouse DEFINE_WAIT(wait); 598b3b94faaSDavid Teigland 59916615be1SSteven Whitehouse if (atomic_read(&sdp->sd_log_in_flight)) { 60016615be1SSteven Whitehouse do { 60116615be1SSteven Whitehouse prepare_to_wait(&sdp->sd_log_flush_wait, &wait, 60216615be1SSteven Whitehouse TASK_UNINTERRUPTIBLE); 60316615be1SSteven Whitehouse if (atomic_read(&sdp->sd_log_in_flight)) 60416615be1SSteven Whitehouse io_schedule(); 60516615be1SSteven Whitehouse } while(atomic_read(&sdp->sd_log_in_flight)); 60616615be1SSteven Whitehouse finish_wait(&sdp->sd_log_flush_wait, &wait); 607b3b94faaSDavid Teigland } 608b3b94faaSDavid Teigland } 609b3b94faaSDavid Teigland 61045138990SSteven Whitehouse static int ip_cmp(void *priv, struct list_head *a, struct list_head *b) 6114a36d08dSBob Peterson { 61245138990SSteven Whitehouse struct gfs2_inode *ipa, *ipb; 6134a36d08dSBob Peterson 61445138990SSteven Whitehouse ipa = list_entry(a, struct gfs2_inode, i_ordered); 61545138990SSteven Whitehouse ipb = list_entry(b, struct gfs2_inode, i_ordered); 6164a36d08dSBob Peterson 61745138990SSteven Whitehouse if (ipa->i_no_addr < ipb->i_no_addr) 6184a36d08dSBob Peterson return -1; 61945138990SSteven Whitehouse if (ipa->i_no_addr > ipb->i_no_addr) 6204a36d08dSBob Peterson return 1; 6214a36d08dSBob Peterson return 0; 6224a36d08dSBob Peterson } 6234a36d08dSBob Peterson 6247542486bSBob Peterson static void __ordered_del_inode(struct gfs2_inode *ip) 6257542486bSBob Peterson { 6267542486bSBob Peterson if (!list_empty(&ip->i_ordered)) 6277542486bSBob Peterson list_del_init(&ip->i_ordered); 6287542486bSBob Peterson } 6297542486bSBob Peterson 630d7b616e2SSteven Whitehouse static void gfs2_ordered_write(struct gfs2_sbd *sdp) 631d7b616e2SSteven Whitehouse { 63245138990SSteven Whitehouse struct gfs2_inode *ip; 633d7b616e2SSteven Whitehouse LIST_HEAD(written); 634d7b616e2SSteven Whitehouse 63545138990SSteven Whitehouse spin_lock(&sdp->sd_ordered_lock); 636a5b1d3fcSAndreas Gruenbacher list_sort(NULL, &sdp->sd_log_ordered, &ip_cmp); 637a5b1d3fcSAndreas Gruenbacher while (!list_empty(&sdp->sd_log_ordered)) { 638969183bcSAndreas Gruenbacher ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered); 6391f23bc78SAbhi Das if (ip->i_inode.i_mapping->nrpages == 0) { 6407542486bSBob Peterson __ordered_del_inode(ip); 641d7b616e2SSteven Whitehouse continue; 6421f23bc78SAbhi Das } 6431f23bc78SAbhi Das list_move(&ip->i_ordered, &written); 64445138990SSteven Whitehouse spin_unlock(&sdp->sd_ordered_lock); 64545138990SSteven Whitehouse filemap_fdatawrite(ip->i_inode.i_mapping); 64645138990SSteven Whitehouse spin_lock(&sdp->sd_ordered_lock); 647d7b616e2SSteven Whitehouse } 648a5b1d3fcSAndreas Gruenbacher list_splice(&written, &sdp->sd_log_ordered); 64945138990SSteven Whitehouse spin_unlock(&sdp->sd_ordered_lock); 650d7b616e2SSteven Whitehouse } 651d7b616e2SSteven Whitehouse 652d7b616e2SSteven Whitehouse static void gfs2_ordered_wait(struct gfs2_sbd *sdp) 653d7b616e2SSteven Whitehouse { 65445138990SSteven Whitehouse struct gfs2_inode *ip; 655d7b616e2SSteven Whitehouse 65645138990SSteven Whitehouse spin_lock(&sdp->sd_ordered_lock); 657a5b1d3fcSAndreas Gruenbacher while (!list_empty(&sdp->sd_log_ordered)) { 658969183bcSAndreas Gruenbacher ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered); 6597542486bSBob Peterson __ordered_del_inode(ip); 66045138990SSteven Whitehouse if (ip->i_inode.i_mapping->nrpages == 0) 661d7b616e2SSteven Whitehouse continue; 66245138990SSteven Whitehouse spin_unlock(&sdp->sd_ordered_lock); 66345138990SSteven Whitehouse filemap_fdatawait(ip->i_inode.i_mapping); 66445138990SSteven Whitehouse spin_lock(&sdp->sd_ordered_lock); 665d7b616e2SSteven Whitehouse } 66645138990SSteven Whitehouse spin_unlock(&sdp->sd_ordered_lock); 667d7b616e2SSteven Whitehouse } 66845138990SSteven Whitehouse 66945138990SSteven Whitehouse void gfs2_ordered_del_inode(struct gfs2_inode *ip) 67045138990SSteven Whitehouse { 67145138990SSteven Whitehouse struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 67245138990SSteven Whitehouse 67345138990SSteven Whitehouse spin_lock(&sdp->sd_ordered_lock); 6747542486bSBob Peterson __ordered_del_inode(ip); 67545138990SSteven Whitehouse spin_unlock(&sdp->sd_ordered_lock); 676d7b616e2SSteven Whitehouse } 677d7b616e2SSteven Whitehouse 6785d054964SBenjamin Marzinski void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd) 6795d054964SBenjamin Marzinski { 6805d054964SBenjamin Marzinski struct buffer_head *bh = bd->bd_bh; 6815d054964SBenjamin Marzinski struct gfs2_glock *gl = bd->bd_gl; 6825d054964SBenjamin Marzinski 683f4e2f5e1SAndreas Gruenbacher sdp->sd_log_num_revoke++; 684f4e2f5e1SAndreas Gruenbacher if (atomic_inc_return(&gl->gl_revokes) == 1) 685f4e2f5e1SAndreas Gruenbacher gfs2_glock_hold(gl); 6865d054964SBenjamin Marzinski bh->b_private = NULL; 6875d054964SBenjamin Marzinski bd->bd_blkno = bh->b_blocknr; 6889290a9a7SBob Peterson gfs2_remove_from_ail(bd); /* drops ref on bh */ 6899290a9a7SBob Peterson bd->bd_bh = NULL; 6905d054964SBenjamin Marzinski set_bit(GLF_LFLUSH, &gl->gl_flags); 691a5b1d3fcSAndreas Gruenbacher list_add(&bd->bd_list, &sdp->sd_log_revokes); 6925d054964SBenjamin Marzinski } 6935d054964SBenjamin Marzinski 694fe5e7ba1SBob Peterson void gfs2_glock_remove_revoke(struct gfs2_glock *gl) 695fe5e7ba1SBob Peterson { 696fe5e7ba1SBob Peterson if (atomic_dec_return(&gl->gl_revokes) == 0) { 697fe5e7ba1SBob Peterson clear_bit(GLF_LFLUSH, &gl->gl_flags); 698fe5e7ba1SBob Peterson gfs2_glock_queue_put(gl); 699fe5e7ba1SBob Peterson } 700fe5e7ba1SBob Peterson } 701fe5e7ba1SBob Peterson 7025e4c7632SBob Peterson /** 703e7501bf8SAndreas Gruenbacher * gfs2_flush_revokes - Add as many revokes to the system transaction as we can 7045e4c7632SBob Peterson * @sdp: The GFS2 superblock 7055e4c7632SBob Peterson * 7065e4c7632SBob Peterson * Our usual strategy is to defer writing revokes as much as we can in the hope 7075e4c7632SBob Peterson * that we'll eventually overwrite the journal, which will make those revokes 7085e4c7632SBob Peterson * go away. This changes when we flush the log: at that point, there will 7095e4c7632SBob Peterson * likely be some left-over space in the last revoke block of that transaction. 7105e4c7632SBob Peterson * We can fill that space with additional revokes for blocks that have already 7115e4c7632SBob Peterson * been written back. This will basically come at no cost now, and will save 7125e4c7632SBob Peterson * us from having to keep track of those blocks on the AIL2 list later. 7135e4c7632SBob Peterson */ 714e7501bf8SAndreas Gruenbacher void gfs2_flush_revokes(struct gfs2_sbd *sdp) 7155d054964SBenjamin Marzinski { 7165e4c7632SBob Peterson /* number of revokes we still have room for */ 7175a4e9c60SAndreas Gruenbacher unsigned int max_revokes; 7185d054964SBenjamin Marzinski 7195e4c7632SBob Peterson gfs2_log_lock(sdp); 7205a4e9c60SAndreas Gruenbacher max_revokes = sdp->sd_ldptrs; 7215a4e9c60SAndreas Gruenbacher if (sdp->sd_log_num_revoke > sdp->sd_ldptrs) 7225a4e9c60SAndreas Gruenbacher max_revokes += roundup(sdp->sd_log_num_revoke - sdp->sd_ldptrs, 7235a4e9c60SAndreas Gruenbacher sdp->sd_inptrs); 7245d054964SBenjamin Marzinski max_revokes -= sdp->sd_log_num_revoke; 7255d054964SBenjamin Marzinski if (!sdp->sd_log_num_revoke) { 7265d054964SBenjamin Marzinski atomic_dec(&sdp->sd_log_blks_free); 7275d054964SBenjamin Marzinski /* If no blocks have been reserved, we need to also 7285d054964SBenjamin Marzinski * reserve a block for the header */ 72977650bdbSBob Peterson if (!sdp->sd_log_blks_reserved) { 7305d054964SBenjamin Marzinski atomic_dec(&sdp->sd_log_blks_free); 73177650bdbSBob Peterson trace_gfs2_log_blocks(sdp, -2); 73277650bdbSBob Peterson } else { 73377650bdbSBob Peterson trace_gfs2_log_blocks(sdp, -1); 73477650bdbSBob Peterson } 7355d054964SBenjamin Marzinski } 7365e4c7632SBob Peterson gfs2_ail1_empty(sdp, max_revokes); 7375d054964SBenjamin Marzinski gfs2_log_unlock(sdp); 7385d054964SBenjamin Marzinski 7395d054964SBenjamin Marzinski if (!sdp->sd_log_num_revoke) { 7405d054964SBenjamin Marzinski atomic_inc(&sdp->sd_log_blks_free); 74177650bdbSBob Peterson if (!sdp->sd_log_blks_reserved) { 7425d054964SBenjamin Marzinski atomic_inc(&sdp->sd_log_blks_free); 74377650bdbSBob Peterson trace_gfs2_log_blocks(sdp, 2); 74477650bdbSBob Peterson } else { 74577650bdbSBob Peterson trace_gfs2_log_blocks(sdp, 1); 74677650bdbSBob Peterson } 7475d054964SBenjamin Marzinski } 7485d054964SBenjamin Marzinski } 7495d054964SBenjamin Marzinski 750b3b94faaSDavid Teigland /** 7517c70b896SBob Peterson * gfs2_write_log_header - Write a journal log header buffer at lblock 752588bff95SBob Peterson * @sdp: The GFS2 superblock 753c1696fb8SBob Peterson * @jd: journal descriptor of the journal to which we are writing 754588bff95SBob Peterson * @seq: sequence number 755588bff95SBob Peterson * @tail: tail of the log 7567c70b896SBob Peterson * @lblock: value for lh_blkno (block number relative to start of journal) 757c1696fb8SBob Peterson * @flags: log header flags GFS2_LOG_HEAD_* 758588bff95SBob Peterson * @op_flags: flags to pass to the bio 759588bff95SBob Peterson * 760588bff95SBob Peterson * Returns: the initialized log buffer descriptor 761588bff95SBob Peterson */ 762588bff95SBob Peterson 763c1696fb8SBob Peterson void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd, 7647c70b896SBob Peterson u64 seq, u32 tail, u32 lblock, u32 flags, 7657c70b896SBob Peterson int op_flags) 766588bff95SBob Peterson { 767588bff95SBob Peterson struct gfs2_log_header *lh; 768c1696fb8SBob Peterson u32 hash, crc; 769ade48088SBob Peterson struct page *page; 770c1696fb8SBob Peterson struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; 771c1696fb8SBob Peterson struct timespec64 tv; 772c1696fb8SBob Peterson struct super_block *sb = sdp->sd_vfs; 7737c70b896SBob Peterson u64 dblock; 774588bff95SBob Peterson 775ade48088SBob Peterson if (gfs2_withdrawn(sdp)) 776ade48088SBob Peterson goto out; 777ade48088SBob Peterson 778ade48088SBob Peterson page = mempool_alloc(gfs2_page_pool, GFP_NOIO); 779588bff95SBob Peterson lh = page_address(page); 780588bff95SBob Peterson clear_page(lh); 781588bff95SBob Peterson 782588bff95SBob Peterson lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC); 783588bff95SBob Peterson lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH); 784588bff95SBob Peterson lh->lh_header.__pad0 = cpu_to_be64(0); 785588bff95SBob Peterson lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH); 786588bff95SBob Peterson lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid); 787588bff95SBob Peterson lh->lh_sequence = cpu_to_be64(seq); 788588bff95SBob Peterson lh->lh_flags = cpu_to_be32(flags); 789588bff95SBob Peterson lh->lh_tail = cpu_to_be32(tail); 7907c70b896SBob Peterson lh->lh_blkno = cpu_to_be32(lblock); 791c1696fb8SBob Peterson hash = ~crc32(~0, lh, LH_V1_SIZE); 792588bff95SBob Peterson lh->lh_hash = cpu_to_be32(hash); 793588bff95SBob Peterson 794ee9c7f9aSArnd Bergmann ktime_get_coarse_real_ts64(&tv); 795c1696fb8SBob Peterson lh->lh_nsec = cpu_to_be32(tv.tv_nsec); 796c1696fb8SBob Peterson lh->lh_sec = cpu_to_be64(tv.tv_sec); 7977c70b896SBob Peterson if (!list_empty(&jd->extent_list)) 79819ebc050SAndreas Gruenbacher dblock = gfs2_log_bmap(jd, lblock); 7997c70b896SBob Peterson else { 8007c70b896SBob Peterson int ret = gfs2_lblk_to_dblk(jd->jd_inode, lblock, &dblock); 8017c70b896SBob Peterson if (gfs2_assert_withdraw(sdp, ret == 0)) 8027c70b896SBob Peterson return; 8037c70b896SBob Peterson } 8047c70b896SBob Peterson lh->lh_addr = cpu_to_be64(dblock); 805c1696fb8SBob Peterson lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr); 806c1696fb8SBob Peterson 807c1696fb8SBob Peterson /* We may only write local statfs, quota, etc., when writing to our 808c1696fb8SBob Peterson own journal. The values are left 0 when recovering a journal 809c1696fb8SBob Peterson different from our own. */ 810c1696fb8SBob Peterson if (!(flags & GFS2_LOG_HEAD_RECOVERY)) { 811c1696fb8SBob Peterson lh->lh_statfs_addr = 812c1696fb8SBob Peterson cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr); 813c1696fb8SBob Peterson lh->lh_quota_addr = 814c1696fb8SBob Peterson cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr); 815c1696fb8SBob Peterson 816c1696fb8SBob Peterson spin_lock(&sdp->sd_statfs_spin); 817c1696fb8SBob Peterson lh->lh_local_total = cpu_to_be64(l_sc->sc_total); 818c1696fb8SBob Peterson lh->lh_local_free = cpu_to_be64(l_sc->sc_free); 819c1696fb8SBob Peterson lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes); 820c1696fb8SBob Peterson spin_unlock(&sdp->sd_statfs_spin); 821c1696fb8SBob Peterson } 822c1696fb8SBob Peterson 823c1696fb8SBob Peterson BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE); 824c1696fb8SBob Peterson 825c1696fb8SBob Peterson crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4, 826c1696fb8SBob Peterson sb->s_blocksize - LH_V1_SIZE - 4); 827c1696fb8SBob Peterson lh->lh_crc = cpu_to_be32(crc); 828c1696fb8SBob Peterson 8297c70b896SBob Peterson gfs2_log_write(sdp, page, sb->s_blocksize, 0, dblock); 830f4686c26SAbhi Das gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE | op_flags); 831ade48088SBob Peterson out: 832588bff95SBob Peterson log_flush_wait(sdp); 833588bff95SBob Peterson } 834588bff95SBob Peterson 835588bff95SBob Peterson /** 83634cc1781SSteven Whitehouse * log_write_header - Get and initialize a journal header buffer 83734cc1781SSteven Whitehouse * @sdp: The GFS2 superblock 838c1696fb8SBob Peterson * @flags: The log header flags, including log header origin 83934cc1781SSteven Whitehouse * 84034cc1781SSteven Whitehouse * Returns: the initialized log buffer descriptor 84134cc1781SSteven Whitehouse */ 84234cc1781SSteven Whitehouse 843fdb76a42SSteven Whitehouse static void log_write_header(struct gfs2_sbd *sdp, u32 flags) 84434cc1781SSteven Whitehouse { 84534cc1781SSteven Whitehouse unsigned int tail; 8460f0b9b63SJan Kara int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC; 8472e60d768SBenjamin Marzinski enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state); 84834cc1781SSteven Whitehouse 8492e60d768SBenjamin Marzinski gfs2_assert_withdraw(sdp, (state != SFS_FROZEN)); 85034cc1781SSteven Whitehouse tail = current_tail(sdp); 85134cc1781SSteven Whitehouse 85234cc1781SSteven Whitehouse if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) { 85334cc1781SSteven Whitehouse gfs2_ordered_wait(sdp); 85434cc1781SSteven Whitehouse log_flush_wait(sdp); 85570fd7614SChristoph Hellwig op_flags = REQ_SYNC | REQ_META | REQ_PRIO; 85634cc1781SSteven Whitehouse } 857e8c92ed7SSteven Whitehouse sdp->sd_log_idle = (tail == sdp->sd_log_flush_head); 858c1696fb8SBob Peterson gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++, tail, 8597c70b896SBob Peterson sdp->sd_log_flush_head, flags, op_flags); 86019ebc050SAndreas Gruenbacher gfs2_log_incr_head(sdp); 86134cc1781SSteven Whitehouse 86234cc1781SSteven Whitehouse if (sdp->sd_log_tail != tail) 86334cc1781SSteven Whitehouse log_pull_tail(sdp, tail); 86434cc1781SSteven Whitehouse } 86534cc1781SSteven Whitehouse 86634cc1781SSteven Whitehouse /** 8672ca0c2fbSBob Peterson * ail_drain - drain the ail lists after a withdraw 8682ca0c2fbSBob Peterson * @sdp: Pointer to GFS2 superblock 8692ca0c2fbSBob Peterson */ 8702ca0c2fbSBob Peterson static void ail_drain(struct gfs2_sbd *sdp) 8712ca0c2fbSBob Peterson { 8722ca0c2fbSBob Peterson struct gfs2_trans *tr; 8732ca0c2fbSBob Peterson 8742ca0c2fbSBob Peterson spin_lock(&sdp->sd_ail_lock); 8752ca0c2fbSBob Peterson /* 8762ca0c2fbSBob Peterson * For transactions on the sd_ail1_list we need to drain both the 8772ca0c2fbSBob Peterson * ail1 and ail2 lists. That's because function gfs2_ail1_start_one 8782ca0c2fbSBob Peterson * (temporarily) moves items from its tr_ail1 list to tr_ail2 list 8792ca0c2fbSBob Peterson * before revokes are sent for that block. Items on the sd_ail2_list 8802ca0c2fbSBob Peterson * should have already gotten beyond that point, so no need. 8812ca0c2fbSBob Peterson */ 8822ca0c2fbSBob Peterson while (!list_empty(&sdp->sd_ail1_list)) { 8832ca0c2fbSBob Peterson tr = list_first_entry(&sdp->sd_ail1_list, struct gfs2_trans, 8842ca0c2fbSBob Peterson tr_list); 8852ca0c2fbSBob Peterson gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail1_list); 8862ca0c2fbSBob Peterson gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list); 8872ca0c2fbSBob Peterson list_del(&tr->tr_list); 888b839dadaSBob Peterson gfs2_trans_free(sdp, tr); 8892ca0c2fbSBob Peterson } 8902ca0c2fbSBob Peterson while (!list_empty(&sdp->sd_ail2_list)) { 8912ca0c2fbSBob Peterson tr = list_first_entry(&sdp->sd_ail2_list, struct gfs2_trans, 8922ca0c2fbSBob Peterson tr_list); 8932ca0c2fbSBob Peterson gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list); 8942ca0c2fbSBob Peterson list_del(&tr->tr_list); 895b839dadaSBob Peterson gfs2_trans_free(sdp, tr); 8962ca0c2fbSBob Peterson } 8972ca0c2fbSBob Peterson spin_unlock(&sdp->sd_ail_lock); 8982ca0c2fbSBob Peterson } 8992ca0c2fbSBob Peterson 9002ca0c2fbSBob Peterson /** 901d5dc3d96SBob Peterson * empty_ail1_list - try to start IO and empty the ail1 list 902d5dc3d96SBob Peterson * @sdp: Pointer to GFS2 superblock 903d5dc3d96SBob Peterson */ 904d5dc3d96SBob Peterson static void empty_ail1_list(struct gfs2_sbd *sdp) 905d5dc3d96SBob Peterson { 906d5dc3d96SBob Peterson unsigned long start = jiffies; 907d5dc3d96SBob Peterson 908d5dc3d96SBob Peterson for (;;) { 909d5dc3d96SBob Peterson if (time_after(jiffies, start + (HZ * 600))) { 910d5dc3d96SBob Peterson fs_err(sdp, "Error: In %s for 10 minutes! t=%d\n", 911d5dc3d96SBob Peterson __func__, current->journal_info ? 1 : 0); 912d5dc3d96SBob Peterson dump_ail_list(sdp); 913d5dc3d96SBob Peterson return; 914d5dc3d96SBob Peterson } 915d5dc3d96SBob Peterson gfs2_ail1_start(sdp); 916d5dc3d96SBob Peterson gfs2_ail1_wait(sdp); 917d5dc3d96SBob Peterson if (gfs2_ail1_empty(sdp, 0)) 918d5dc3d96SBob Peterson return; 919d5dc3d96SBob Peterson } 920d5dc3d96SBob Peterson } 921d5dc3d96SBob Peterson 922d5dc3d96SBob Peterson /** 923521031faSBob Peterson * trans_drain - drain the buf and databuf queue for a failed transaction 924462582b9SBob Peterson * @tr: the transaction to drain 925462582b9SBob Peterson * 926462582b9SBob Peterson * When this is called, we're taking an error exit for a log write that failed 927462582b9SBob Peterson * but since we bypassed the after_commit functions, we need to remove the 928462582b9SBob Peterson * items from the buf and databuf queue. 929462582b9SBob Peterson */ 930462582b9SBob Peterson static void trans_drain(struct gfs2_trans *tr) 931462582b9SBob Peterson { 932462582b9SBob Peterson struct gfs2_bufdata *bd; 933462582b9SBob Peterson struct list_head *head; 934462582b9SBob Peterson 935462582b9SBob Peterson if (!tr) 936462582b9SBob Peterson return; 937462582b9SBob Peterson 938462582b9SBob Peterson head = &tr->tr_buf; 939462582b9SBob Peterson while (!list_empty(head)) { 940462582b9SBob Peterson bd = list_first_entry(head, struct gfs2_bufdata, bd_list); 941462582b9SBob Peterson list_del_init(&bd->bd_list); 942462582b9SBob Peterson kmem_cache_free(gfs2_bufdata_cachep, bd); 943462582b9SBob Peterson } 944462582b9SBob Peterson head = &tr->tr_databuf; 945462582b9SBob Peterson while (!list_empty(head)) { 946462582b9SBob Peterson bd = list_first_entry(head, struct gfs2_bufdata, bd_list); 947462582b9SBob Peterson list_del_init(&bd->bd_list); 948462582b9SBob Peterson kmem_cache_free(gfs2_bufdata_cachep, bd); 949462582b9SBob Peterson } 950462582b9SBob Peterson } 951462582b9SBob Peterson 952462582b9SBob Peterson /** 953b09e593dSSteven Whitehouse * gfs2_log_flush - flush incore transaction(s) 954b3b94faaSDavid Teigland * @sdp: the filesystem 955b3b94faaSDavid Teigland * @gl: The glock structure to flush. If NULL, flush the whole incore log 956805c0907SBob Peterson * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags 957b3b94faaSDavid Teigland * 958b3b94faaSDavid Teigland */ 959b3b94faaSDavid Teigland 960c1696fb8SBob Peterson void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags) 961b3b94faaSDavid Teigland { 9622ca0c2fbSBob Peterson struct gfs2_trans *tr = NULL; 9632e60d768SBenjamin Marzinski enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state); 964b3b94faaSDavid Teigland 965484adff8SSteven Whitehouse down_write(&sdp->sd_log_flush_lock); 966f55ab26aSSteven Whitehouse 9672ca0c2fbSBob Peterson /* 9682ca0c2fbSBob Peterson * Do this check while holding the log_flush_lock to prevent new 9692ca0c2fbSBob Peterson * buffers from being added to the ail via gfs2_pin() 9702ca0c2fbSBob Peterson */ 9712ca0c2fbSBob Peterson if (gfs2_withdrawn(sdp)) 9722ca0c2fbSBob Peterson goto out; 9732ca0c2fbSBob Peterson 9742bcd610dSSteven Whitehouse /* Log might have been flushed while we waited for the flush lock */ 9755a61ae14SAndreas Gruenbacher if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) 9765a61ae14SAndreas Gruenbacher goto out; 977805c0907SBob Peterson trace_gfs2_log_flush(sdp, 1, flags); 978f55ab26aSSteven Whitehouse 979c1696fb8SBob Peterson if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN) 980400ac52eSBenjamin Marzinski clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags); 981400ac52eSBenjamin Marzinski 982b1ab1e44SSteven Whitehouse sdp->sd_log_flush_head = sdp->sd_log_head; 98316ca9412SBenjamin Marzinski tr = sdp->sd_log_tr; 98416ca9412SBenjamin Marzinski if (tr) { 98516ca9412SBenjamin Marzinski sdp->sd_log_tr = NULL; 986b1ab1e44SSteven Whitehouse tr->tr_first = sdp->sd_log_flush_head; 9872e60d768SBenjamin Marzinski if (unlikely (state == SFS_FROZEN)) 988ca399c96SBob Peterson if (gfs2_assert_withdraw_delayed(sdp, 989ca399c96SBob Peterson !tr->tr_num_buf_new && !tr->tr_num_databuf_new)) 9905a61ae14SAndreas Gruenbacher goto out_withdraw; 99116ca9412SBenjamin Marzinski } 992b3b94faaSDavid Teigland 9932e60d768SBenjamin Marzinski if (unlikely(state == SFS_FROZEN)) 994ca399c96SBob Peterson if (gfs2_assert_withdraw_delayed(sdp, !sdp->sd_log_num_revoke)) 9955a61ae14SAndreas Gruenbacher goto out_withdraw; 996ca399c96SBob Peterson if (gfs2_assert_withdraw_delayed(sdp, 997ca399c96SBob Peterson sdp->sd_log_num_revoke == sdp->sd_log_committed_revoke)) 9985a61ae14SAndreas Gruenbacher goto out_withdraw; 999b3b94faaSDavid Teigland 1000d7b616e2SSteven Whitehouse gfs2_ordered_write(sdp); 10012ca0c2fbSBob Peterson if (gfs2_withdrawn(sdp)) 10025a61ae14SAndreas Gruenbacher goto out_withdraw; 1003d69a3c65SSteven Whitehouse lops_before_commit(sdp, tr); 10042ca0c2fbSBob Peterson if (gfs2_withdrawn(sdp)) 10055a61ae14SAndreas Gruenbacher goto out_withdraw; 1006f4686c26SAbhi Das gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE); 10072ca0c2fbSBob Peterson if (gfs2_withdrawn(sdp)) 10085a61ae14SAndreas Gruenbacher goto out_withdraw; 1009d7b616e2SSteven Whitehouse 101034cc1781SSteven Whitehouse if (sdp->sd_log_head != sdp->sd_log_flush_head) { 1011428fd95dSBob Peterson log_flush_wait(sdp); 1012c1696fb8SBob Peterson log_write_header(sdp, flags); 101334cc1781SSteven Whitehouse } else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle) { 1014fd041f0bSSteven Whitehouse atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */ 101563997775SSteven Whitehouse trace_gfs2_log_blocks(sdp, -1); 1016c1696fb8SBob Peterson log_write_header(sdp, flags); 10172332c443SRobert Peterson } 10182ca0c2fbSBob Peterson if (gfs2_withdrawn(sdp)) 10195a61ae14SAndreas Gruenbacher goto out_withdraw; 102016ca9412SBenjamin Marzinski lops_after_commit(sdp, tr); 1021fe1a698fSSteven Whitehouse 1022fe1a698fSSteven Whitehouse gfs2_log_lock(sdp); 1023b3b94faaSDavid Teigland sdp->sd_log_head = sdp->sd_log_flush_head; 1024faa31ce8SSteven Whitehouse sdp->sd_log_blks_reserved = 0; 10255d439758SAndreas Gruenbacher sdp->sd_log_committed_revoke = 0; 1026b3b94faaSDavid Teigland 1027d6a079e8SDave Chinner spin_lock(&sdp->sd_ail_lock); 102816ca9412SBenjamin Marzinski if (tr && !list_empty(&tr->tr_ail1_list)) { 102916ca9412SBenjamin Marzinski list_add(&tr->tr_list, &sdp->sd_ail1_list); 103016ca9412SBenjamin Marzinski tr = NULL; 1031b3b94faaSDavid Teigland } 1032d6a079e8SDave Chinner spin_unlock(&sdp->sd_ail_lock); 1033b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 103424972557SBenjamin Marzinski 1035c1696fb8SBob Peterson if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) { 103624972557SBenjamin Marzinski if (!sdp->sd_log_idle) { 1037d5dc3d96SBob Peterson empty_ail1_list(sdp); 103830fe70a8SBob Peterson if (gfs2_withdrawn(sdp)) 10395a61ae14SAndreas Gruenbacher goto out_withdraw; 104024972557SBenjamin Marzinski atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */ 104124972557SBenjamin Marzinski trace_gfs2_log_blocks(sdp, -1); 1042c1696fb8SBob Peterson log_write_header(sdp, flags); 104324972557SBenjamin Marzinski sdp->sd_log_head = sdp->sd_log_flush_head; 104424972557SBenjamin Marzinski } 1045c1696fb8SBob Peterson if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN | 1046c1696fb8SBob Peterson GFS2_LOG_HEAD_FLUSH_FREEZE)) 104724972557SBenjamin Marzinski gfs2_log_shutdown(sdp); 1048c1696fb8SBob Peterson if (flags & GFS2_LOG_HEAD_FLUSH_FREEZE) 10492e60d768SBenjamin Marzinski atomic_set(&sdp->sd_freeze_state, SFS_FROZEN); 105024972557SBenjamin Marzinski } 105124972557SBenjamin Marzinski 10525a61ae14SAndreas Gruenbacher out_end: 10535a61ae14SAndreas Gruenbacher trace_gfs2_log_flush(sdp, 0, flags); 105430fe70a8SBob Peterson out: 10555a61ae14SAndreas Gruenbacher up_write(&sdp->sd_log_flush_lock); 10565a61ae14SAndreas Gruenbacher gfs2_trans_free(sdp, tr); 10575a61ae14SAndreas Gruenbacher if (gfs2_withdrawing(sdp)) 10585a61ae14SAndreas Gruenbacher gfs2_withdraw(sdp); 10595a61ae14SAndreas Gruenbacher return; 10605a61ae14SAndreas Gruenbacher 10615a61ae14SAndreas Gruenbacher out_withdraw: 1062462582b9SBob Peterson trans_drain(tr); 106358e08e8dSBob Peterson /** 106458e08e8dSBob Peterson * If the tr_list is empty, we're withdrawing during a log 106558e08e8dSBob Peterson * flush that targets a transaction, but the transaction was 106658e08e8dSBob Peterson * never queued onto any of the ail lists. Here we add it to 106758e08e8dSBob Peterson * ail1 just so that ail_drain() will find and free it. 106858e08e8dSBob Peterson */ 106958e08e8dSBob Peterson spin_lock(&sdp->sd_ail_lock); 107058e08e8dSBob Peterson if (tr && list_empty(&tr->tr_list)) 107158e08e8dSBob Peterson list_add(&tr->tr_list, &sdp->sd_ail1_list); 107258e08e8dSBob Peterson spin_unlock(&sdp->sd_ail_lock); 10732ca0c2fbSBob Peterson ail_drain(sdp); /* frees all transactions */ 10742ca0c2fbSBob Peterson tr = NULL; 10755a61ae14SAndreas Gruenbacher goto out_end; 1076b3b94faaSDavid Teigland } 1077b3b94faaSDavid Teigland 1078d69a3c65SSteven Whitehouse /** 1079d69a3c65SSteven Whitehouse * gfs2_merge_trans - Merge a new transaction into a cached transaction 1080d69a3c65SSteven Whitehouse * @old: Original transaction to be expanded 1081d69a3c65SSteven Whitehouse * @new: New transaction to be merged 1082d69a3c65SSteven Whitehouse */ 1083d69a3c65SSteven Whitehouse 108483d060caSBob Peterson static void gfs2_merge_trans(struct gfs2_sbd *sdp, struct gfs2_trans *new) 1085d69a3c65SSteven Whitehouse { 108683d060caSBob Peterson struct gfs2_trans *old = sdp->sd_log_tr; 108783d060caSBob Peterson 10889862ca05SBob Peterson WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags)); 1089d69a3c65SSteven Whitehouse 1090d69a3c65SSteven Whitehouse old->tr_num_buf_new += new->tr_num_buf_new; 1091d69a3c65SSteven Whitehouse old->tr_num_databuf_new += new->tr_num_databuf_new; 1092d69a3c65SSteven Whitehouse old->tr_num_buf_rm += new->tr_num_buf_rm; 1093d69a3c65SSteven Whitehouse old->tr_num_databuf_rm += new->tr_num_databuf_rm; 1094d69a3c65SSteven Whitehouse old->tr_num_revoke += new->tr_num_revoke; 1095a31b4ec5SBob Peterson old->tr_num_revoke_rm += new->tr_num_revoke_rm; 1096d69a3c65SSteven Whitehouse 1097d69a3c65SSteven Whitehouse list_splice_tail_init(&new->tr_databuf, &old->tr_databuf); 1098d69a3c65SSteven Whitehouse list_splice_tail_init(&new->tr_buf, &old->tr_buf); 109983d060caSBob Peterson 110083d060caSBob Peterson spin_lock(&sdp->sd_ail_lock); 110183d060caSBob Peterson list_splice_tail_init(&new->tr_ail1_list, &old->tr_ail1_list); 110283d060caSBob Peterson list_splice_tail_init(&new->tr_ail2_list, &old->tr_ail2_list); 110383d060caSBob Peterson spin_unlock(&sdp->sd_ail_lock); 1104d69a3c65SSteven Whitehouse } 1105d69a3c65SSteven Whitehouse 1106b3b94faaSDavid Teigland static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 1107b3b94faaSDavid Teigland { 11082332c443SRobert Peterson unsigned int reserved; 1109ac39aaddSSteven Whitehouse unsigned int unused; 1110022ef4feSSteven Whitehouse unsigned int maxres; 1111b3b94faaSDavid Teigland 1112b3b94faaSDavid Teigland gfs2_log_lock(sdp); 1113b3b94faaSDavid Teigland 1114d69a3c65SSteven Whitehouse if (sdp->sd_log_tr) { 111583d060caSBob Peterson gfs2_merge_trans(sdp, tr); 1116d69a3c65SSteven Whitehouse } else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) { 1117*c968f578SAndreas Gruenbacher gfs2_assert_withdraw(sdp, !test_bit(TR_ONSTACK, &tr->tr_flags)); 111816ca9412SBenjamin Marzinski sdp->sd_log_tr = tr; 11199862ca05SBob Peterson set_bit(TR_ATTACHED, &tr->tr_flags); 112016ca9412SBenjamin Marzinski } 1121022ef4feSSteven Whitehouse 1122a31b4ec5SBob Peterson sdp->sd_log_committed_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm; 1123022ef4feSSteven Whitehouse reserved = calc_reserved(sdp); 1124022ef4feSSteven Whitehouse maxres = sdp->sd_log_blks_reserved + tr->tr_reserved; 1125022ef4feSSteven Whitehouse gfs2_assert_withdraw(sdp, maxres >= reserved); 1126022ef4feSSteven Whitehouse unused = maxres - reserved; 1127022ef4feSSteven Whitehouse atomic_add(unused, &sdp->sd_log_blks_free); 1128022ef4feSSteven Whitehouse trace_gfs2_log_blocks(sdp, unused); 1129022ef4feSSteven Whitehouse gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= 1130022ef4feSSteven Whitehouse sdp->sd_jdesc->jd_blocks); 1131022ef4feSSteven Whitehouse sdp->sd_log_blks_reserved = reserved; 1132022ef4feSSteven Whitehouse 1133b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 1134b3b94faaSDavid Teigland } 1135b3b94faaSDavid Teigland 1136b3b94faaSDavid Teigland /** 1137b3b94faaSDavid Teigland * gfs2_log_commit - Commit a transaction to the log 1138b3b94faaSDavid Teigland * @sdp: the filesystem 1139b3b94faaSDavid Teigland * @tr: the transaction 1140b3b94faaSDavid Teigland * 11415e687eacSBenjamin Marzinski * We wake up gfs2_logd if the number of pinned blocks exceed thresh1 11425e687eacSBenjamin Marzinski * or the total number of used blocks (pinned blocks plus AIL blocks) 11435e687eacSBenjamin Marzinski * is greater than thresh2. 11445e687eacSBenjamin Marzinski * 1145b57bc0fbSBob Peterson * At mount time thresh1 is 2/5ths of journal size, thresh2 is 4/5ths of 11465e687eacSBenjamin Marzinski * journal size. 11475e687eacSBenjamin Marzinski * 1148b3b94faaSDavid Teigland * Returns: errno 1149b3b94faaSDavid Teigland */ 1150b3b94faaSDavid Teigland 1151b3b94faaSDavid Teigland void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 1152b3b94faaSDavid Teigland { 1153b3b94faaSDavid Teigland log_refund(sdp, tr); 1154b3b94faaSDavid Teigland 11555e687eacSBenjamin Marzinski if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) || 11565e687eacSBenjamin Marzinski ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) > 11575e687eacSBenjamin Marzinski atomic_read(&sdp->sd_log_thresh2))) 11585e687eacSBenjamin Marzinski wake_up(&sdp->sd_logd_waitq); 1159faa31ce8SSteven Whitehouse } 1160b3b94faaSDavid Teigland 1161b3b94faaSDavid Teigland /** 1162b3b94faaSDavid Teigland * gfs2_log_shutdown - write a shutdown header into a journal 1163b3b94faaSDavid Teigland * @sdp: the filesystem 1164b3b94faaSDavid Teigland * 1165b3b94faaSDavid Teigland */ 1166b3b94faaSDavid Teigland 1167feed98a8SBob Peterson static void gfs2_log_shutdown(struct gfs2_sbd *sdp) 1168b3b94faaSDavid Teigland { 1169b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved); 1170b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); 1171b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list)); 1172b3b94faaSDavid Teigland 1173b3b94faaSDavid Teigland sdp->sd_log_flush_head = sdp->sd_log_head; 1174b3b94faaSDavid Teigland 1175805c0907SBob Peterson log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN); 1176b3b94faaSDavid Teigland 1177a74604beSSteven Whitehouse gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail); 1178a74604beSSteven Whitehouse gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list)); 1179b3b94faaSDavid Teigland 1180b3b94faaSDavid Teigland sdp->sd_log_head = sdp->sd_log_flush_head; 1181b3b94faaSDavid Teigland sdp->sd_log_tail = sdp->sd_log_head; 1182a25311c8SSteven Whitehouse } 1183a25311c8SSteven Whitehouse 11845e687eacSBenjamin Marzinski static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp) 11855e687eacSBenjamin Marzinski { 1186f07b3520SBob Peterson return (atomic_read(&sdp->sd_log_pinned) + 1187f07b3520SBob Peterson atomic_read(&sdp->sd_log_blks_needed) >= 1188f07b3520SBob Peterson atomic_read(&sdp->sd_log_thresh1)); 11895e687eacSBenjamin Marzinski } 11905e687eacSBenjamin Marzinski 11915e687eacSBenjamin Marzinski static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp) 11925e687eacSBenjamin Marzinski { 11935e687eacSBenjamin Marzinski unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free); 1194b066a4eeSAbhi Das 1195b066a4eeSAbhi Das if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags)) 1196b066a4eeSAbhi Das return 1; 1197b066a4eeSAbhi Das 1198f07b3520SBob Peterson return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >= 1199f07b3520SBob Peterson atomic_read(&sdp->sd_log_thresh2); 12005e687eacSBenjamin Marzinski } 1201ec69b188SSteven Whitehouse 1202ec69b188SSteven Whitehouse /** 1203ec69b188SSteven Whitehouse * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks 1204ec69b188SSteven Whitehouse * @sdp: Pointer to GFS2 superblock 1205ec69b188SSteven Whitehouse * 1206ec69b188SSteven Whitehouse * Also, periodically check to make sure that we're using the most recent 1207ec69b188SSteven Whitehouse * journal index. 1208ec69b188SSteven Whitehouse */ 1209ec69b188SSteven Whitehouse 1210ec69b188SSteven Whitehouse int gfs2_logd(void *data) 1211ec69b188SSteven Whitehouse { 1212ec69b188SSteven Whitehouse struct gfs2_sbd *sdp = data; 12135e687eacSBenjamin Marzinski unsigned long t = 1; 12145e687eacSBenjamin Marzinski DEFINE_WAIT(wait); 1215b63f5e84SBob Peterson bool did_flush; 1216ec69b188SSteven Whitehouse 1217ec69b188SSteven Whitehouse while (!kthread_should_stop()) { 1218ec69b188SSteven Whitehouse 1219d22f69a0SBob Peterson if (gfs2_withdrawn(sdp)) { 1220d22f69a0SBob Peterson msleep_interruptible(HZ); 1221d22f69a0SBob Peterson continue; 1222d22f69a0SBob Peterson } 1223942b0cddSBob Peterson /* Check for errors writing to the journal */ 1224942b0cddSBob Peterson if (sdp->sd_log_error) { 1225badb55ecSAndreas Gruenbacher gfs2_lm(sdp, 1226942b0cddSBob Peterson "GFS2: fsid=%s: error %d: " 1227942b0cddSBob Peterson "withdrawing the file system to " 1228942b0cddSBob Peterson "prevent further damage.\n", 1229942b0cddSBob Peterson sdp->sd_fsname, sdp->sd_log_error); 1230badb55ecSAndreas Gruenbacher gfs2_withdraw(sdp); 1231d22f69a0SBob Peterson continue; 1232942b0cddSBob Peterson } 1233942b0cddSBob Peterson 1234b63f5e84SBob Peterson did_flush = false; 12355e687eacSBenjamin Marzinski if (gfs2_jrnl_flush_reqd(sdp) || t == 0) { 12365e4c7632SBob Peterson gfs2_ail1_empty(sdp, 0); 1237805c0907SBob Peterson gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL | 1238805c0907SBob Peterson GFS2_LFC_LOGD_JFLUSH_REQD); 1239b63f5e84SBob Peterson did_flush = true; 1240ec69b188SSteven Whitehouse } 1241ec69b188SSteven Whitehouse 12425e687eacSBenjamin Marzinski if (gfs2_ail_flush_reqd(sdp)) { 12435e687eacSBenjamin Marzinski gfs2_ail1_start(sdp); 124426b06a69SSteven Whitehouse gfs2_ail1_wait(sdp); 12455e4c7632SBob Peterson gfs2_ail1_empty(sdp, 0); 1246805c0907SBob Peterson gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL | 1247805c0907SBob Peterson GFS2_LFC_LOGD_AIL_FLUSH_REQD); 1248b63f5e84SBob Peterson did_flush = true; 12495e687eacSBenjamin Marzinski } 12505e687eacSBenjamin Marzinski 1251b63f5e84SBob Peterson if (!gfs2_ail_flush_reqd(sdp) || did_flush) 12525e687eacSBenjamin Marzinski wake_up(&sdp->sd_log_waitq); 125326b06a69SSteven Whitehouse 1254ec69b188SSteven Whitehouse t = gfs2_tune_get(sdp, gt_logd_secs) * HZ; 1255a0acae0eSTejun Heo 1256a0acae0eSTejun Heo try_to_freeze(); 12575e687eacSBenjamin Marzinski 12585e687eacSBenjamin Marzinski do { 12595e687eacSBenjamin Marzinski prepare_to_wait(&sdp->sd_logd_waitq, &wait, 12605f487490SSteven Whitehouse TASK_INTERRUPTIBLE); 12615e687eacSBenjamin Marzinski if (!gfs2_ail_flush_reqd(sdp) && 12625e687eacSBenjamin Marzinski !gfs2_jrnl_flush_reqd(sdp) && 12635e687eacSBenjamin Marzinski !kthread_should_stop()) 12645e687eacSBenjamin Marzinski t = schedule_timeout(t); 12655e687eacSBenjamin Marzinski } while(t && !gfs2_ail_flush_reqd(sdp) && 12665e687eacSBenjamin Marzinski !gfs2_jrnl_flush_reqd(sdp) && 12675e687eacSBenjamin Marzinski !kthread_should_stop()); 12685e687eacSBenjamin Marzinski finish_wait(&sdp->sd_logd_waitq, &wait); 1269ec69b188SSteven Whitehouse } 1270ec69b188SSteven Whitehouse 1271ec69b188SSteven Whitehouse return 0; 1272ec69b188SSteven Whitehouse } 1273ec69b188SSteven Whitehouse 1274