Thomas Gleixner | 7336d0e | 2019-05-31 01:09:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Bob Peterson | da6dd40 | 2007-12-11 18:49:21 -0600 | [diff] [blame] | 4 | * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/sched.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/completion.h> |
| 11 | #include <linux/buffer_head.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 12 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 13 | #include <linux/crc32.h> |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 14 | #include <linux/crc32c.h> |
Steven Whitehouse | a25311c | 2006-11-23 11:06:35 -0500 | [diff] [blame] | 15 | #include <linux/delay.h> |
Steven Whitehouse | ec69b18 | 2007-11-09 10:01:41 +0000 | [diff] [blame] | 16 | #include <linux/kthread.h> |
| 17 | #include <linux/freezer.h> |
Steven Whitehouse | 254db57 | 2008-09-26 10:23:22 +0100 | [diff] [blame] | 18 | #include <linux/bio.h> |
Steven Whitehouse | 885bcec | 2014-02-03 09:57:29 +0000 | [diff] [blame] | 19 | #include <linux/blkdev.h> |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 20 | #include <linux/writeback.h> |
Bob Peterson | 4a36d08 | 2012-02-14 14:49:57 -0500 | [diff] [blame] | 21 | #include <linux/list_sort.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | |
| 23 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 24 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 25 | #include "bmap.h" |
| 26 | #include "glock.h" |
| 27 | #include "log.h" |
| 28 | #include "lops.h" |
| 29 | #include "meta_io.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 30 | #include "util.h" |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 31 | #include "dir.h" |
Steven Whitehouse | 6399777 | 2009-06-12 08:49:20 +0100 | [diff] [blame] | 32 | #include "trace_gfs2.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 33 | |
Bob Peterson | feed98a | 2019-11-14 09:48:26 -0500 | [diff] [blame] | 34 | static void gfs2_log_shutdown(struct gfs2_sbd *sdp); |
| 35 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 36 | /** |
| 37 | * gfs2_struct2blk - compute stuff |
| 38 | * @sdp: the filesystem |
| 39 | * @nstruct: the number of structures |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 40 | * |
| 41 | * Compute the number of log descriptor blocks needed to hold a certain number |
| 42 | * of structures of a certain size. |
| 43 | * |
| 44 | * Returns: the number of blocks needed (minimum is always 1) |
| 45 | */ |
| 46 | |
Bob Peterson | 2e9eeaa | 2019-12-13 08:10:51 -0600 | [diff] [blame] | 47 | unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 48 | { |
| 49 | unsigned int blks; |
| 50 | unsigned int first, second; |
| 51 | |
| 52 | blks = 1; |
Bob Peterson | 2e9eeaa | 2019-12-13 08:10:51 -0600 | [diff] [blame] | 53 | first = sdp->sd_ldptrs; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 54 | |
| 55 | if (nstruct > first) { |
Bob Peterson | 2e9eeaa | 2019-12-13 08:10:51 -0600 | [diff] [blame] | 56 | second = sdp->sd_inptrs; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 57 | blks += DIV_ROUND_UP(nstruct - first, second); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | return blks; |
| 61 | } |
| 62 | |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 63 | /** |
Steven Whitehouse | 1e1a3d0 | 2007-08-27 09:45:26 +0100 | [diff] [blame] | 64 | * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters |
| 65 | * @mapping: The associated mapping (maybe NULL) |
| 66 | * @bd: The gfs2_bufdata to remove |
| 67 | * |
Steven Whitehouse | c618e87 | 2011-03-14 12:40:29 +0000 | [diff] [blame] | 68 | * The ail lock _must_ be held when calling this function |
Steven Whitehouse | 1e1a3d0 | 2007-08-27 09:45:26 +0100 | [diff] [blame] | 69 | * |
| 70 | */ |
| 71 | |
Bob Peterson | 9bc980c | 2018-03-02 06:59:44 -0700 | [diff] [blame] | 72 | static void gfs2_remove_from_ail(struct gfs2_bufdata *bd) |
Steven Whitehouse | 1e1a3d0 | 2007-08-27 09:45:26 +0100 | [diff] [blame] | 73 | { |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 74 | bd->bd_tr = NULL; |
Steven Whitehouse | 1ad38c4 | 2007-09-03 11:01:33 +0100 | [diff] [blame] | 75 | list_del_init(&bd->bd_ail_st_list); |
| 76 | list_del_init(&bd->bd_ail_gl_list); |
Steven Whitehouse | 1e1a3d0 | 2007-08-27 09:45:26 +0100 | [diff] [blame] | 77 | atomic_dec(&bd->bd_gl->gl_ail_count); |
Steven Whitehouse | 1e1a3d0 | 2007-08-27 09:45:26 +0100 | [diff] [blame] | 78 | brelse(bd->bd_bh); |
| 79 | } |
| 80 | |
| 81 | /** |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 82 | * gfs2_ail1_start_one - Start I/O on a part of the AIL |
| 83 | * @sdp: the filesystem |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 84 | * @wbc: The writeback control structure |
| 85 | * @ai: The ail structure |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 86 | * |
| 87 | */ |
| 88 | |
Steven Whitehouse | 4f1de01 | 2011-04-26 10:23:56 +0100 | [diff] [blame] | 89 | static int gfs2_ail1_start_one(struct gfs2_sbd *sdp, |
| 90 | struct writeback_control *wbc, |
Bob Peterson | 6951108 | 2019-02-12 13:43:55 -0700 | [diff] [blame] | 91 | struct gfs2_trans *tr) |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 92 | __releases(&sdp->sd_ail_lock) |
| 93 | __acquires(&sdp->sd_ail_lock) |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 94 | { |
Steven Whitehouse | 5ac048b | 2011-03-30 16:25:51 +0100 | [diff] [blame] | 95 | struct gfs2_glock *gl = NULL; |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 96 | struct address_space *mapping; |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 97 | struct gfs2_bufdata *bd, *s; |
| 98 | struct buffer_head *bh; |
Bob Peterson | b1676cb | 2019-11-13 13:53:42 -0600 | [diff] [blame] | 99 | int ret = 0; |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 100 | |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 101 | list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) { |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 102 | bh = bd->bd_bh; |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 103 | |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 104 | gfs2_assert(sdp, bd->bd_tr == tr); |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 105 | |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 106 | if (!buffer_busy(bh)) { |
Bob Peterson | 30fe70a | 2019-11-13 11:47:09 -0600 | [diff] [blame] | 107 | if (buffer_uptodate(bh)) { |
| 108 | list_move(&bd->bd_ail_st_list, |
| 109 | &tr->tr_ail2_list); |
| 110 | continue; |
| 111 | } |
Bob Peterson | 036330c | 2019-04-10 11:46:35 -0600 | [diff] [blame] | 112 | if (!cmpxchg(&sdp->sd_log_error, 0, -EIO)) { |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 113 | gfs2_io_error_bh(sdp, bh); |
Bob Peterson | 6951108 | 2019-02-12 13:43:55 -0700 | [diff] [blame] | 114 | gfs2_withdraw_delayed(sdp); |
Andreas Gruenbacher | 9e1a9ec | 2018-06-07 11:56:46 +0100 | [diff] [blame] | 115 | } |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 116 | } |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 117 | |
Bob Peterson | 30fe70a | 2019-11-13 11:47:09 -0600 | [diff] [blame] | 118 | if (gfs2_withdrawn(sdp)) { |
| 119 | gfs2_remove_from_ail(bd); |
| 120 | continue; |
| 121 | } |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 122 | if (!buffer_dirty(bh)) |
| 123 | continue; |
| 124 | if (gl == bd->bd_gl) |
| 125 | continue; |
| 126 | gl = bd->bd_gl; |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 127 | list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list); |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 128 | mapping = bh->b_page->mapping; |
Steven Whitehouse | 4f1de01 | 2011-04-26 10:23:56 +0100 | [diff] [blame] | 129 | if (!mapping) |
| 130 | continue; |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 131 | spin_unlock(&sdp->sd_ail_lock); |
Bob Peterson | b1676cb | 2019-11-13 13:53:42 -0600 | [diff] [blame] | 132 | ret = generic_writepages(mapping, wbc); |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 133 | spin_lock(&sdp->sd_ail_lock); |
Bob Peterson | b1676cb | 2019-11-13 13:53:42 -0600 | [diff] [blame] | 134 | if (ret || wbc->nr_to_write <= 0) |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 135 | break; |
Bob Peterson | b1676cb | 2019-11-13 13:53:42 -0600 | [diff] [blame] | 136 | return -EBUSY; |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 137 | } |
Steven Whitehouse | 4f1de01 | 2011-04-26 10:23:56 +0100 | [diff] [blame] | 138 | |
Bob Peterson | b1676cb | 2019-11-13 13:53:42 -0600 | [diff] [blame] | 139 | return ret; |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * gfs2_ail1_flush - start writeback of some ail1 entries |
| 145 | * @sdp: The super block |
| 146 | * @wbc: The writeback control structure |
| 147 | * |
| 148 | * Writes back some ail1 entries, according to the limits in the |
| 149 | * writeback control structure |
| 150 | */ |
| 151 | |
| 152 | void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc) |
| 153 | { |
| 154 | struct list_head *head = &sdp->sd_ail1_list; |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 155 | struct gfs2_trans *tr; |
Steven Whitehouse | 885bcec | 2014-02-03 09:57:29 +0000 | [diff] [blame] | 156 | struct blk_plug plug; |
Bob Peterson | b1676cb | 2019-11-13 13:53:42 -0600 | [diff] [blame] | 157 | int ret = 0; |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 158 | |
Steven Whitehouse | c83ae9c | 2011-04-18 14:18:38 +0100 | [diff] [blame] | 159 | trace_gfs2_ail_flush(sdp, wbc, 1); |
Steven Whitehouse | 885bcec | 2014-02-03 09:57:29 +0000 | [diff] [blame] | 160 | blk_start_plug(&plug); |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 161 | spin_lock(&sdp->sd_ail_lock); |
Steven Whitehouse | 4f1de01 | 2011-04-26 10:23:56 +0100 | [diff] [blame] | 162 | restart: |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 163 | list_for_each_entry_reverse(tr, head, tr_list) { |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 164 | if (wbc->nr_to_write <= 0) |
| 165 | break; |
Bob Peterson | b1676cb | 2019-11-13 13:53:42 -0600 | [diff] [blame] | 166 | ret = gfs2_ail1_start_one(sdp, wbc, tr); |
| 167 | if (ret) { |
| 168 | if (ret == -EBUSY) |
| 169 | goto restart; |
| 170 | break; |
| 171 | } |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 172 | } |
| 173 | spin_unlock(&sdp->sd_ail_lock); |
Steven Whitehouse | 885bcec | 2014-02-03 09:57:29 +0000 | [diff] [blame] | 174 | blk_finish_plug(&plug); |
Bob Peterson | b1676cb | 2019-11-13 13:53:42 -0600 | [diff] [blame] | 175 | if (ret) |
Andreas Gruenbacher | badb55e | 2020-01-23 18:41:00 +0100 | [diff] [blame] | 176 | gfs2_withdraw(sdp); |
Steven Whitehouse | c83ae9c | 2011-04-18 14:18:38 +0100 | [diff] [blame] | 177 | trace_gfs2_ail_flush(sdp, wbc, 0); |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * gfs2_ail1_start - start writeback of all ail1 entries |
| 182 | * @sdp: The superblock |
| 183 | */ |
| 184 | |
| 185 | static void gfs2_ail1_start(struct gfs2_sbd *sdp) |
| 186 | { |
| 187 | struct writeback_control wbc = { |
| 188 | .sync_mode = WB_SYNC_NONE, |
| 189 | .nr_to_write = LONG_MAX, |
| 190 | .range_start = 0, |
| 191 | .range_end = LLONG_MAX, |
| 192 | }; |
| 193 | |
| 194 | return gfs2_ail1_flush(sdp, &wbc); |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced |
| 199 | * @sdp: the filesystem |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 200 | * @tr: the transaction |
| 201 | * @max_revokes: If nonzero, issue revokes for the bd items for written buffers |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 202 | * |
| 203 | */ |
| 204 | |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 205 | static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr, |
| 206 | int *max_revokes) |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 207 | { |
| 208 | struct gfs2_bufdata *bd, *s; |
| 209 | struct buffer_head *bh; |
| 210 | |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 211 | list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 212 | bd_ail_st_list) { |
| 213 | bh = bd->bd_bh; |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 214 | gfs2_assert(sdp, bd->bd_tr == tr); |
Bob Peterson | 036330c | 2019-04-10 11:46:35 -0600 | [diff] [blame] | 215 | /* |
| 216 | * If another process flagged an io error, e.g. writing to the |
| 217 | * journal, error all other bhs and move them off the ail1 to |
| 218 | * prevent a tight loop when unmount tries to flush ail1, |
| 219 | * regardless of whether they're still busy. If no outside |
| 220 | * errors were found and the buffer is busy, move to the next. |
| 221 | * If the ail buffer is not busy and caught an error, flag it |
| 222 | * for others. |
| 223 | */ |
| 224 | if (!sdp->sd_log_error && buffer_busy(bh)) |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 225 | continue; |
Bob Peterson | b524abc | 2018-10-04 10:21:07 -0500 | [diff] [blame] | 226 | if (!buffer_uptodate(bh) && |
Bob Peterson | 036330c | 2019-04-10 11:46:35 -0600 | [diff] [blame] | 227 | !cmpxchg(&sdp->sd_log_error, 0, -EIO)) { |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 228 | gfs2_io_error_bh(sdp, bh); |
Bob Peterson | 6951108 | 2019-02-12 13:43:55 -0700 | [diff] [blame] | 229 | gfs2_withdraw_delayed(sdp); |
Andreas Gruenbacher | 9e1a9ec | 2018-06-07 11:56:46 +0100 | [diff] [blame] | 230 | } |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 231 | /* |
| 232 | * If we have space for revokes and the bd is no longer on any |
| 233 | * buf list, we can just add a revoke for it immediately and |
| 234 | * avoid having to put it on the ail2 list, where it would need |
| 235 | * to be revoked later. |
| 236 | */ |
| 237 | if (*max_revokes && list_empty(&bd->bd_list)) { |
| 238 | gfs2_add_revoke(sdp, bd); |
| 239 | (*max_revokes)--; |
| 240 | continue; |
| 241 | } |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 242 | list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list); |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 243 | } |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 244 | } |
| 245 | |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 246 | /** |
| 247 | * gfs2_ail1_empty - Try to empty the ail1 lists |
| 248 | * @sdp: The superblock |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 249 | * @max_revokes: If non-zero, add revokes where appropriate |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 250 | * |
| 251 | * Tries to empty the ail1 lists, starting with the oldest first |
| 252 | */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 253 | |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 254 | static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int max_revokes) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 255 | { |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 256 | struct gfs2_trans *tr, *s; |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 257 | int oldest_tr = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 258 | int ret; |
| 259 | |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 260 | spin_lock(&sdp->sd_ail_lock); |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 261 | list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) { |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 262 | gfs2_ail1_empty_one(sdp, tr, &max_revokes); |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 263 | if (list_empty(&tr->tr_ail1_list) && oldest_tr) |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 264 | list_move(&tr->tr_list, &sdp->sd_ail2_list); |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 265 | else |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 266 | oldest_tr = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 267 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 268 | ret = list_empty(&sdp->sd_ail1_list); |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 269 | spin_unlock(&sdp->sd_ail_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 270 | |
Bob Peterson | 6951108 | 2019-02-12 13:43:55 -0700 | [diff] [blame] | 271 | if (test_bit(SDF_WITHDRAWING, &sdp->sd_flags)) { |
Andreas Gruenbacher | badb55e | 2020-01-23 18:41:00 +0100 | [diff] [blame] | 272 | gfs2_lm(sdp, "fatal: I/O error(s)\n"); |
| 273 | gfs2_withdraw(sdp); |
| 274 | } |
Andreas Gruenbacher | 9e1a9ec | 2018-06-07 11:56:46 +0100 | [diff] [blame] | 275 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 276 | return ret; |
| 277 | } |
| 278 | |
Steven Whitehouse | 26b06a6 | 2011-05-21 19:21:07 +0100 | [diff] [blame] | 279 | static void gfs2_ail1_wait(struct gfs2_sbd *sdp) |
| 280 | { |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 281 | struct gfs2_trans *tr; |
Steven Whitehouse | 26b06a6 | 2011-05-21 19:21:07 +0100 | [diff] [blame] | 282 | struct gfs2_bufdata *bd; |
| 283 | struct buffer_head *bh; |
| 284 | |
| 285 | spin_lock(&sdp->sd_ail_lock); |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 286 | list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) { |
| 287 | list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) { |
Steven Whitehouse | 26b06a6 | 2011-05-21 19:21:07 +0100 | [diff] [blame] | 288 | bh = bd->bd_bh; |
| 289 | if (!buffer_locked(bh)) |
| 290 | continue; |
| 291 | get_bh(bh); |
| 292 | spin_unlock(&sdp->sd_ail_lock); |
| 293 | wait_on_buffer(bh); |
| 294 | brelse(bh); |
| 295 | return; |
| 296 | } |
| 297 | } |
| 298 | spin_unlock(&sdp->sd_ail_lock); |
| 299 | } |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 300 | |
| 301 | /** |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 302 | * gfs2_ail_empty_tr - empty one of the ail lists for a transaction |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 303 | */ |
| 304 | |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 305 | static void gfs2_ail_empty_tr(struct gfs2_sbd *sdp, struct gfs2_trans *tr, |
| 306 | struct list_head *head) |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 307 | { |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 308 | struct gfs2_bufdata *bd; |
| 309 | |
| 310 | while (!list_empty(head)) { |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 311 | bd = list_first_entry(head, struct gfs2_bufdata, |
| 312 | bd_ail_st_list); |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 313 | gfs2_assert(sdp, bd->bd_tr == tr); |
Steven Whitehouse | f91a0d3 | 2007-10-15 16:29:05 +0100 | [diff] [blame] | 314 | gfs2_remove_from_ail(bd); |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 318 | static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail) |
| 319 | { |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 320 | struct gfs2_trans *tr, *safe; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 321 | unsigned int old_tail = sdp->sd_log_tail; |
| 322 | int wrap = (new_tail < old_tail); |
| 323 | int a, b, rm; |
| 324 | |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 325 | spin_lock(&sdp->sd_ail_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 326 | |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 327 | list_for_each_entry_safe(tr, safe, &sdp->sd_ail2_list, tr_list) { |
| 328 | a = (old_tail <= tr->tr_first); |
| 329 | b = (tr->tr_first < new_tail); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 330 | rm = (wrap) ? (a || b) : (a && b); |
| 331 | if (!rm) |
| 332 | continue; |
| 333 | |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 334 | gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list); |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 335 | list_del(&tr->tr_list); |
| 336 | gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list)); |
| 337 | gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list)); |
| 338 | kfree(tr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 341 | spin_unlock(&sdp->sd_ail_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 345 | * gfs2_log_release - Release a given number of log blocks |
| 346 | * @sdp: The GFS2 superblock |
| 347 | * @blks: The number of blocks |
| 348 | * |
| 349 | */ |
| 350 | |
| 351 | void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks) |
| 352 | { |
| 353 | |
| 354 | atomic_add(blks, &sdp->sd_log_blks_free); |
| 355 | trace_gfs2_log_blocks(sdp, blks); |
| 356 | gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= |
| 357 | sdp->sd_jdesc->jd_blocks); |
| 358 | up_read(&sdp->sd_log_flush_lock); |
| 359 | } |
| 360 | |
| 361 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 362 | * gfs2_log_reserve - Make a log reservation |
| 363 | * @sdp: The GFS2 superblock |
| 364 | * @blks: The number of blocks to reserve |
| 365 | * |
Steven Whitehouse | 8991864 | 2007-06-01 15:19:33 +0100 | [diff] [blame] | 366 | * Note that we never give out the last few blocks of the journal. Thats |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 367 | * due to the fact that there is a small number of header blocks |
Steven Whitehouse | b004157 | 2006-11-23 10:51:34 -0500 | [diff] [blame] | 368 | * associated with each log flush. The exact number can't be known until |
| 369 | * flush time, so we ensure that we have just enough free blocks at all |
| 370 | * times to avoid running out during a log flush. |
| 371 | * |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 372 | * We no longer flush the log here, instead we wake up logd to do that |
| 373 | * for us. To avoid the thundering herd and to ensure that we deal fairly |
| 374 | * with queued waiters, we use an exclusive wait. This means that when we |
| 375 | * get woken with enough journal space to get our reservation, we need to |
| 376 | * wake the next waiter on the list. |
| 377 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 378 | * Returns: errno |
| 379 | */ |
| 380 | |
| 381 | int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks) |
| 382 | { |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 383 | int ret = 0; |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 384 | unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 385 | unsigned wanted = blks + reserved_blks; |
| 386 | DEFINE_WAIT(wait); |
| 387 | int did_wait = 0; |
| 388 | unsigned int free_blocks; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 389 | |
| 390 | if (gfs2_assert_warn(sdp, blks) || |
| 391 | gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks)) |
| 392 | return -EINVAL; |
Bob Peterson | f07b352 | 2017-01-05 16:01:45 -0500 | [diff] [blame] | 393 | atomic_add(blks, &sdp->sd_log_blks_needed); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 394 | retry: |
| 395 | free_blocks = atomic_read(&sdp->sd_log_blks_free); |
| 396 | if (unlikely(free_blocks <= wanted)) { |
| 397 | do { |
| 398 | prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait, |
| 399 | TASK_UNINTERRUPTIBLE); |
| 400 | wake_up(&sdp->sd_logd_waitq); |
| 401 | did_wait = 1; |
| 402 | if (atomic_read(&sdp->sd_log_blks_free) <= wanted) |
| 403 | io_schedule(); |
| 404 | free_blocks = atomic_read(&sdp->sd_log_blks_free); |
| 405 | } while(free_blocks <= wanted); |
| 406 | finish_wait(&sdp->sd_log_waitq, &wait); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 407 | } |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 408 | atomic_inc(&sdp->sd_reserving_log); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 409 | if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks, |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 410 | free_blocks - blks) != free_blocks) { |
| 411 | if (atomic_dec_and_test(&sdp->sd_reserving_log)) |
| 412 | wake_up(&sdp->sd_reserving_log_wait); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 413 | goto retry; |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 414 | } |
Bob Peterson | f07b352 | 2017-01-05 16:01:45 -0500 | [diff] [blame] | 415 | atomic_sub(blks, &sdp->sd_log_blks_needed); |
Steven Whitehouse | 6399777 | 2009-06-12 08:49:20 +0100 | [diff] [blame] | 416 | trace_gfs2_log_blocks(sdp, -blks); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | * If we waited, then so might others, wake them up _after_ we get |
| 420 | * our share of the log. |
| 421 | */ |
| 422 | if (unlikely(did_wait)) |
| 423 | wake_up(&sdp->sd_log_waitq); |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 424 | |
| 425 | down_read(&sdp->sd_log_flush_lock); |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 426 | if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) { |
| 427 | gfs2_log_release(sdp, blks); |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 428 | ret = -EROFS; |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 429 | } |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 430 | if (atomic_dec_and_test(&sdp->sd_reserving_log)) |
| 431 | wake_up(&sdp->sd_reserving_log_wait); |
| 432 | return ret; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 433 | } |
| 434 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 435 | /** |
| 436 | * log_distance - Compute distance between two journal blocks |
| 437 | * @sdp: The GFS2 superblock |
| 438 | * @newer: The most recent journal block of the pair |
| 439 | * @older: The older journal block of the pair |
| 440 | * |
| 441 | * Compute the distance (in the journal direction) between two |
| 442 | * blocks in the journal |
| 443 | * |
| 444 | * Returns: the distance in blocks |
| 445 | */ |
| 446 | |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 447 | static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 448 | unsigned int older) |
| 449 | { |
| 450 | int dist; |
| 451 | |
| 452 | dist = newer - older; |
| 453 | if (dist < 0) |
| 454 | dist += sdp->sd_jdesc->jd_blocks; |
| 455 | |
| 456 | return dist; |
| 457 | } |
| 458 | |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 459 | /** |
| 460 | * calc_reserved - Calculate the number of blocks to reserve when |
| 461 | * refunding a transaction's unused buffers. |
| 462 | * @sdp: The GFS2 superblock |
| 463 | * |
| 464 | * This is complex. We need to reserve room for all our currently used |
| 465 | * metadata buffers (e.g. normal file I/O rewriting file time stamps) and |
| 466 | * all our journaled data buffers for journaled files (e.g. files in the |
| 467 | * meta_fs like rindex, or files for which chattr +j was done.) |
| 468 | * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush |
| 469 | * will count it as free space (sd_log_blks_free) and corruption will follow. |
| 470 | * |
| 471 | * We can have metadata bufs and jdata bufs in the same journal. So each |
| 472 | * type gets its own log header, for which we need to reserve a block. |
| 473 | * In fact, each type has the potential for needing more than one header |
| 474 | * in cases where we have more buffers than will fit on a journal page. |
| 475 | * Metadata journal entries take up half the space of journaled buffer entries. |
| 476 | * Thus, metadata entries have buf_limit (502) and journaled buffers have |
| 477 | * databuf_limit (251) before they cause a wrap around. |
| 478 | * |
| 479 | * Also, we need to reserve blocks for revoke journal entries and one for an |
| 480 | * overall header for the lot. |
| 481 | * |
| 482 | * Returns: the number of blocks reserved |
| 483 | */ |
| 484 | static unsigned int calc_reserved(struct gfs2_sbd *sdp) |
| 485 | { |
| 486 | unsigned int reserved = 0; |
Steven Whitehouse | 022ef4f | 2014-02-21 21:55:33 +0000 | [diff] [blame] | 487 | unsigned int mbuf; |
| 488 | unsigned int dbuf; |
| 489 | struct gfs2_trans *tr = sdp->sd_log_tr; |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 490 | |
Steven Whitehouse | 022ef4f | 2014-02-21 21:55:33 +0000 | [diff] [blame] | 491 | if (tr) { |
| 492 | mbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm; |
| 493 | dbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm; |
| 494 | reserved = mbuf + dbuf; |
| 495 | /* Account for header blocks */ |
| 496 | reserved += DIV_ROUND_UP(mbuf, buf_limit(sdp)); |
| 497 | reserved += DIV_ROUND_UP(dbuf, databuf_limit(sdp)); |
| 498 | } |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 499 | |
Andreas Gruenbacher | 5d43975 | 2020-01-09 13:54:36 +0100 | [diff] [blame] | 500 | if (sdp->sd_log_committed_revoke > 0) |
| 501 | reserved += gfs2_struct2blk(sdp, sdp->sd_log_committed_revoke); |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 502 | /* One for the overall header */ |
| 503 | if (reserved) |
| 504 | reserved++; |
| 505 | return reserved; |
| 506 | } |
| 507 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 508 | static unsigned int current_tail(struct gfs2_sbd *sdp) |
| 509 | { |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 510 | struct gfs2_trans *tr; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 511 | unsigned int tail; |
| 512 | |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 513 | spin_lock(&sdp->sd_ail_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 514 | |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 515 | if (list_empty(&sdp->sd_ail1_list)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 516 | tail = sdp->sd_log_head; |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 517 | } else { |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 518 | tr = list_entry(sdp->sd_ail1_list.prev, struct gfs2_trans, |
| 519 | tr_list); |
| 520 | tail = tr->tr_first; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 523 | spin_unlock(&sdp->sd_ail_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 524 | |
| 525 | return tail; |
| 526 | } |
| 527 | |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 528 | static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 529 | { |
| 530 | unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail); |
| 531 | |
| 532 | ail2_empty(sdp, new_tail); |
| 533 | |
Steven Whitehouse | fd041f0 | 2007-11-08 14:55:03 +0000 | [diff] [blame] | 534 | atomic_add(dist, &sdp->sd_log_blks_free); |
Steven Whitehouse | 6399777 | 2009-06-12 08:49:20 +0100 | [diff] [blame] | 535 | trace_gfs2_log_blocks(sdp, dist); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 536 | gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= |
| 537 | sdp->sd_jdesc->jd_blocks); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 538 | |
| 539 | sdp->sd_log_tail = new_tail; |
| 540 | } |
| 541 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 542 | |
Bob Peterson | 9ff7828 | 2019-11-13 13:47:02 -0600 | [diff] [blame] | 543 | void log_flush_wait(struct gfs2_sbd *sdp) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 544 | { |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 545 | DEFINE_WAIT(wait); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 546 | |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 547 | if (atomic_read(&sdp->sd_log_in_flight)) { |
| 548 | do { |
| 549 | prepare_to_wait(&sdp->sd_log_flush_wait, &wait, |
| 550 | TASK_UNINTERRUPTIBLE); |
| 551 | if (atomic_read(&sdp->sd_log_in_flight)) |
| 552 | io_schedule(); |
| 553 | } while(atomic_read(&sdp->sd_log_in_flight)); |
| 554 | finish_wait(&sdp->sd_log_flush_wait, &wait); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 555 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 558 | static int ip_cmp(void *priv, struct list_head *a, struct list_head *b) |
Bob Peterson | 4a36d08 | 2012-02-14 14:49:57 -0500 | [diff] [blame] | 559 | { |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 560 | struct gfs2_inode *ipa, *ipb; |
Bob Peterson | 4a36d08 | 2012-02-14 14:49:57 -0500 | [diff] [blame] | 561 | |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 562 | ipa = list_entry(a, struct gfs2_inode, i_ordered); |
| 563 | ipb = list_entry(b, struct gfs2_inode, i_ordered); |
Bob Peterson | 4a36d08 | 2012-02-14 14:49:57 -0500 | [diff] [blame] | 564 | |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 565 | if (ipa->i_no_addr < ipb->i_no_addr) |
Bob Peterson | 4a36d08 | 2012-02-14 14:49:57 -0500 | [diff] [blame] | 566 | return -1; |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 567 | if (ipa->i_no_addr > ipb->i_no_addr) |
Bob Peterson | 4a36d08 | 2012-02-14 14:49:57 -0500 | [diff] [blame] | 568 | return 1; |
| 569 | return 0; |
| 570 | } |
| 571 | |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 572 | static void gfs2_ordered_write(struct gfs2_sbd *sdp) |
| 573 | { |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 574 | struct gfs2_inode *ip; |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 575 | LIST_HEAD(written); |
| 576 | |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 577 | spin_lock(&sdp->sd_ordered_lock); |
Andreas Gruenbacher | a5b1d3f | 2019-04-05 12:16:14 +0100 | [diff] [blame] | 578 | list_sort(NULL, &sdp->sd_log_ordered, &ip_cmp); |
| 579 | while (!list_empty(&sdp->sd_log_ordered)) { |
| 580 | ip = list_entry(sdp->sd_log_ordered.next, struct gfs2_inode, i_ordered); |
Abhi Das | 1f23bc7 | 2017-12-22 07:55:31 -0600 | [diff] [blame] | 581 | if (ip->i_inode.i_mapping->nrpages == 0) { |
| 582 | test_and_clear_bit(GIF_ORDERED, &ip->i_flags); |
| 583 | list_del(&ip->i_ordered); |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 584 | continue; |
Abhi Das | 1f23bc7 | 2017-12-22 07:55:31 -0600 | [diff] [blame] | 585 | } |
| 586 | list_move(&ip->i_ordered, &written); |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 587 | spin_unlock(&sdp->sd_ordered_lock); |
| 588 | filemap_fdatawrite(ip->i_inode.i_mapping); |
| 589 | spin_lock(&sdp->sd_ordered_lock); |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 590 | } |
Andreas Gruenbacher | a5b1d3f | 2019-04-05 12:16:14 +0100 | [diff] [blame] | 591 | list_splice(&written, &sdp->sd_log_ordered); |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 592 | spin_unlock(&sdp->sd_ordered_lock); |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static void gfs2_ordered_wait(struct gfs2_sbd *sdp) |
| 596 | { |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 597 | struct gfs2_inode *ip; |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 598 | |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 599 | spin_lock(&sdp->sd_ordered_lock); |
Andreas Gruenbacher | a5b1d3f | 2019-04-05 12:16:14 +0100 | [diff] [blame] | 600 | while (!list_empty(&sdp->sd_log_ordered)) { |
| 601 | ip = list_entry(sdp->sd_log_ordered.next, struct gfs2_inode, i_ordered); |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 602 | list_del(&ip->i_ordered); |
| 603 | WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags)); |
| 604 | if (ip->i_inode.i_mapping->nrpages == 0) |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 605 | continue; |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 606 | spin_unlock(&sdp->sd_ordered_lock); |
| 607 | filemap_fdatawait(ip->i_inode.i_mapping); |
| 608 | spin_lock(&sdp->sd_ordered_lock); |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 609 | } |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 610 | spin_unlock(&sdp->sd_ordered_lock); |
| 611 | } |
| 612 | |
| 613 | void gfs2_ordered_del_inode(struct gfs2_inode *ip) |
| 614 | { |
| 615 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 616 | |
| 617 | spin_lock(&sdp->sd_ordered_lock); |
| 618 | if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags)) |
| 619 | list_del(&ip->i_ordered); |
| 620 | spin_unlock(&sdp->sd_ordered_lock); |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 621 | } |
| 622 | |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 623 | void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd) |
| 624 | { |
| 625 | struct buffer_head *bh = bd->bd_bh; |
| 626 | struct gfs2_glock *gl = bd->bd_gl; |
| 627 | |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 628 | bh->b_private = NULL; |
| 629 | bd->bd_blkno = bh->b_blocknr; |
Bob Peterson | 9290a9a | 2013-12-10 12:06:35 -0500 | [diff] [blame] | 630 | gfs2_remove_from_ail(bd); /* drops ref on bh */ |
| 631 | bd->bd_bh = NULL; |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 632 | sdp->sd_log_num_revoke++; |
Bob Peterson | 638803d | 2019-06-06 07:33:38 -0500 | [diff] [blame] | 633 | if (atomic_inc_return(&gl->gl_revokes) == 1) |
Andreas Gruenbacher | 9287c64 | 2019-04-04 21:11:11 +0100 | [diff] [blame] | 634 | gfs2_glock_hold(gl); |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 635 | set_bit(GLF_LFLUSH, &gl->gl_flags); |
Andreas Gruenbacher | a5b1d3f | 2019-04-05 12:16:14 +0100 | [diff] [blame] | 636 | list_add(&bd->bd_list, &sdp->sd_log_revokes); |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 637 | } |
| 638 | |
Bob Peterson | fe5e7ba | 2019-11-14 09:49:11 -0500 | [diff] [blame] | 639 | void gfs2_glock_remove_revoke(struct gfs2_glock *gl) |
| 640 | { |
| 641 | if (atomic_dec_return(&gl->gl_revokes) == 0) { |
| 642 | clear_bit(GLF_LFLUSH, &gl->gl_flags); |
| 643 | gfs2_glock_queue_put(gl); |
| 644 | } |
| 645 | } |
| 646 | |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 647 | /** |
| 648 | * gfs2_write_revokes - Add as many revokes to the system transaction as we can |
| 649 | * @sdp: The GFS2 superblock |
| 650 | * |
| 651 | * Our usual strategy is to defer writing revokes as much as we can in the hope |
| 652 | * that we'll eventually overwrite the journal, which will make those revokes |
| 653 | * go away. This changes when we flush the log: at that point, there will |
| 654 | * likely be some left-over space in the last revoke block of that transaction. |
| 655 | * We can fill that space with additional revokes for blocks that have already |
| 656 | * been written back. This will basically come at no cost now, and will save |
| 657 | * us from having to keep track of those blocks on the AIL2 list later. |
| 658 | */ |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 659 | void gfs2_write_revokes(struct gfs2_sbd *sdp) |
| 660 | { |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 661 | /* number of revokes we still have room for */ |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 662 | int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64); |
| 663 | |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 664 | gfs2_log_lock(sdp); |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 665 | while (sdp->sd_log_num_revoke > max_revokes) |
| 666 | max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64); |
| 667 | max_revokes -= sdp->sd_log_num_revoke; |
| 668 | if (!sdp->sd_log_num_revoke) { |
| 669 | atomic_dec(&sdp->sd_log_blks_free); |
| 670 | /* If no blocks have been reserved, we need to also |
| 671 | * reserve a block for the header */ |
| 672 | if (!sdp->sd_log_blks_reserved) |
| 673 | atomic_dec(&sdp->sd_log_blks_free); |
| 674 | } |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 675 | gfs2_ail1_empty(sdp, max_revokes); |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 676 | gfs2_log_unlock(sdp); |
| 677 | |
| 678 | if (!sdp->sd_log_num_revoke) { |
| 679 | atomic_inc(&sdp->sd_log_blks_free); |
| 680 | if (!sdp->sd_log_blks_reserved) |
| 681 | atomic_inc(&sdp->sd_log_blks_free); |
| 682 | } |
| 683 | } |
| 684 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 685 | /** |
Bob Peterson | 7c70b89 | 2019-03-25 09:34:19 -0600 | [diff] [blame] | 686 | * gfs2_write_log_header - Write a journal log header buffer at lblock |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 687 | * @sdp: The GFS2 superblock |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 688 | * @jd: journal descriptor of the journal to which we are writing |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 689 | * @seq: sequence number |
| 690 | * @tail: tail of the log |
Bob Peterson | 7c70b89 | 2019-03-25 09:34:19 -0600 | [diff] [blame] | 691 | * @lblock: value for lh_blkno (block number relative to start of journal) |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 692 | * @flags: log header flags GFS2_LOG_HEAD_* |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 693 | * @op_flags: flags to pass to the bio |
| 694 | * |
| 695 | * Returns: the initialized log buffer descriptor |
| 696 | */ |
| 697 | |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 698 | void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd, |
Bob Peterson | 7c70b89 | 2019-03-25 09:34:19 -0600 | [diff] [blame] | 699 | u64 seq, u32 tail, u32 lblock, u32 flags, |
| 700 | int op_flags) |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 701 | { |
| 702 | struct gfs2_log_header *lh; |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 703 | u32 hash, crc; |
Bob Peterson | ade4808 | 2019-11-20 08:53:14 -0500 | [diff] [blame] | 704 | struct page *page; |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 705 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; |
| 706 | struct timespec64 tv; |
| 707 | struct super_block *sb = sdp->sd_vfs; |
Bob Peterson | 7c70b89 | 2019-03-25 09:34:19 -0600 | [diff] [blame] | 708 | u64 dblock; |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 709 | |
Bob Peterson | ade4808 | 2019-11-20 08:53:14 -0500 | [diff] [blame] | 710 | if (gfs2_withdrawn(sdp)) |
| 711 | goto out; |
| 712 | |
| 713 | page = mempool_alloc(gfs2_page_pool, GFP_NOIO); |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 714 | lh = page_address(page); |
| 715 | clear_page(lh); |
| 716 | |
| 717 | lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC); |
| 718 | lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH); |
| 719 | lh->lh_header.__pad0 = cpu_to_be64(0); |
| 720 | lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH); |
| 721 | lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid); |
| 722 | lh->lh_sequence = cpu_to_be64(seq); |
| 723 | lh->lh_flags = cpu_to_be32(flags); |
| 724 | lh->lh_tail = cpu_to_be32(tail); |
Bob Peterson | 7c70b89 | 2019-03-25 09:34:19 -0600 | [diff] [blame] | 725 | lh->lh_blkno = cpu_to_be32(lblock); |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 726 | hash = ~crc32(~0, lh, LH_V1_SIZE); |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 727 | lh->lh_hash = cpu_to_be32(hash); |
| 728 | |
Arnd Bergmann | ee9c7f9 | 2018-06-20 15:15:24 -0500 | [diff] [blame] | 729 | ktime_get_coarse_real_ts64(&tv); |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 730 | lh->lh_nsec = cpu_to_be32(tv.tv_nsec); |
| 731 | lh->lh_sec = cpu_to_be64(tv.tv_sec); |
Bob Peterson | 7c70b89 | 2019-03-25 09:34:19 -0600 | [diff] [blame] | 732 | if (!list_empty(&jd->extent_list)) |
Andreas Gruenbacher | 19ebc05 | 2019-08-28 22:21:34 +0200 | [diff] [blame] | 733 | dblock = gfs2_log_bmap(jd, lblock); |
Bob Peterson | 7c70b89 | 2019-03-25 09:34:19 -0600 | [diff] [blame] | 734 | else { |
| 735 | int ret = gfs2_lblk_to_dblk(jd->jd_inode, lblock, &dblock); |
| 736 | if (gfs2_assert_withdraw(sdp, ret == 0)) |
| 737 | return; |
| 738 | } |
| 739 | lh->lh_addr = cpu_to_be64(dblock); |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 740 | lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr); |
| 741 | |
| 742 | /* We may only write local statfs, quota, etc., when writing to our |
| 743 | own journal. The values are left 0 when recovering a journal |
| 744 | different from our own. */ |
| 745 | if (!(flags & GFS2_LOG_HEAD_RECOVERY)) { |
| 746 | lh->lh_statfs_addr = |
| 747 | cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr); |
| 748 | lh->lh_quota_addr = |
| 749 | cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr); |
| 750 | |
| 751 | spin_lock(&sdp->sd_statfs_spin); |
| 752 | lh->lh_local_total = cpu_to_be64(l_sc->sc_total); |
| 753 | lh->lh_local_free = cpu_to_be64(l_sc->sc_free); |
| 754 | lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes); |
| 755 | spin_unlock(&sdp->sd_statfs_spin); |
| 756 | } |
| 757 | |
| 758 | BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE); |
| 759 | |
| 760 | crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4, |
| 761 | sb->s_blocksize - LH_V1_SIZE - 4); |
| 762 | lh->lh_crc = cpu_to_be32(crc); |
| 763 | |
Bob Peterson | 7c70b89 | 2019-03-25 09:34:19 -0600 | [diff] [blame] | 764 | gfs2_log_write(sdp, page, sb->s_blocksize, 0, dblock); |
Abhi Das | f4686c2 | 2019-05-02 14:17:40 -0500 | [diff] [blame] | 765 | gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE | op_flags); |
Bob Peterson | ade4808 | 2019-11-20 08:53:14 -0500 | [diff] [blame] | 766 | out: |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 767 | log_flush_wait(sdp); |
| 768 | } |
| 769 | |
| 770 | /** |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 771 | * log_write_header - Get and initialize a journal header buffer |
| 772 | * @sdp: The GFS2 superblock |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 773 | * @flags: The log header flags, including log header origin |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 774 | * |
| 775 | * Returns: the initialized log buffer descriptor |
| 776 | */ |
| 777 | |
Steven Whitehouse | fdb76a4 | 2012-04-02 15:34:36 +0100 | [diff] [blame] | 778 | static void log_write_header(struct gfs2_sbd *sdp, u32 flags) |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 779 | { |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 780 | unsigned int tail; |
Jan Kara | 0f0b9b6 | 2017-05-02 13:14:13 +0200 | [diff] [blame] | 781 | int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC; |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 782 | enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state); |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 783 | |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 784 | gfs2_assert_withdraw(sdp, (state != SFS_FROZEN)); |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 785 | tail = current_tail(sdp); |
| 786 | |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 787 | if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) { |
| 788 | gfs2_ordered_wait(sdp); |
| 789 | log_flush_wait(sdp); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 790 | op_flags = REQ_SYNC | REQ_META | REQ_PRIO; |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 791 | } |
Steven Whitehouse | e8c92ed | 2012-04-16 09:28:31 +0100 | [diff] [blame] | 792 | sdp->sd_log_idle = (tail == sdp->sd_log_flush_head); |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 793 | gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++, tail, |
Bob Peterson | 7c70b89 | 2019-03-25 09:34:19 -0600 | [diff] [blame] | 794 | sdp->sd_log_flush_head, flags, op_flags); |
Andreas Gruenbacher | 19ebc05 | 2019-08-28 22:21:34 +0200 | [diff] [blame] | 795 | gfs2_log_incr_head(sdp); |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 796 | |
| 797 | if (sdp->sd_log_tail != tail) |
| 798 | log_pull_tail(sdp, tail); |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /** |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 802 | * ail_drain - drain the ail lists after a withdraw |
| 803 | * @sdp: Pointer to GFS2 superblock |
| 804 | */ |
| 805 | static void ail_drain(struct gfs2_sbd *sdp) |
| 806 | { |
| 807 | struct gfs2_trans *tr; |
| 808 | |
| 809 | spin_lock(&sdp->sd_ail_lock); |
| 810 | /* |
| 811 | * For transactions on the sd_ail1_list we need to drain both the |
| 812 | * ail1 and ail2 lists. That's because function gfs2_ail1_start_one |
| 813 | * (temporarily) moves items from its tr_ail1 list to tr_ail2 list |
| 814 | * before revokes are sent for that block. Items on the sd_ail2_list |
| 815 | * should have already gotten beyond that point, so no need. |
| 816 | */ |
| 817 | while (!list_empty(&sdp->sd_ail1_list)) { |
| 818 | tr = list_first_entry(&sdp->sd_ail1_list, struct gfs2_trans, |
| 819 | tr_list); |
| 820 | gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail1_list); |
| 821 | gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list); |
| 822 | list_del(&tr->tr_list); |
| 823 | kfree(tr); |
| 824 | } |
| 825 | while (!list_empty(&sdp->sd_ail2_list)) { |
| 826 | tr = list_first_entry(&sdp->sd_ail2_list, struct gfs2_trans, |
| 827 | tr_list); |
| 828 | gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list); |
| 829 | list_del(&tr->tr_list); |
| 830 | kfree(tr); |
| 831 | } |
| 832 | spin_unlock(&sdp->sd_ail_lock); |
| 833 | } |
| 834 | |
| 835 | /** |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 836 | * gfs2_log_flush - flush incore transaction(s) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 837 | * @sdp: the filesystem |
| 838 | * @gl: The glock structure to flush. If NULL, flush the whole incore log |
Bob Peterson | 805c0907 | 2018-01-08 10:34:17 -0500 | [diff] [blame] | 839 | * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 840 | * |
| 841 | */ |
| 842 | |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 843 | void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 844 | { |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 845 | struct gfs2_trans *tr = NULL; |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 846 | enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 847 | |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 848 | down_write(&sdp->sd_log_flush_lock); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 849 | |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 850 | /* |
| 851 | * Do this check while holding the log_flush_lock to prevent new |
| 852 | * buffers from being added to the ail via gfs2_pin() |
| 853 | */ |
| 854 | if (gfs2_withdrawn(sdp)) |
| 855 | goto out; |
| 856 | |
Steven Whitehouse | 2bcd610 | 2007-11-08 14:25:12 +0000 | [diff] [blame] | 857 | /* Log might have been flushed while we waited for the flush lock */ |
| 858 | if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) { |
| 859 | up_write(&sdp->sd_log_flush_lock); |
| 860 | return; |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 861 | } |
Bob Peterson | 805c0907 | 2018-01-08 10:34:17 -0500 | [diff] [blame] | 862 | trace_gfs2_log_flush(sdp, 1, flags); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 863 | |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 864 | if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN) |
Benjamin Marzinski | 400ac52 | 2015-12-09 07:46:33 -0600 | [diff] [blame] | 865 | clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags); |
| 866 | |
Steven Whitehouse | b1ab1e4 | 2014-02-25 11:52:20 +0000 | [diff] [blame] | 867 | sdp->sd_log_flush_head = sdp->sd_log_head; |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 868 | tr = sdp->sd_log_tr; |
| 869 | if (tr) { |
| 870 | sdp->sd_log_tr = NULL; |
| 871 | INIT_LIST_HEAD(&tr->tr_ail1_list); |
| 872 | INIT_LIST_HEAD(&tr->tr_ail2_list); |
Steven Whitehouse | b1ab1e4 | 2014-02-25 11:52:20 +0000 | [diff] [blame] | 873 | tr->tr_first = sdp->sd_log_flush_head; |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 874 | if (unlikely (state == SFS_FROZEN)) |
| 875 | gfs2_assert_withdraw(sdp, !tr->tr_num_buf_new && !tr->tr_num_databuf_new); |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 876 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 877 | |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 878 | if (unlikely(state == SFS_FROZEN)) |
| 879 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 880 | gfs2_assert_withdraw(sdp, |
Andreas Gruenbacher | 5d43975 | 2020-01-09 13:54:36 +0100 | [diff] [blame] | 881 | sdp->sd_log_num_revoke == sdp->sd_log_committed_revoke); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 882 | |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 883 | gfs2_ordered_write(sdp); |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 884 | if (gfs2_withdrawn(sdp)) |
| 885 | goto out; |
Steven Whitehouse | d69a3c6 | 2014-02-21 15:22:35 +0000 | [diff] [blame] | 886 | lops_before_commit(sdp, tr); |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 887 | if (gfs2_withdrawn(sdp)) |
| 888 | goto out; |
Abhi Das | f4686c2 | 2019-05-02 14:17:40 -0500 | [diff] [blame] | 889 | gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE); |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 890 | if (gfs2_withdrawn(sdp)) |
| 891 | goto out; |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 892 | |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 893 | if (sdp->sd_log_head != sdp->sd_log_flush_head) { |
Bob Peterson | 428fd95 | 2014-03-12 10:34:16 -0400 | [diff] [blame] | 894 | log_flush_wait(sdp); |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 895 | log_write_header(sdp, flags); |
Steven Whitehouse | 34cc178 | 2012-03-09 10:45:56 +0000 | [diff] [blame] | 896 | } else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){ |
Steven Whitehouse | fd041f0 | 2007-11-08 14:55:03 +0000 | [diff] [blame] | 897 | atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */ |
Steven Whitehouse | 6399777 | 2009-06-12 08:49:20 +0100 | [diff] [blame] | 898 | trace_gfs2_log_blocks(sdp, -1); |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 899 | log_write_header(sdp, flags); |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 900 | } |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 901 | if (gfs2_withdrawn(sdp)) |
| 902 | goto out; |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 903 | lops_after_commit(sdp, tr); |
Steven Whitehouse | fe1a698 | 2006-10-11 13:34:59 -0400 | [diff] [blame] | 904 | |
| 905 | gfs2_log_lock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 906 | sdp->sd_log_head = sdp->sd_log_flush_head; |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 907 | sdp->sd_log_blks_reserved = 0; |
Andreas Gruenbacher | 5d43975 | 2020-01-09 13:54:36 +0100 | [diff] [blame] | 908 | sdp->sd_log_committed_revoke = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 909 | |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 910 | spin_lock(&sdp->sd_ail_lock); |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 911 | if (tr && !list_empty(&tr->tr_ail1_list)) { |
| 912 | list_add(&tr->tr_list, &sdp->sd_ail1_list); |
| 913 | tr = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 914 | } |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 915 | spin_unlock(&sdp->sd_ail_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 916 | gfs2_log_unlock(sdp); |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 917 | |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 918 | if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) { |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 919 | if (!sdp->sd_log_idle) { |
| 920 | for (;;) { |
| 921 | gfs2_ail1_start(sdp); |
| 922 | gfs2_ail1_wait(sdp); |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 923 | if (gfs2_ail1_empty(sdp, 0)) |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 924 | break; |
| 925 | } |
Bob Peterson | 30fe70a | 2019-11-13 11:47:09 -0600 | [diff] [blame] | 926 | if (gfs2_withdrawn(sdp)) |
| 927 | goto out; |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 928 | atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */ |
| 929 | trace_gfs2_log_blocks(sdp, -1); |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 930 | log_write_header(sdp, flags); |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 931 | sdp->sd_log_head = sdp->sd_log_flush_head; |
| 932 | } |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 933 | if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN | |
| 934 | GFS2_LOG_HEAD_FLUSH_FREEZE)) |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 935 | gfs2_log_shutdown(sdp); |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 936 | if (flags & GFS2_LOG_HEAD_FLUSH_FREEZE) |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 937 | atomic_set(&sdp->sd_freeze_state, SFS_FROZEN); |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 938 | } |
| 939 | |
Bob Peterson | 30fe70a | 2019-11-13 11:47:09 -0600 | [diff] [blame] | 940 | out: |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame^] | 941 | if (gfs2_withdrawn(sdp)) { |
| 942 | ail_drain(sdp); /* frees all transactions */ |
| 943 | tr = NULL; |
| 944 | } |
| 945 | |
Bob Peterson | 805c0907 | 2018-01-08 10:34:17 -0500 | [diff] [blame] | 946 | trace_gfs2_log_flush(sdp, 0, flags); |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 947 | up_write(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 948 | |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 949 | kfree(tr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Steven Whitehouse | d69a3c6 | 2014-02-21 15:22:35 +0000 | [diff] [blame] | 952 | /** |
| 953 | * gfs2_merge_trans - Merge a new transaction into a cached transaction |
| 954 | * @old: Original transaction to be expanded |
| 955 | * @new: New transaction to be merged |
| 956 | */ |
| 957 | |
| 958 | static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new) |
| 959 | { |
Bob Peterson | 9862ca0 | 2017-01-25 12:50:47 -0500 | [diff] [blame] | 960 | WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags)); |
Steven Whitehouse | d69a3c6 | 2014-02-21 15:22:35 +0000 | [diff] [blame] | 961 | |
| 962 | old->tr_num_buf_new += new->tr_num_buf_new; |
| 963 | old->tr_num_databuf_new += new->tr_num_databuf_new; |
| 964 | old->tr_num_buf_rm += new->tr_num_buf_rm; |
| 965 | old->tr_num_databuf_rm += new->tr_num_databuf_rm; |
| 966 | old->tr_num_revoke += new->tr_num_revoke; |
Bob Peterson | a31b4ec | 2020-01-20 15:49:28 +0100 | [diff] [blame] | 967 | old->tr_num_revoke_rm += new->tr_num_revoke_rm; |
Steven Whitehouse | d69a3c6 | 2014-02-21 15:22:35 +0000 | [diff] [blame] | 968 | |
| 969 | list_splice_tail_init(&new->tr_databuf, &old->tr_databuf); |
| 970 | list_splice_tail_init(&new->tr_buf, &old->tr_buf); |
| 971 | } |
| 972 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 973 | static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
| 974 | { |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 975 | unsigned int reserved; |
Steven Whitehouse | ac39aad | 2008-01-10 14:49:43 +0000 | [diff] [blame] | 976 | unsigned int unused; |
Steven Whitehouse | 022ef4f | 2014-02-21 21:55:33 +0000 | [diff] [blame] | 977 | unsigned int maxres; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 978 | |
| 979 | gfs2_log_lock(sdp); |
| 980 | |
Steven Whitehouse | d69a3c6 | 2014-02-21 15:22:35 +0000 | [diff] [blame] | 981 | if (sdp->sd_log_tr) { |
| 982 | gfs2_merge_trans(sdp->sd_log_tr, tr); |
| 983 | } else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) { |
Bob Peterson | 9862ca0 | 2017-01-25 12:50:47 -0500 | [diff] [blame] | 984 | gfs2_assert_withdraw(sdp, test_bit(TR_ALLOCED, &tr->tr_flags)); |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 985 | sdp->sd_log_tr = tr; |
Bob Peterson | 9862ca0 | 2017-01-25 12:50:47 -0500 | [diff] [blame] | 986 | set_bit(TR_ATTACHED, &tr->tr_flags); |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 987 | } |
Steven Whitehouse | 022ef4f | 2014-02-21 21:55:33 +0000 | [diff] [blame] | 988 | |
Bob Peterson | a31b4ec | 2020-01-20 15:49:28 +0100 | [diff] [blame] | 989 | sdp->sd_log_committed_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm; |
Steven Whitehouse | 022ef4f | 2014-02-21 21:55:33 +0000 | [diff] [blame] | 990 | reserved = calc_reserved(sdp); |
| 991 | maxres = sdp->sd_log_blks_reserved + tr->tr_reserved; |
| 992 | gfs2_assert_withdraw(sdp, maxres >= reserved); |
| 993 | unused = maxres - reserved; |
| 994 | atomic_add(unused, &sdp->sd_log_blks_free); |
| 995 | trace_gfs2_log_blocks(sdp, unused); |
| 996 | gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= |
| 997 | sdp->sd_jdesc->jd_blocks); |
| 998 | sdp->sd_log_blks_reserved = reserved; |
| 999 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1000 | gfs2_log_unlock(sdp); |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * gfs2_log_commit - Commit a transaction to the log |
| 1005 | * @sdp: the filesystem |
| 1006 | * @tr: the transaction |
| 1007 | * |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1008 | * We wake up gfs2_logd if the number of pinned blocks exceed thresh1 |
| 1009 | * or the total number of used blocks (pinned blocks plus AIL blocks) |
| 1010 | * is greater than thresh2. |
| 1011 | * |
| 1012 | * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of |
| 1013 | * journal size. |
| 1014 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1015 | * Returns: errno |
| 1016 | */ |
| 1017 | |
| 1018 | void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
| 1019 | { |
| 1020 | log_refund(sdp, tr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1021 | |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1022 | if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) || |
| 1023 | ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) > |
| 1024 | atomic_read(&sdp->sd_log_thresh2))) |
| 1025 | wake_up(&sdp->sd_logd_waitq); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * gfs2_log_shutdown - write a shutdown header into a journal |
| 1030 | * @sdp: the filesystem |
| 1031 | * |
| 1032 | */ |
| 1033 | |
Bob Peterson | feed98a | 2019-11-14 09:48:26 -0500 | [diff] [blame] | 1034 | static void gfs2_log_shutdown(struct gfs2_sbd *sdp) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1035 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1036 | gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1037 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1038 | gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list)); |
| 1039 | |
| 1040 | sdp->sd_log_flush_head = sdp->sd_log_head; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1041 | |
Bob Peterson | 805c0907 | 2018-01-08 10:34:17 -0500 | [diff] [blame] | 1042 | log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1043 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 1044 | gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail); |
| 1045 | gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1046 | |
| 1047 | sdp->sd_log_head = sdp->sd_log_flush_head; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1048 | sdp->sd_log_tail = sdp->sd_log_head; |
Steven Whitehouse | a25311c | 2006-11-23 11:06:35 -0500 | [diff] [blame] | 1049 | } |
| 1050 | |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1051 | static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp) |
| 1052 | { |
Bob Peterson | f07b352 | 2017-01-05 16:01:45 -0500 | [diff] [blame] | 1053 | return (atomic_read(&sdp->sd_log_pinned) + |
| 1054 | atomic_read(&sdp->sd_log_blks_needed) >= |
| 1055 | atomic_read(&sdp->sd_log_thresh1)); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp) |
| 1059 | { |
| 1060 | unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free); |
Abhi Das | b066a4eeb | 2017-08-04 12:15:32 -0500 | [diff] [blame] | 1061 | |
| 1062 | if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags)) |
| 1063 | return 1; |
| 1064 | |
Bob Peterson | f07b352 | 2017-01-05 16:01:45 -0500 | [diff] [blame] | 1065 | return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >= |
| 1066 | atomic_read(&sdp->sd_log_thresh2); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1067 | } |
Steven Whitehouse | ec69b18 | 2007-11-09 10:01:41 +0000 | [diff] [blame] | 1068 | |
| 1069 | /** |
| 1070 | * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks |
| 1071 | * @sdp: Pointer to GFS2 superblock |
| 1072 | * |
| 1073 | * Also, periodically check to make sure that we're using the most recent |
| 1074 | * journal index. |
| 1075 | */ |
| 1076 | |
| 1077 | int gfs2_logd(void *data) |
| 1078 | { |
| 1079 | struct gfs2_sbd *sdp = data; |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1080 | unsigned long t = 1; |
| 1081 | DEFINE_WAIT(wait); |
Bob Peterson | b63f5e8 | 2017-01-06 22:14:28 -0500 | [diff] [blame] | 1082 | bool did_flush; |
Steven Whitehouse | ec69b18 | 2007-11-09 10:01:41 +0000 | [diff] [blame] | 1083 | |
| 1084 | while (!kthread_should_stop()) { |
Steven Whitehouse | ec69b18 | 2007-11-09 10:01:41 +0000 | [diff] [blame] | 1085 | |
Bob Peterson | 942b0cd | 2017-08-16 11:30:06 -0500 | [diff] [blame] | 1086 | /* Check for errors writing to the journal */ |
| 1087 | if (sdp->sd_log_error) { |
Andreas Gruenbacher | badb55e | 2020-01-23 18:41:00 +0100 | [diff] [blame] | 1088 | gfs2_lm(sdp, |
| 1089 | "GFS2: fsid=%s: error %d: " |
| 1090 | "withdrawing the file system to " |
| 1091 | "prevent further damage.\n", |
| 1092 | sdp->sd_fsname, sdp->sd_log_error); |
| 1093 | gfs2_withdraw(sdp); |
Bob Peterson | 942b0cd | 2017-08-16 11:30:06 -0500 | [diff] [blame] | 1094 | } |
| 1095 | |
Bob Peterson | b63f5e8 | 2017-01-06 22:14:28 -0500 | [diff] [blame] | 1096 | did_flush = false; |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1097 | if (gfs2_jrnl_flush_reqd(sdp) || t == 0) { |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 1098 | gfs2_ail1_empty(sdp, 0); |
Bob Peterson | 805c0907 | 2018-01-08 10:34:17 -0500 | [diff] [blame] | 1099 | gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL | |
| 1100 | GFS2_LFC_LOGD_JFLUSH_REQD); |
Bob Peterson | b63f5e8 | 2017-01-06 22:14:28 -0500 | [diff] [blame] | 1101 | did_flush = true; |
Steven Whitehouse | ec69b18 | 2007-11-09 10:01:41 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1104 | if (gfs2_ail_flush_reqd(sdp)) { |
| 1105 | gfs2_ail1_start(sdp); |
Steven Whitehouse | 26b06a6 | 2011-05-21 19:21:07 +0100 | [diff] [blame] | 1106 | gfs2_ail1_wait(sdp); |
Bob Peterson | 5e4c763 | 2019-02-21 14:28:07 -0700 | [diff] [blame] | 1107 | gfs2_ail1_empty(sdp, 0); |
Bob Peterson | 805c0907 | 2018-01-08 10:34:17 -0500 | [diff] [blame] | 1108 | gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL | |
| 1109 | GFS2_LFC_LOGD_AIL_FLUSH_REQD); |
Bob Peterson | b63f5e8 | 2017-01-06 22:14:28 -0500 | [diff] [blame] | 1110 | did_flush = true; |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1111 | } |
| 1112 | |
Bob Peterson | b63f5e8 | 2017-01-06 22:14:28 -0500 | [diff] [blame] | 1113 | if (!gfs2_ail_flush_reqd(sdp) || did_flush) |
Steven Whitehouse | 26b06a6 | 2011-05-21 19:21:07 +0100 | [diff] [blame] | 1114 | wake_up(&sdp->sd_log_waitq); |
| 1115 | |
Steven Whitehouse | ec69b18 | 2007-11-09 10:01:41 +0000 | [diff] [blame] | 1116 | t = gfs2_tune_get(sdp, gt_logd_secs) * HZ; |
Tejun Heo | a0acae0 | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 1117 | |
| 1118 | try_to_freeze(); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1119 | |
| 1120 | do { |
| 1121 | prepare_to_wait(&sdp->sd_logd_waitq, &wait, |
Steven Whitehouse | 5f48749 | 2010-09-09 14:45:00 +0100 | [diff] [blame] | 1122 | TASK_INTERRUPTIBLE); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1123 | if (!gfs2_ail_flush_reqd(sdp) && |
| 1124 | !gfs2_jrnl_flush_reqd(sdp) && |
| 1125 | !kthread_should_stop()) |
| 1126 | t = schedule_timeout(t); |
| 1127 | } while(t && !gfs2_ail_flush_reqd(sdp) && |
| 1128 | !gfs2_jrnl_flush_reqd(sdp) && |
| 1129 | !kthread_should_stop()); |
| 1130 | finish_wait(&sdp->sd_logd_waitq, &wait); |
Steven Whitehouse | ec69b18 | 2007-11-09 10:01:41 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |