blob: f0ee3ff6f9a876560e808b9c87b794d984ff6a6b [file] [log] [blame]
Thomas Gleixner7336d0e2019-05-31 01:09:56 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglandb3b94fa2006-01-16 16:50:04 +00002/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersonda6dd402007-12-11 18:49:21 -06004 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00005 */
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 Whitehouse5c676f62006-02-27 17:23:27 -050012#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050013#include <linux/crc32.h>
Bob Petersonc1696fb2018-01-17 00:01:33 +010014#include <linux/crc32c.h>
Steven Whitehousea25311c2006-11-23 11:06:35 -050015#include <linux/delay.h>
Steven Whitehouseec69b182007-11-09 10:01:41 +000016#include <linux/kthread.h>
17#include <linux/freezer.h>
Steven Whitehouse254db572008-09-26 10:23:22 +010018#include <linux/bio.h>
Steven Whitehouse885bcec2014-02-03 09:57:29 +000019#include <linux/blkdev.h>
Steven Whitehouse4667a0e2011-04-18 14:18:09 +010020#include <linux/writeback.h>
Bob Peterson4a36d082012-02-14 14:49:57 -050021#include <linux/list_sort.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022
23#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050024#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "bmap.h"
26#include "glock.h"
27#include "log.h"
28#include "lops.h"
29#include "meta_io.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050030#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050031#include "dir.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010032#include "trace_gfs2.h"
Bob Petersonb839dad2019-04-17 12:04:27 -060033#include "trans.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000034
Bob Petersonfeed98a2019-11-14 09:48:26 -050035static void gfs2_log_shutdown(struct gfs2_sbd *sdp);
36
David Teiglandb3b94fa2006-01-16 16:50:04 +000037/**
38 * gfs2_struct2blk - compute stuff
39 * @sdp: the filesystem
40 * @nstruct: the number of structures
David Teiglandb3b94fa2006-01-16 16:50:04 +000041 *
42 * Compute the number of log descriptor blocks needed to hold a certain number
43 * of structures of a certain size.
44 *
45 * Returns: the number of blocks needed (minimum is always 1)
46 */
47
Bob Peterson2e9eeaa2019-12-13 08:10:51 -060048unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct)
David Teiglandb3b94fa2006-01-16 16:50:04 +000049{
50 unsigned int blks;
51 unsigned int first, second;
52
Andreas Gruenbacher6188e872020-12-06 13:30:14 +010053 /* The initial struct gfs2_log_descriptor block */
David Teiglandb3b94fa2006-01-16 16:50:04 +000054 blks = 1;
Bob Peterson2e9eeaa2019-12-13 08:10:51 -060055 first = sdp->sd_ldptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +000056
57 if (nstruct > first) {
Andreas Gruenbacher6188e872020-12-06 13:30:14 +010058 /* Subsequent struct gfs2_meta_header blocks */
Bob Peterson2e9eeaa2019-12-13 08:10:51 -060059 second = sdp->sd_inptrs;
Steven Whitehouse5c676f62006-02-27 17:23:27 -050060 blks += DIV_ROUND_UP(nstruct - first, second);
David Teiglandb3b94fa2006-01-16 16:50:04 +000061 }
62
63 return blks;
64}
65
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040066/**
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010067 * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010068 * @bd: The gfs2_bufdata to remove
69 *
Steven Whitehousec618e872011-03-14 12:40:29 +000070 * The ail lock _must_ be held when calling this function
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010071 *
72 */
73
Bob Peterson68942872020-07-22 10:27:50 -050074void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010075{
Benjamin Marzinski16ca9412013-04-05 20:31:46 -050076 bd->bd_tr = NULL;
Steven Whitehouse1ad38c42007-09-03 11:01:33 +010077 list_del_init(&bd->bd_ail_st_list);
78 list_del_init(&bd->bd_ail_gl_list);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010079 atomic_dec(&bd->bd_gl->gl_ail_count);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010080 brelse(bd->bd_bh);
81}
82
83/**
Lee Jonesc551f662021-03-30 17:44:29 +010084 * gfs2_ail1_start_one - Start I/O on a transaction
85 * @sdp: The superblock
Steven Whitehouse4667a0e2011-04-18 14:18:09 +010086 * @wbc: The writeback control structure
Lee Jonesc551f662021-03-30 17:44:29 +010087 * @tr: The transaction to start I/O on
88 * @plug: The block plug currently active
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040089 */
90
Steven Whitehouse4f1de012011-04-26 10:23:56 +010091static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
92 struct writeback_control *wbc,
Bob Peterson17d77682021-02-18 09:07:21 -050093 struct gfs2_trans *tr, struct blk_plug *plug)
Dave Chinnerd6a079e2011-03-11 11:52:25 +000094__releases(&sdp->sd_ail_lock)
95__acquires(&sdp->sd_ail_lock)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040096{
Steven Whitehouse5ac048b2011-03-30 16:25:51 +010097 struct gfs2_glock *gl = NULL;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +010098 struct address_space *mapping;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040099 struct gfs2_bufdata *bd, *s;
100 struct buffer_head *bh;
Bob Petersonb1676cb2019-11-13 13:53:42 -0600101 int ret = 0;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400102
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500103 list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100104 bh = bd->bd_bh;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400105
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500106 gfs2_assert(sdp, bd->bd_tr == tr);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400107
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100108 if (!buffer_busy(bh)) {
Bob Peterson30fe70a2019-11-13 11:47:09 -0600109 if (buffer_uptodate(bh)) {
110 list_move(&bd->bd_ail_st_list,
111 &tr->tr_ail2_list);
112 continue;
113 }
Bob Peterson036330c2019-04-10 11:46:35 -0600114 if (!cmpxchg(&sdp->sd_log_error, 0, -EIO)) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100115 gfs2_io_error_bh(sdp, bh);
Bob Peterson69511082019-02-12 13:43:55 -0700116 gfs2_withdraw_delayed(sdp);
Andreas Gruenbacher9e1a9ec2018-06-07 11:56:46 +0100117 }
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400118 }
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100119
Bob Peterson30fe70a2019-11-13 11:47:09 -0600120 if (gfs2_withdrawn(sdp)) {
121 gfs2_remove_from_ail(bd);
122 continue;
123 }
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100124 if (!buffer_dirty(bh))
125 continue;
126 if (gl == bd->bd_gl)
127 continue;
128 gl = bd->bd_gl;
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500129 list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100130 mapping = bh->b_page->mapping;
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100131 if (!mapping)
132 continue;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100133 spin_unlock(&sdp->sd_ail_lock);
Bob Petersonb1676cb2019-11-13 13:53:42 -0600134 ret = generic_writepages(mapping, wbc);
Bob Peterson17d77682021-02-18 09:07:21 -0500135 if (need_resched()) {
136 blk_finish_plug(plug);
137 cond_resched();
138 blk_start_plug(plug);
139 }
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100140 spin_lock(&sdp->sd_ail_lock);
Bob Peterson4e79e3f2020-11-12 10:02:48 -0600141 if (ret == -ENODATA) /* if a jdata write into a new hole */
142 ret = 0; /* ignore it */
Bob Petersonb1676cb2019-11-13 13:53:42 -0600143 if (ret || wbc->nr_to_write <= 0)
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100144 break;
Bob Petersonb1676cb2019-11-13 13:53:42 -0600145 return -EBUSY;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100146 }
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100147
Bob Petersonb1676cb2019-11-13 13:53:42 -0600148 return ret;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100149}
150
Bob Peterson9592ea82020-03-26 12:19:54 -0500151static void dump_ail_list(struct gfs2_sbd *sdp)
152{
153 struct gfs2_trans *tr;
154 struct gfs2_bufdata *bd;
155 struct buffer_head *bh;
156
Bob Peterson9592ea82020-03-26 12:19:54 -0500157 list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
158 list_for_each_entry_reverse(bd, &tr->tr_ail1_list,
159 bd_ail_st_list) {
160 bh = bd->bd_bh;
161 fs_err(sdp, "bd %p: blk:0x%llx bh=%p ", bd,
162 (unsigned long long)bd->bd_blkno, bh);
163 if (!bh) {
164 fs_err(sdp, "\n");
165 continue;
166 }
167 fs_err(sdp, "0x%llx up2:%d dirt:%d lkd:%d req:%d "
168 "map:%d new:%d ar:%d aw:%d delay:%d "
169 "io err:%d unwritten:%d dfr:%d pin:%d esc:%d\n",
170 (unsigned long long)bh->b_blocknr,
171 buffer_uptodate(bh), buffer_dirty(bh),
172 buffer_locked(bh), buffer_req(bh),
173 buffer_mapped(bh), buffer_new(bh),
174 buffer_async_read(bh), buffer_async_write(bh),
175 buffer_delay(bh), buffer_write_io_error(bh),
176 buffer_unwritten(bh),
177 buffer_defer_completion(bh),
178 buffer_pinned(bh), buffer_escaped(bh));
179 }
180 }
181}
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100182
183/**
184 * gfs2_ail1_flush - start writeback of some ail1 entries
185 * @sdp: The super block
186 * @wbc: The writeback control structure
187 *
188 * Writes back some ail1 entries, according to the limits in the
189 * writeback control structure
190 */
191
192void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
193{
194 struct list_head *head = &sdp->sd_ail1_list;
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500195 struct gfs2_trans *tr;
Steven Whitehouse885bcec2014-02-03 09:57:29 +0000196 struct blk_plug plug;
Bob Peterson75b46c42020-03-27 15:23:14 -0500197 int ret;
Bob Peterson9592ea82020-03-26 12:19:54 -0500198 unsigned long flush_start = jiffies;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100199
Steven Whitehousec83ae9c2011-04-18 14:18:38 +0100200 trace_gfs2_ail_flush(sdp, wbc, 1);
Steven Whitehouse885bcec2014-02-03 09:57:29 +0000201 blk_start_plug(&plug);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100202 spin_lock(&sdp->sd_ail_lock);
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100203restart:
Bob Peterson75b46c42020-03-27 15:23:14 -0500204 ret = 0;
Bob Peterson9592ea82020-03-26 12:19:54 -0500205 if (time_after(jiffies, flush_start + (HZ * 600))) {
Bob Petersond5dc3d92020-05-22 14:03:21 -0500206 fs_err(sdp, "Error: In %s for ten minutes! t=%d\n",
207 __func__, current->journal_info ? 1 : 0);
Bob Peterson9592ea82020-03-26 12:19:54 -0500208 dump_ail_list(sdp);
209 goto out;
210 }
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500211 list_for_each_entry_reverse(tr, head, tr_list) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100212 if (wbc->nr_to_write <= 0)
213 break;
Bob Peterson17d77682021-02-18 09:07:21 -0500214 ret = gfs2_ail1_start_one(sdp, wbc, tr, &plug);
Bob Petersonb1676cb2019-11-13 13:53:42 -0600215 if (ret) {
216 if (ret == -EBUSY)
217 goto restart;
218 break;
219 }
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100220 }
Bob Peterson9592ea82020-03-26 12:19:54 -0500221out:
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100222 spin_unlock(&sdp->sd_ail_lock);
Steven Whitehouse885bcec2014-02-03 09:57:29 +0000223 blk_finish_plug(&plug);
Bob Peterson49003122020-03-06 10:15:03 -0600224 if (ret) {
225 gfs2_lm(sdp, "gfs2_ail1_start_one (generic_writepages) "
226 "returned: %d\n", ret);
Andreas Gruenbacherbadb55e2020-01-23 18:41:00 +0100227 gfs2_withdraw(sdp);
Bob Peterson49003122020-03-06 10:15:03 -0600228 }
Steven Whitehousec83ae9c2011-04-18 14:18:38 +0100229 trace_gfs2_ail_flush(sdp, wbc, 0);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100230}
231
232/**
233 * gfs2_ail1_start - start writeback of all ail1 entries
234 * @sdp: The superblock
235 */
236
237static void gfs2_ail1_start(struct gfs2_sbd *sdp)
238{
239 struct writeback_control wbc = {
240 .sync_mode = WB_SYNC_NONE,
241 .nr_to_write = LONG_MAX,
242 .range_start = 0,
243 .range_end = LLONG_MAX,
244 };
245
246 return gfs2_ail1_flush(sdp, &wbc);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400247}
248
Andreas Gruenbacher5cb738b2020-12-19 07:15:17 +0100249static void gfs2_log_update_flush_tail(struct gfs2_sbd *sdp)
250{
251 unsigned int new_flush_tail = sdp->sd_log_head;
252 struct gfs2_trans *tr;
253
254 if (!list_empty(&sdp->sd_ail1_list)) {
255 tr = list_last_entry(&sdp->sd_ail1_list,
256 struct gfs2_trans, tr_list);
257 new_flush_tail = tr->tr_first;
258 }
259 sdp->sd_log_flush_tail = new_flush_tail;
260}
261
262static void gfs2_log_update_head(struct gfs2_sbd *sdp)
263{
264 unsigned int new_head = sdp->sd_log_flush_head;
265
266 if (sdp->sd_log_flush_tail == sdp->sd_log_head)
267 sdp->sd_log_flush_tail = new_head;
268 sdp->sd_log_head = new_head;
269}
270
Lee Jonesc551f662021-03-30 17:44:29 +0100271/*
Andreas Gruenbacher76fce652020-12-19 10:54:51 +0100272 * gfs2_ail_empty_tr - empty one of the ail lists of a transaction
273 */
274
275static void gfs2_ail_empty_tr(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
276 struct list_head *head)
277{
278 struct gfs2_bufdata *bd;
279
280 while (!list_empty(head)) {
281 bd = list_first_entry(head, struct gfs2_bufdata,
282 bd_ail_st_list);
283 gfs2_assert(sdp, bd->bd_tr == tr);
284 gfs2_remove_from_ail(bd);
285 }
286}
287
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400288/**
289 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
290 * @sdp: the filesystem
Bob Peterson5e4c7632019-02-21 14:28:07 -0700291 * @tr: the transaction
292 * @max_revokes: If nonzero, issue revokes for the bd items for written buffers
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400293 *
Bob Peterson36c78302020-08-18 08:05:14 -0500294 * returns: the transaction's count of remaining active items
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400295 */
296
Bob Peterson36c78302020-08-18 08:05:14 -0500297static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
Bob Peterson5e4c7632019-02-21 14:28:07 -0700298 int *max_revokes)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400299{
300 struct gfs2_bufdata *bd, *s;
301 struct buffer_head *bh;
Bob Peterson36c78302020-08-18 08:05:14 -0500302 int active_count = 0;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400303
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500304 list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400305 bd_ail_st_list) {
306 bh = bd->bd_bh;
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500307 gfs2_assert(sdp, bd->bd_tr == tr);
Bob Peterson036330c2019-04-10 11:46:35 -0600308 /*
309 * If another process flagged an io error, e.g. writing to the
310 * journal, error all other bhs and move them off the ail1 to
311 * prevent a tight loop when unmount tries to flush ail1,
312 * regardless of whether they're still busy. If no outside
313 * errors were found and the buffer is busy, move to the next.
314 * If the ail buffer is not busy and caught an error, flag it
315 * for others.
316 */
Bob Peterson36c78302020-08-18 08:05:14 -0500317 if (!sdp->sd_log_error && buffer_busy(bh)) {
318 active_count++;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100319 continue;
Bob Peterson36c78302020-08-18 08:05:14 -0500320 }
Bob Petersonb524abc2018-10-04 10:21:07 -0500321 if (!buffer_uptodate(bh) &&
Bob Peterson036330c2019-04-10 11:46:35 -0600322 !cmpxchg(&sdp->sd_log_error, 0, -EIO)) {
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400323 gfs2_io_error_bh(sdp, bh);
Bob Peterson69511082019-02-12 13:43:55 -0700324 gfs2_withdraw_delayed(sdp);
Andreas Gruenbacher9e1a9ec2018-06-07 11:56:46 +0100325 }
Bob Peterson5e4c7632019-02-21 14:28:07 -0700326 /*
327 * If we have space for revokes and the bd is no longer on any
328 * buf list, we can just add a revoke for it immediately and
329 * avoid having to put it on the ail2 list, where it would need
330 * to be revoked later.
331 */
332 if (*max_revokes && list_empty(&bd->bd_list)) {
333 gfs2_add_revoke(sdp, bd);
334 (*max_revokes)--;
335 continue;
336 }
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500337 list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400338 }
Bob Peterson36c78302020-08-18 08:05:14 -0500339 return active_count;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400340}
341
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100342/**
343 * gfs2_ail1_empty - Try to empty the ail1 lists
344 * @sdp: The superblock
Bob Peterson5e4c7632019-02-21 14:28:07 -0700345 * @max_revokes: If non-zero, add revokes where appropriate
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100346 *
347 * Tries to empty the ail1 lists, starting with the oldest first
348 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000349
Bob Peterson5e4c7632019-02-21 14:28:07 -0700350static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int max_revokes)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351{
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500352 struct gfs2_trans *tr, *s;
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500353 int oldest_tr = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 int ret;
355
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000356 spin_lock(&sdp->sd_ail_lock);
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500357 list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
Bob Peterson36c78302020-08-18 08:05:14 -0500358 if (!gfs2_ail1_empty_one(sdp, tr, &max_revokes) && oldest_tr)
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500359 list_move(&tr->tr_list, &sdp->sd_ail2_list);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100360 else
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500361 oldest_tr = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362 }
Andreas Gruenbacher5cb738b2020-12-19 07:15:17 +0100363 gfs2_log_update_flush_tail(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000364 ret = list_empty(&sdp->sd_ail1_list);
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000365 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366
Bob Peterson69511082019-02-12 13:43:55 -0700367 if (test_bit(SDF_WITHDRAWING, &sdp->sd_flags)) {
Andreas Gruenbacherbadb55e2020-01-23 18:41:00 +0100368 gfs2_lm(sdp, "fatal: I/O error(s)\n");
369 gfs2_withdraw(sdp);
370 }
Andreas Gruenbacher9e1a9ec2018-06-07 11:56:46 +0100371
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372 return ret;
373}
374
Steven Whitehouse26b06a62011-05-21 19:21:07 +0100375static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
376{
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500377 struct gfs2_trans *tr;
Steven Whitehouse26b06a62011-05-21 19:21:07 +0100378 struct gfs2_bufdata *bd;
379 struct buffer_head *bh;
380
381 spin_lock(&sdp->sd_ail_lock);
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500382 list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
383 list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
Steven Whitehouse26b06a62011-05-21 19:21:07 +0100384 bh = bd->bd_bh;
385 if (!buffer_locked(bh))
386 continue;
387 get_bh(bh);
388 spin_unlock(&sdp->sd_ail_lock);
389 wait_on_buffer(bh);
390 brelse(bh);
391 return;
392 }
393 }
394 spin_unlock(&sdp->sd_ail_lock);
395}
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400396
Andreas Gruenbacher6e806742020-12-11 14:45:23 +0100397static void __ail2_empty(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400398{
Andreas Gruenbacher6e806742020-12-11 14:45:23 +0100399 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
400 list_del(&tr->tr_list);
401 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
402 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
403 gfs2_trans_free(sdp, tr);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400404}
405
David Teiglandb3b94fa2006-01-16 16:50:04 +0000406static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
407{
Andreas Gruenbacher6e806742020-12-11 14:45:23 +0100408 struct list_head *ail2_list = &sdp->sd_ail2_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409 unsigned int old_tail = sdp->sd_log_tail;
Andreas Gruenbacher6e806742020-12-11 14:45:23 +0100410 struct gfs2_trans *tr, *safe;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000412 spin_lock(&sdp->sd_ail_lock);
Andreas Gruenbacher6e806742020-12-11 14:45:23 +0100413 if (old_tail <= new_tail) {
414 list_for_each_entry_safe(tr, safe, ail2_list, tr_list) {
415 if (old_tail <= tr->tr_first && tr->tr_first < new_tail)
416 __ail2_empty(sdp, tr);
417 }
418 } else {
419 list_for_each_entry_safe(tr, safe, ail2_list, tr_list) {
420 if (old_tail <= tr->tr_first || tr->tr_first < new_tail)
421 __ail2_empty(sdp, tr);
422 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 }
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000424 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425}
426
427/**
Andreas Gruenbacherf3708fb2020-12-13 09:21:34 +0100428 * gfs2_log_is_empty - Check if the log is empty
429 * @sdp: The GFS2 superblock
430 */
431
432bool gfs2_log_is_empty(struct gfs2_sbd *sdp) {
433 return atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks;
434}
435
Andreas Gruenbacher2129b422020-12-17 16:14:30 +0100436static bool __gfs2_log_try_reserve_revokes(struct gfs2_sbd *sdp, unsigned int revokes)
437{
438 unsigned int available;
439
440 available = atomic_read(&sdp->sd_log_revokes_available);
441 while (available >= revokes) {
442 if (atomic_try_cmpxchg(&sdp->sd_log_revokes_available,
443 &available, available - revokes))
444 return true;
445 }
446 return false;
447}
448
449/**
450 * gfs2_log_release_revokes - Release a given number of revokes
451 * @sdp: The GFS2 superblock
452 * @revokes: The number of revokes to release
453 *
454 * sdp->sd_log_flush_lock must be held.
455 */
456void gfs2_log_release_revokes(struct gfs2_sbd *sdp, unsigned int revokes)
457{
458 if (revokes)
459 atomic_add(revokes, &sdp->sd_log_revokes_available);
460}
461
Andreas Gruenbacherf3708fb2020-12-13 09:21:34 +0100462/**
Benjamin Marzinski24972552014-05-01 22:26:55 -0500463 * gfs2_log_release - Release a given number of log blocks
464 * @sdp: The GFS2 superblock
465 * @blks: The number of blocks
466 *
467 */
468
469void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
470{
Benjamin Marzinski24972552014-05-01 22:26:55 -0500471 atomic_add(blks, &sdp->sd_log_blks_free);
472 trace_gfs2_log_blocks(sdp, blks);
473 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
474 sdp->sd_jdesc->jd_blocks);
Andreas Gruenbacher5ae8fff2020-12-13 11:37:17 +0100475 if (atomic_read(&sdp->sd_log_blks_needed))
476 wake_up(&sdp->sd_log_waitq);
Benjamin Marzinski24972552014-05-01 22:26:55 -0500477}
478
479/**
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100480 * __gfs2_log_try_reserve - Try to make a log reservation
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481 * @sdp: The GFS2 superblock
482 * @blks: The number of blocks to reserve
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100483 * @taboo_blks: The number of blocks to leave free
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484 *
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100485 * Try to do the same as __gfs2_log_reserve(), but fail if no more log
486 * space is immediately available.
487 */
488static bool __gfs2_log_try_reserve(struct gfs2_sbd *sdp, unsigned int blks,
489 unsigned int taboo_blks)
490{
491 unsigned wanted = blks + taboo_blks;
492 unsigned int free_blocks;
493
494 free_blocks = atomic_read(&sdp->sd_log_blks_free);
495 while (free_blocks >= wanted) {
496 if (atomic_try_cmpxchg(&sdp->sd_log_blks_free, &free_blocks,
497 free_blocks - blks)) {
498 trace_gfs2_log_blocks(sdp, -blks);
499 return true;
500 }
501 }
502 return false;
503}
504
505/**
506 * __gfs2_log_reserve - Make a log reservation
507 * @sdp: The GFS2 superblock
508 * @blks: The number of blocks to reserve
509 * @taboo_blks: The number of blocks to leave free
510 *
511 * @taboo_blks is set to 0 for logd, and to GFS2_LOG_FLUSH_MIN_BLOCKS
512 * for all other processes. This ensures that when the log is almost full,
513 * logd will still be able to call gfs2_log_flush one more time without
514 * blocking, which will advance the tail and make some more log space
515 * available.
Steven Whitehouseb0041572006-11-23 10:51:34 -0500516 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500517 * We no longer flush the log here, instead we wake up logd to do that
518 * for us. To avoid the thundering herd and to ensure that we deal fairly
519 * with queued waiters, we use an exclusive wait. This means that when we
520 * get woken with enough journal space to get our reservation, we need to
521 * wake the next waiter on the list.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522 */
523
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100524static void __gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks,
525 unsigned int taboo_blks)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526{
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100527 unsigned wanted = blks + taboo_blks;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500528 unsigned int free_blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529
Bob Petersonf07b3522017-01-05 16:01:45 -0500530 atomic_add(blks, &sdp->sd_log_blks_needed);
Andreas Gruenbacher5ae8fff2020-12-13 11:37:17 +0100531 for (;;) {
532 if (current != sdp->sd_logd_process)
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500533 wake_up(&sdp->sd_logd_waitq);
Andreas Gruenbacher5ae8fff2020-12-13 11:37:17 +0100534 io_wait_event(sdp->sd_log_waitq,
535 (free_blocks = atomic_read(&sdp->sd_log_blks_free),
536 free_blocks >= wanted));
537 do {
538 if (atomic_try_cmpxchg(&sdp->sd_log_blks_free,
539 &free_blocks,
540 free_blocks - blks))
541 goto reserved;
542 } while (free_blocks >= wanted);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 }
Andreas Gruenbacher5ae8fff2020-12-13 11:37:17 +0100544
545reserved:
Steven Whitehouse63997772009-06-12 08:49:20 +0100546 trace_gfs2_log_blocks(sdp, -blks);
Andreas Gruenbacher5ae8fff2020-12-13 11:37:17 +0100547 if (atomic_sub_return(blks, &sdp->sd_log_blks_needed))
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500548 wake_up(&sdp->sd_log_waitq);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551/**
Andreas Gruenbacher2129b422020-12-17 16:14:30 +0100552 * gfs2_log_try_reserve - Try to make a log reservation
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100553 * @sdp: The GFS2 superblock
Andreas Gruenbacher2129b422020-12-17 16:14:30 +0100554 * @tr: The transaction
555 * @extra_revokes: The number of additional revokes reserved (output)
556 *
557 * This is similar to gfs2_log_reserve, but sdp->sd_log_flush_lock must be
558 * held for correct revoke accounting.
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100559 */
560
Andreas Gruenbacher2129b422020-12-17 16:14:30 +0100561bool gfs2_log_try_reserve(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
562 unsigned int *extra_revokes)
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100563{
Andreas Gruenbacher2129b422020-12-17 16:14:30 +0100564 unsigned int blks = tr->tr_reserved;
565 unsigned int revokes = tr->tr_revokes;
566 unsigned int revoke_blks = 0;
567
568 *extra_revokes = 0;
569 if (revokes && !__gfs2_log_try_reserve_revokes(sdp, revokes)) {
570 revoke_blks = DIV_ROUND_UP(revokes, sdp->sd_inptrs);
571 *extra_revokes = revoke_blks * sdp->sd_inptrs - revokes;
572 blks += revoke_blks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573 }
Andreas Gruenbacher2129b422020-12-17 16:14:30 +0100574 if (!blks)
575 return true;
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100576 if (__gfs2_log_try_reserve(sdp, blks, GFS2_LOG_FLUSH_MIN_BLOCKS))
Andreas Gruenbacher2129b422020-12-17 16:14:30 +0100577 return true;
578 if (!revoke_blks)
579 gfs2_log_release_revokes(sdp, revokes);
580 return false;
581}
582
583/**
584 * gfs2_log_reserve - Make a log reservation
585 * @sdp: The GFS2 superblock
586 * @tr: The transaction
587 * @extra_revokes: The number of additional revokes reserved (output)
588 *
589 * sdp->sd_log_flush_lock must not be held.
590 */
591
592void gfs2_log_reserve(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
593 unsigned int *extra_revokes)
594{
595 unsigned int blks = tr->tr_reserved;
596 unsigned int revokes = tr->tr_revokes;
Bob Petersondc7674e2021-06-30 12:49:59 -0500597 unsigned int revoke_blks;
Andreas Gruenbacher2129b422020-12-17 16:14:30 +0100598
599 *extra_revokes = 0;
600 if (revokes) {
601 revoke_blks = DIV_ROUND_UP(revokes, sdp->sd_inptrs);
602 *extra_revokes = revoke_blks * sdp->sd_inptrs - revokes;
603 blks += revoke_blks;
604 }
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100605 __gfs2_log_reserve(sdp, blks, GFS2_LOG_FLUSH_MIN_BLOCKS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606}
607
608/**
609 * log_distance - Compute distance between two journal blocks
610 * @sdp: The GFS2 superblock
611 * @newer: The most recent journal block of the pair
612 * @older: The older journal block of the pair
613 *
614 * Compute the distance (in the journal direction) between two
615 * blocks in the journal
616 *
617 * Returns: the distance in blocks
618 */
619
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400620static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621 unsigned int older)
622{
623 int dist;
624
625 dist = newer - older;
626 if (dist < 0)
627 dist += sdp->sd_jdesc->jd_blocks;
628
629 return dist;
630}
631
Robert Peterson2332c442007-06-18 14:50:20 -0500632/**
Andreas Gruenbacher6188e872020-12-06 13:30:14 +0100633 * calc_reserved - Calculate the number of blocks to keep reserved
Robert Peterson2332c442007-06-18 14:50:20 -0500634 * @sdp: The GFS2 superblock
635 *
636 * This is complex. We need to reserve room for all our currently used
Andreas Gruenbacher6188e872020-12-06 13:30:14 +0100637 * metadata blocks (e.g. normal file I/O rewriting file time stamps) and
638 * all our journaled data blocks for journaled files (e.g. files in the
Robert Peterson2332c442007-06-18 14:50:20 -0500639 * meta_fs like rindex, or files for which chattr +j was done.)
Andreas Gruenbacher6188e872020-12-06 13:30:14 +0100640 * If we don't reserve enough space, corruption will follow.
Robert Peterson2332c442007-06-18 14:50:20 -0500641 *
Andreas Gruenbacher6188e872020-12-06 13:30:14 +0100642 * We can have metadata blocks and jdata blocks in the same journal. Each
643 * type gets its own log descriptor, for which we need to reserve a block.
644 * In fact, each type has the potential for needing more than one log descriptor
645 * in cases where we have more blocks than will fit in a log descriptor.
Robert Peterson2332c442007-06-18 14:50:20 -0500646 * Metadata journal entries take up half the space of journaled buffer entries.
Robert Peterson2332c442007-06-18 14:50:20 -0500647 *
648 * Also, we need to reserve blocks for revoke journal entries and one for an
649 * overall header for the lot.
650 *
651 * Returns: the number of blocks reserved
652 */
653static unsigned int calc_reserved(struct gfs2_sbd *sdp)
654{
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100655 unsigned int reserved = GFS2_LOG_FLUSH_MIN_BLOCKS;
Andreas Gruenbacher71b219f2021-01-29 19:56:37 +0100656 unsigned int blocks;
Steven Whitehouse022ef4f2014-02-21 21:55:33 +0000657 struct gfs2_trans *tr = sdp->sd_log_tr;
Robert Peterson2332c442007-06-18 14:50:20 -0500658
Steven Whitehouse022ef4f2014-02-21 21:55:33 +0000659 if (tr) {
Andreas Gruenbacher71b219f2021-01-29 19:56:37 +0100660 blocks = tr->tr_num_buf_new - tr->tr_num_buf_rm;
661 reserved += blocks + DIV_ROUND_UP(blocks, buf_limit(sdp));
662 blocks = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
663 reserved += blocks + DIV_ROUND_UP(blocks, databuf_limit(sdp));
Steven Whitehouse022ef4f2014-02-21 21:55:33 +0000664 }
Robert Peterson2332c442007-06-18 14:50:20 -0500665 return reserved;
666}
667
Andreas Gruenbacher5cb738b2020-12-19 07:15:17 +0100668static void log_pull_tail(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669{
Andreas Gruenbacher5cb738b2020-12-19 07:15:17 +0100670 unsigned int new_tail = sdp->sd_log_flush_tail;
671 unsigned int dist;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672
Andreas Gruenbacher5cb738b2020-12-19 07:15:17 +0100673 if (new_tail == sdp->sd_log_tail)
674 return;
675 dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000676 ail2_empty(sdp, new_tail);
Andreas Gruenbacherc1eba1b2020-12-12 23:30:22 +0100677 gfs2_log_release(sdp, dist);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 sdp->sd_log_tail = new_tail;
679}
680
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681
Bob Peterson9ff78282019-11-13 13:47:02 -0600682void log_flush_wait(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683{
Steven Whitehouse16615be2007-09-17 10:59:52 +0100684 DEFINE_WAIT(wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685
Steven Whitehouse16615be2007-09-17 10:59:52 +0100686 if (atomic_read(&sdp->sd_log_in_flight)) {
687 do {
688 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
689 TASK_UNINTERRUPTIBLE);
690 if (atomic_read(&sdp->sd_log_in_flight))
691 io_schedule();
692 } while(atomic_read(&sdp->sd_log_in_flight));
693 finish_wait(&sdp->sd_log_flush_wait, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695}
696
Sami Tolvanen4f0f5862021-04-08 11:28:34 -0700697static int ip_cmp(void *priv, const struct list_head *a, const struct list_head *b)
Bob Peterson4a36d082012-02-14 14:49:57 -0500698{
Steven Whitehouse45138992013-01-28 09:30:07 +0000699 struct gfs2_inode *ipa, *ipb;
Bob Peterson4a36d082012-02-14 14:49:57 -0500700
Steven Whitehouse45138992013-01-28 09:30:07 +0000701 ipa = list_entry(a, struct gfs2_inode, i_ordered);
702 ipb = list_entry(b, struct gfs2_inode, i_ordered);
Bob Peterson4a36d082012-02-14 14:49:57 -0500703
Steven Whitehouse45138992013-01-28 09:30:07 +0000704 if (ipa->i_no_addr < ipb->i_no_addr)
Bob Peterson4a36d082012-02-14 14:49:57 -0500705 return -1;
Steven Whitehouse45138992013-01-28 09:30:07 +0000706 if (ipa->i_no_addr > ipb->i_no_addr)
Bob Peterson4a36d082012-02-14 14:49:57 -0500707 return 1;
708 return 0;
709}
710
Bob Peterson75424862020-06-17 07:47:34 -0500711static void __ordered_del_inode(struct gfs2_inode *ip)
712{
713 if (!list_empty(&ip->i_ordered))
714 list_del_init(&ip->i_ordered);
715}
716
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100717static void gfs2_ordered_write(struct gfs2_sbd *sdp)
718{
Steven Whitehouse45138992013-01-28 09:30:07 +0000719 struct gfs2_inode *ip;
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100720 LIST_HEAD(written);
721
Steven Whitehouse45138992013-01-28 09:30:07 +0000722 spin_lock(&sdp->sd_ordered_lock);
Andreas Gruenbachera5b1d3f2019-04-05 12:16:14 +0100723 list_sort(NULL, &sdp->sd_log_ordered, &ip_cmp);
724 while (!list_empty(&sdp->sd_log_ordered)) {
Andreas Gruenbacher969183b2020-02-03 19:22:45 +0100725 ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
Abhi Das1f23bc72017-12-22 07:55:31 -0600726 if (ip->i_inode.i_mapping->nrpages == 0) {
Bob Peterson75424862020-06-17 07:47:34 -0500727 __ordered_del_inode(ip);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100728 continue;
Abhi Das1f23bc72017-12-22 07:55:31 -0600729 }
730 list_move(&ip->i_ordered, &written);
Steven Whitehouse45138992013-01-28 09:30:07 +0000731 spin_unlock(&sdp->sd_ordered_lock);
732 filemap_fdatawrite(ip->i_inode.i_mapping);
733 spin_lock(&sdp->sd_ordered_lock);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100734 }
Andreas Gruenbachera5b1d3f2019-04-05 12:16:14 +0100735 list_splice(&written, &sdp->sd_log_ordered);
Steven Whitehouse45138992013-01-28 09:30:07 +0000736 spin_unlock(&sdp->sd_ordered_lock);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100737}
738
739static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
740{
Steven Whitehouse45138992013-01-28 09:30:07 +0000741 struct gfs2_inode *ip;
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100742
Steven Whitehouse45138992013-01-28 09:30:07 +0000743 spin_lock(&sdp->sd_ordered_lock);
Andreas Gruenbachera5b1d3f2019-04-05 12:16:14 +0100744 while (!list_empty(&sdp->sd_log_ordered)) {
Andreas Gruenbacher969183b2020-02-03 19:22:45 +0100745 ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
Bob Peterson75424862020-06-17 07:47:34 -0500746 __ordered_del_inode(ip);
Steven Whitehouse45138992013-01-28 09:30:07 +0000747 if (ip->i_inode.i_mapping->nrpages == 0)
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100748 continue;
Steven Whitehouse45138992013-01-28 09:30:07 +0000749 spin_unlock(&sdp->sd_ordered_lock);
750 filemap_fdatawait(ip->i_inode.i_mapping);
751 spin_lock(&sdp->sd_ordered_lock);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100752 }
Steven Whitehouse45138992013-01-28 09:30:07 +0000753 spin_unlock(&sdp->sd_ordered_lock);
754}
755
756void gfs2_ordered_del_inode(struct gfs2_inode *ip)
757{
758 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
759
760 spin_lock(&sdp->sd_ordered_lock);
Bob Peterson75424862020-06-17 07:47:34 -0500761 __ordered_del_inode(ip);
Steven Whitehouse45138992013-01-28 09:30:07 +0000762 spin_unlock(&sdp->sd_ordered_lock);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100763}
764
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500765void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
766{
767 struct buffer_head *bh = bd->bd_bh;
768 struct gfs2_glock *gl = bd->bd_gl;
769
Andreas Gruenbacherf4e2f5e2020-05-05 11:56:46 -0500770 sdp->sd_log_num_revoke++;
771 if (atomic_inc_return(&gl->gl_revokes) == 1)
772 gfs2_glock_hold(gl);
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500773 bh->b_private = NULL;
774 bd->bd_blkno = bh->b_blocknr;
Bob Peterson9290a9a2013-12-10 12:06:35 -0500775 gfs2_remove_from_ail(bd); /* drops ref on bh */
776 bd->bd_bh = NULL;
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500777 set_bit(GLF_LFLUSH, &gl->gl_flags);
Andreas Gruenbachera5b1d3f2019-04-05 12:16:14 +0100778 list_add(&bd->bd_list, &sdp->sd_log_revokes);
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500779}
780
Bob Petersonfe5e7ba2019-11-14 09:49:11 -0500781void gfs2_glock_remove_revoke(struct gfs2_glock *gl)
782{
783 if (atomic_dec_return(&gl->gl_revokes) == 0) {
784 clear_bit(GLF_LFLUSH, &gl->gl_flags);
785 gfs2_glock_queue_put(gl);
786 }
787}
788
Bob Peterson5e4c7632019-02-21 14:28:07 -0700789/**
Andreas Gruenbachere7501bf2020-12-19 03:11:51 +0100790 * gfs2_flush_revokes - Add as many revokes to the system transaction as we can
Bob Peterson5e4c7632019-02-21 14:28:07 -0700791 * @sdp: The GFS2 superblock
792 *
793 * Our usual strategy is to defer writing revokes as much as we can in the hope
794 * that we'll eventually overwrite the journal, which will make those revokes
795 * go away. This changes when we flush the log: at that point, there will
796 * likely be some left-over space in the last revoke block of that transaction.
797 * We can fill that space with additional revokes for blocks that have already
798 * been written back. This will basically come at no cost now, and will save
799 * us from having to keep track of those blocks on the AIL2 list later.
800 */
Andreas Gruenbachere7501bf2020-12-19 03:11:51 +0100801void gfs2_flush_revokes(struct gfs2_sbd *sdp)
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500802{
Bob Peterson5e4c7632019-02-21 14:28:07 -0700803 /* number of revokes we still have room for */
Andreas Gruenbacher2129b422020-12-17 16:14:30 +0100804 unsigned int max_revokes = atomic_read(&sdp->sd_log_revokes_available);
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500805
Bob Peterson5e4c7632019-02-21 14:28:07 -0700806 gfs2_log_lock(sdp);
Bob Peterson5e4c7632019-02-21 14:28:07 -0700807 gfs2_ail1_empty(sdp, max_revokes);
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500808 gfs2_log_unlock(sdp);
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500809}
810
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811/**
Bob Peterson7c70b892019-03-25 09:34:19 -0600812 * gfs2_write_log_header - Write a journal log header buffer at lblock
Bob Peterson588bff92017-12-18 12:48:29 -0600813 * @sdp: The GFS2 superblock
Bob Petersonc1696fb2018-01-17 00:01:33 +0100814 * @jd: journal descriptor of the journal to which we are writing
Bob Peterson588bff92017-12-18 12:48:29 -0600815 * @seq: sequence number
816 * @tail: tail of the log
Bob Peterson7c70b892019-03-25 09:34:19 -0600817 * @lblock: value for lh_blkno (block number relative to start of journal)
Bob Petersonc1696fb2018-01-17 00:01:33 +0100818 * @flags: log header flags GFS2_LOG_HEAD_*
Bob Peterson588bff92017-12-18 12:48:29 -0600819 * @op_flags: flags to pass to the bio
820 *
821 * Returns: the initialized log buffer descriptor
822 */
823
Bob Petersonc1696fb2018-01-17 00:01:33 +0100824void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
Bob Peterson7c70b892019-03-25 09:34:19 -0600825 u64 seq, u32 tail, u32 lblock, u32 flags,
826 int op_flags)
Bob Peterson588bff92017-12-18 12:48:29 -0600827{
828 struct gfs2_log_header *lh;
Bob Petersonc1696fb2018-01-17 00:01:33 +0100829 u32 hash, crc;
Bob Petersonade48082019-11-20 08:53:14 -0500830 struct page *page;
Bob Petersonc1696fb2018-01-17 00:01:33 +0100831 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
832 struct timespec64 tv;
833 struct super_block *sb = sdp->sd_vfs;
Bob Peterson7c70b892019-03-25 09:34:19 -0600834 u64 dblock;
Bob Peterson588bff92017-12-18 12:48:29 -0600835
Bob Petersonade48082019-11-20 08:53:14 -0500836 if (gfs2_withdrawn(sdp))
Andreas Gruenbacher4a3d0492020-12-11 01:36:25 +0100837 return;
Bob Petersonade48082019-11-20 08:53:14 -0500838
839 page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
Bob Peterson588bff92017-12-18 12:48:29 -0600840 lh = page_address(page);
841 clear_page(lh);
842
843 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
844 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
845 lh->lh_header.__pad0 = cpu_to_be64(0);
846 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
847 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
848 lh->lh_sequence = cpu_to_be64(seq);
849 lh->lh_flags = cpu_to_be32(flags);
850 lh->lh_tail = cpu_to_be32(tail);
Bob Peterson7c70b892019-03-25 09:34:19 -0600851 lh->lh_blkno = cpu_to_be32(lblock);
Bob Petersonc1696fb2018-01-17 00:01:33 +0100852 hash = ~crc32(~0, lh, LH_V1_SIZE);
Bob Peterson588bff92017-12-18 12:48:29 -0600853 lh->lh_hash = cpu_to_be32(hash);
854
Arnd Bergmannee9c7f92018-06-20 15:15:24 -0500855 ktime_get_coarse_real_ts64(&tv);
Bob Petersonc1696fb2018-01-17 00:01:33 +0100856 lh->lh_nsec = cpu_to_be32(tv.tv_nsec);
857 lh->lh_sec = cpu_to_be64(tv.tv_sec);
Bob Peterson7c70b892019-03-25 09:34:19 -0600858 if (!list_empty(&jd->extent_list))
Andreas Gruenbacher19ebc052019-08-28 22:21:34 +0200859 dblock = gfs2_log_bmap(jd, lblock);
Bob Peterson7c70b892019-03-25 09:34:19 -0600860 else {
Andreas Gruenbacher152f58c2021-03-27 22:05:14 +0100861 unsigned int extlen;
862 int ret;
863
864 extlen = 1;
865 ret = gfs2_get_extent(jd->jd_inode, lblock, &dblock, &extlen);
Bob Peterson7c70b892019-03-25 09:34:19 -0600866 if (gfs2_assert_withdraw(sdp, ret == 0))
867 return;
868 }
869 lh->lh_addr = cpu_to_be64(dblock);
Bob Petersonc1696fb2018-01-17 00:01:33 +0100870 lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr);
871
872 /* We may only write local statfs, quota, etc., when writing to our
873 own journal. The values are left 0 when recovering a journal
874 different from our own. */
875 if (!(flags & GFS2_LOG_HEAD_RECOVERY)) {
876 lh->lh_statfs_addr =
877 cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr);
878 lh->lh_quota_addr =
879 cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr);
880
881 spin_lock(&sdp->sd_statfs_spin);
882 lh->lh_local_total = cpu_to_be64(l_sc->sc_total);
883 lh->lh_local_free = cpu_to_be64(l_sc->sc_free);
884 lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes);
885 spin_unlock(&sdp->sd_statfs_spin);
886 }
887
888 BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE);
889
890 crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
891 sb->s_blocksize - LH_V1_SIZE - 4);
892 lh->lh_crc = cpu_to_be32(crc);
893
Bob Peterson82218942021-01-21 10:10:26 -0500894 gfs2_log_write(sdp, jd, page, sb->s_blocksize, 0, dblock);
895 gfs2_log_submit_bio(&jd->jd_log_bio, REQ_OP_WRITE | op_flags);
Bob Peterson588bff92017-12-18 12:48:29 -0600896}
897
898/**
Steven Whitehouse34cc1782012-03-09 10:45:56 +0000899 * log_write_header - Get and initialize a journal header buffer
900 * @sdp: The GFS2 superblock
Bob Petersonc1696fb2018-01-17 00:01:33 +0100901 * @flags: The log header flags, including log header origin
Steven Whitehouse34cc1782012-03-09 10:45:56 +0000902 *
903 * Returns: the initialized log buffer descriptor
904 */
905
Steven Whitehousefdb76a42012-04-02 15:34:36 +0100906static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
Steven Whitehouse34cc1782012-03-09 10:45:56 +0000907{
Jan Kara0f0b9b62017-05-02 13:14:13 +0200908 int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600909 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
Steven Whitehouse34cc1782012-03-09 10:45:56 +0000910
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600911 gfs2_assert_withdraw(sdp, (state != SFS_FROZEN));
Steven Whitehouse34cc1782012-03-09 10:45:56 +0000912
Steven Whitehouse34cc1782012-03-09 10:45:56 +0000913 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
914 gfs2_ordered_wait(sdp);
915 log_flush_wait(sdp);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600916 op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
Steven Whitehouse34cc1782012-03-09 10:45:56 +0000917 }
Andreas Gruenbacher5cb738b2020-12-19 07:15:17 +0100918 sdp->sd_log_idle = (sdp->sd_log_flush_tail == sdp->sd_log_flush_head);
919 gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++,
920 sdp->sd_log_flush_tail, sdp->sd_log_flush_head,
921 flags, op_flags);
Andreas Gruenbacher19ebc052019-08-28 22:21:34 +0200922 gfs2_log_incr_head(sdp);
Andreas Gruenbacher4a3d0492020-12-11 01:36:25 +0100923 log_flush_wait(sdp);
Andreas Gruenbacher5cb738b2020-12-19 07:15:17 +0100924 log_pull_tail(sdp);
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +0100925 gfs2_log_update_head(sdp);
Steven Whitehouse34cc1782012-03-09 10:45:56 +0000926}
927
928/**
Bob Petersonf5456b52021-05-19 14:54:02 -0400929 * gfs2_ail_drain - drain the ail lists after a withdraw
Bob Peterson2ca0c2f2019-11-13 13:58:30 -0600930 * @sdp: Pointer to GFS2 superblock
931 */
Bob Petersonf5456b52021-05-19 14:54:02 -0400932void gfs2_ail_drain(struct gfs2_sbd *sdp)
Bob Peterson2ca0c2f2019-11-13 13:58:30 -0600933{
934 struct gfs2_trans *tr;
935
936 spin_lock(&sdp->sd_ail_lock);
937 /*
938 * For transactions on the sd_ail1_list we need to drain both the
939 * ail1 and ail2 lists. That's because function gfs2_ail1_start_one
940 * (temporarily) moves items from its tr_ail1 list to tr_ail2 list
941 * before revokes are sent for that block. Items on the sd_ail2_list
942 * should have already gotten beyond that point, so no need.
943 */
944 while (!list_empty(&sdp->sd_ail1_list)) {
945 tr = list_first_entry(&sdp->sd_ail1_list, struct gfs2_trans,
946 tr_list);
947 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail1_list);
948 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
949 list_del(&tr->tr_list);
Bob Petersonb839dad2019-04-17 12:04:27 -0600950 gfs2_trans_free(sdp, tr);
Bob Peterson2ca0c2f2019-11-13 13:58:30 -0600951 }
952 while (!list_empty(&sdp->sd_ail2_list)) {
953 tr = list_first_entry(&sdp->sd_ail2_list, struct gfs2_trans,
954 tr_list);
955 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
956 list_del(&tr->tr_list);
Bob Petersonb839dad2019-04-17 12:04:27 -0600957 gfs2_trans_free(sdp, tr);
Bob Peterson2ca0c2f2019-11-13 13:58:30 -0600958 }
Bob Petersonf5456b52021-05-19 14:54:02 -0400959 gfs2_drain_revokes(sdp);
Bob Peterson2ca0c2f2019-11-13 13:58:30 -0600960 spin_unlock(&sdp->sd_ail_lock);
961}
962
963/**
Bob Petersond5dc3d92020-05-22 14:03:21 -0500964 * empty_ail1_list - try to start IO and empty the ail1 list
965 * @sdp: Pointer to GFS2 superblock
966 */
967static void empty_ail1_list(struct gfs2_sbd *sdp)
968{
969 unsigned long start = jiffies;
970
971 for (;;) {
972 if (time_after(jiffies, start + (HZ * 600))) {
973 fs_err(sdp, "Error: In %s for 10 minutes! t=%d\n",
974 __func__, current->journal_info ? 1 : 0);
975 dump_ail_list(sdp);
976 return;
977 }
978 gfs2_ail1_start(sdp);
979 gfs2_ail1_wait(sdp);
980 if (gfs2_ail1_empty(sdp, 0))
981 return;
982 }
983}
984
985/**
Bob Peterson521031fa2020-08-31 08:12:15 -0500986 * trans_drain - drain the buf and databuf queue for a failed transaction
Bob Peterson462582b2020-08-21 08:50:34 -0500987 * @tr: the transaction to drain
988 *
989 * When this is called, we're taking an error exit for a log write that failed
990 * but since we bypassed the after_commit functions, we need to remove the
991 * items from the buf and databuf queue.
992 */
993static void trans_drain(struct gfs2_trans *tr)
994{
995 struct gfs2_bufdata *bd;
996 struct list_head *head;
997
998 if (!tr)
999 return;
1000
1001 head = &tr->tr_buf;
1002 while (!list_empty(head)) {
1003 bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
1004 list_del_init(&bd->bd_list);
Bob Peterson1a5a2cf2021-02-25 11:11:09 -05001005 if (!list_empty(&bd->bd_ail_st_list))
1006 gfs2_remove_from_ail(bd);
Bob Peterson462582b2020-08-21 08:50:34 -05001007 kmem_cache_free(gfs2_bufdata_cachep, bd);
1008 }
1009 head = &tr->tr_databuf;
1010 while (!list_empty(head)) {
1011 bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
1012 list_del_init(&bd->bd_list);
Bob Peterson1a5a2cf2021-02-25 11:11:09 -05001013 if (!list_empty(&bd->bd_ail_st_list))
1014 gfs2_remove_from_ail(bd);
Bob Peterson462582b2020-08-21 08:50:34 -05001015 kmem_cache_free(gfs2_bufdata_cachep, bd);
1016 }
1017}
1018
1019/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -04001020 * gfs2_log_flush - flush incore transaction(s)
Lee Jonesc551f662021-03-30 17:44:29 +01001021 * @sdp: The filesystem
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 * @gl: The glock structure to flush. If NULL, flush the whole incore log
Bob Peterson805c09072018-01-08 10:34:17 -05001023 * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024 *
1025 */
1026
Bob Petersonc1696fb2018-01-17 00:01:33 +01001027void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028{
Bob Peterson2ca0c2f2019-11-13 13:58:30 -06001029 struct gfs2_trans *tr = NULL;
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001030 unsigned int reserved_blocks = 0, used_blocks = 0;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001031 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001032 unsigned int first_log_head;
Andreas Gruenbacher2129b422020-12-17 16:14:30 +01001033 unsigned int reserved_revokes = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034
Steven Whitehouse484adff2006-03-29 09:12:12 -05001035 down_write(&sdp->sd_log_flush_lock);
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001036 trace_gfs2_log_flush(sdp, 1, flags);
Steven Whitehousef55ab262006-02-21 12:51:39 +00001037
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001038repeat:
Bob Peterson2ca0c2f2019-11-13 13:58:30 -06001039 /*
1040 * Do this check while holding the log_flush_lock to prevent new
1041 * buffers from being added to the ail via gfs2_pin()
1042 */
Bob Peterson0efc4972021-03-12 08:47:47 -05001043 if (gfs2_withdrawn(sdp) || !test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
Bob Peterson2ca0c2f2019-11-13 13:58:30 -06001044 goto out;
1045
Steven Whitehouse2bcd6102007-11-08 14:25:12 +00001046 /* Log might have been flushed while we waited for the flush lock */
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001047 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags))
1048 goto out;
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001049
1050 first_log_head = sdp->sd_log_head;
1051 sdp->sd_log_flush_head = first_log_head;
1052
1053 tr = sdp->sd_log_tr;
1054 if (tr || sdp->sd_log_num_revoke) {
1055 if (reserved_blocks)
1056 gfs2_log_release(sdp, reserved_blocks);
1057 reserved_blocks = sdp->sd_log_blks_reserved;
Andreas Gruenbacher2129b422020-12-17 16:14:30 +01001058 reserved_revokes = sdp->sd_log_num_revoke;
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001059 if (tr) {
1060 sdp->sd_log_tr = NULL;
1061 tr->tr_first = first_log_head;
Andreas Gruenbacher2129b422020-12-17 16:14:30 +01001062 if (unlikely (state == SFS_FROZEN)) {
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001063 if (gfs2_assert_withdraw_delayed(sdp,
1064 !tr->tr_num_buf_new && !tr->tr_num_databuf_new))
1065 goto out_withdraw;
Andreas Gruenbacher2129b422020-12-17 16:14:30 +01001066 }
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001067 }
1068 } else if (!reserved_blocks) {
1069 unsigned int taboo_blocks = GFS2_LOG_FLUSH_MIN_BLOCKS;
1070
1071 reserved_blocks = GFS2_LOG_FLUSH_MIN_BLOCKS;
1072 if (current == sdp->sd_logd_process)
1073 taboo_blocks = 0;
1074
1075 if (!__gfs2_log_try_reserve(sdp, reserved_blocks, taboo_blocks)) {
1076 up_write(&sdp->sd_log_flush_lock);
1077 __gfs2_log_reserve(sdp, reserved_blocks, taboo_blocks);
1078 down_write(&sdp->sd_log_flush_lock);
1079 goto repeat;
1080 }
Andreas Gruenbacher2129b422020-12-17 16:14:30 +01001081 BUG_ON(sdp->sd_log_num_revoke);
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001082 }
Steven Whitehousef55ab262006-02-21 12:51:39 +00001083
Bob Petersonc1696fb2018-01-17 00:01:33 +01001084 if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN)
Benjamin Marzinski400ac522015-12-09 07:46:33 -06001085 clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
1086
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001087 if (unlikely(state == SFS_FROZEN))
Andreas Gruenbacher2129b422020-12-17 16:14:30 +01001088 if (gfs2_assert_withdraw_delayed(sdp, !reserved_revokes))
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001089 goto out_withdraw;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090
Steven Whitehoused7b616e2007-09-02 10:48:13 +01001091 gfs2_ordered_write(sdp);
Bob Peterson2ca0c2f2019-11-13 13:58:30 -06001092 if (gfs2_withdrawn(sdp))
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001093 goto out_withdraw;
Steven Whitehoused69a3c62014-02-21 15:22:35 +00001094 lops_before_commit(sdp, tr);
Bob Peterson2ca0c2f2019-11-13 13:58:30 -06001095 if (gfs2_withdrawn(sdp))
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001096 goto out_withdraw;
Bob Peterson82218942021-01-21 10:10:26 -05001097 gfs2_log_submit_bio(&sdp->sd_jdesc->jd_log_bio, REQ_OP_WRITE);
Bob Peterson2ca0c2f2019-11-13 13:58:30 -06001098 if (gfs2_withdrawn(sdp))
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001099 goto out_withdraw;
Steven Whitehoused7b616e2007-09-02 10:48:13 +01001100
Steven Whitehouse34cc1782012-03-09 10:45:56 +00001101 if (sdp->sd_log_head != sdp->sd_log_flush_head) {
Bob Petersonc1696fb2018-01-17 00:01:33 +01001102 log_write_header(sdp, flags);
Andreas Gruenbacher5cb738b2020-12-19 07:15:17 +01001103 } else if (sdp->sd_log_tail != sdp->sd_log_flush_tail && !sdp->sd_log_idle) {
Bob Petersonc1696fb2018-01-17 00:01:33 +01001104 log_write_header(sdp, flags);
Robert Peterson2332c442007-06-18 14:50:20 -05001105 }
Bob Peterson2ca0c2f2019-11-13 13:58:30 -06001106 if (gfs2_withdrawn(sdp))
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001107 goto out_withdraw;
Benjamin Marzinski16ca9412013-04-05 20:31:46 -05001108 lops_after_commit(sdp, tr);
Steven Whitehousefe1a6982006-10-11 13:34:59 -04001109
1110 gfs2_log_lock(sdp);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -04001111 sdp->sd_log_blks_reserved = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001112
Dave Chinnerd6a079e2011-03-11 11:52:25 +00001113 spin_lock(&sdp->sd_ail_lock);
Benjamin Marzinski16ca9412013-04-05 20:31:46 -05001114 if (tr && !list_empty(&tr->tr_ail1_list)) {
1115 list_add(&tr->tr_list, &sdp->sd_ail1_list);
1116 tr = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001117 }
Dave Chinnerd6a079e2011-03-11 11:52:25 +00001118 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119 gfs2_log_unlock(sdp);
Benjamin Marzinski24972552014-05-01 22:26:55 -05001120
Bob Petersonc1696fb2018-01-17 00:01:33 +01001121 if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
Benjamin Marzinski24972552014-05-01 22:26:55 -05001122 if (!sdp->sd_log_idle) {
Bob Petersond5dc3d92020-05-22 14:03:21 -05001123 empty_ail1_list(sdp);
Bob Peterson30fe70a2019-11-13 11:47:09 -06001124 if (gfs2_withdrawn(sdp))
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001125 goto out_withdraw;
Bob Petersonc1696fb2018-01-17 00:01:33 +01001126 log_write_header(sdp, flags);
Benjamin Marzinski24972552014-05-01 22:26:55 -05001127 }
Bob Petersonc1696fb2018-01-17 00:01:33 +01001128 if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
1129 GFS2_LOG_HEAD_FLUSH_FREEZE))
Benjamin Marzinski24972552014-05-01 22:26:55 -05001130 gfs2_log_shutdown(sdp);
Bob Petersonc1696fb2018-01-17 00:01:33 +01001131 if (flags & GFS2_LOG_HEAD_FLUSH_FREEZE)
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001132 atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
Benjamin Marzinski24972552014-05-01 22:26:55 -05001133 }
1134
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001135out_end:
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001136 used_blocks = log_distance(sdp, sdp->sd_log_flush_head, first_log_head);
Andreas Gruenbacher2129b422020-12-17 16:14:30 +01001137 reserved_revokes += atomic_read(&sdp->sd_log_revokes_available);
1138 atomic_set(&sdp->sd_log_revokes_available, sdp->sd_ldptrs);
1139 gfs2_assert_withdraw(sdp, reserved_revokes % sdp->sd_inptrs == sdp->sd_ldptrs);
1140 if (reserved_revokes > sdp->sd_ldptrs)
1141 reserved_blocks += (reserved_revokes - sdp->sd_ldptrs) / sdp->sd_inptrs;
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001142out:
Andreas Gruenbacher2129b422020-12-17 16:14:30 +01001143 if (used_blocks != reserved_blocks) {
1144 gfs2_assert_withdraw_delayed(sdp, used_blocks < reserved_blocks);
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001145 gfs2_log_release(sdp, reserved_blocks - used_blocks);
Andreas Gruenbacher2129b422020-12-17 16:14:30 +01001146 }
Steven Whitehouse484adff2006-03-29 09:12:12 -05001147 up_write(&sdp->sd_log_flush_lock);
Bob Petersonb839dad2019-04-17 12:04:27 -06001148 gfs2_trans_free(sdp, tr);
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001149 if (gfs2_withdrawing(sdp))
1150 gfs2_withdraw(sdp);
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001151 trace_gfs2_log_flush(sdp, 0, flags);
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001152 return;
1153
1154out_withdraw:
1155 trans_drain(tr);
1156 /**
1157 * If the tr_list is empty, we're withdrawing during a log
1158 * flush that targets a transaction, but the transaction was
1159 * never queued onto any of the ail lists. Here we add it to
1160 * ail1 just so that ail_drain() will find and free it.
1161 */
1162 spin_lock(&sdp->sd_ail_lock);
1163 if (tr && list_empty(&tr->tr_list))
1164 list_add(&tr->tr_list, &sdp->sd_ail1_list);
1165 spin_unlock(&sdp->sd_ail_lock);
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +02001166 tr = NULL;
1167 goto out_end;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001168}
1169
Steven Whitehoused69a3c62014-02-21 15:22:35 +00001170/**
1171 * gfs2_merge_trans - Merge a new transaction into a cached transaction
Lee Jonesc551f662021-03-30 17:44:29 +01001172 * @sdp: the filesystem
Steven Whitehoused69a3c62014-02-21 15:22:35 +00001173 * @new: New transaction to be merged
1174 */
1175
Bob Peterson83d060c2020-06-04 14:28:58 -05001176static void gfs2_merge_trans(struct gfs2_sbd *sdp, struct gfs2_trans *new)
Steven Whitehoused69a3c62014-02-21 15:22:35 +00001177{
Bob Peterson83d060c2020-06-04 14:28:58 -05001178 struct gfs2_trans *old = sdp->sd_log_tr;
1179
Bob Peterson9862ca02017-01-25 12:50:47 -05001180 WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
Steven Whitehoused69a3c62014-02-21 15:22:35 +00001181
1182 old->tr_num_buf_new += new->tr_num_buf_new;
1183 old->tr_num_databuf_new += new->tr_num_databuf_new;
1184 old->tr_num_buf_rm += new->tr_num_buf_rm;
1185 old->tr_num_databuf_rm += new->tr_num_databuf_rm;
Andreas Gruenbacher2129b422020-12-17 16:14:30 +01001186 old->tr_revokes += new->tr_revokes;
Steven Whitehoused69a3c62014-02-21 15:22:35 +00001187 old->tr_num_revoke += new->tr_num_revoke;
Steven Whitehoused69a3c62014-02-21 15:22:35 +00001188
1189 list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
1190 list_splice_tail_init(&new->tr_buf, &old->tr_buf);
Bob Peterson83d060c2020-06-04 14:28:58 -05001191
1192 spin_lock(&sdp->sd_ail_lock);
1193 list_splice_tail_init(&new->tr_ail1_list, &old->tr_ail1_list);
1194 list_splice_tail_init(&new->tr_ail2_list, &old->tr_ail2_list);
1195 spin_unlock(&sdp->sd_ail_lock);
Steven Whitehoused69a3c62014-02-21 15:22:35 +00001196}
1197
David Teiglandb3b94fa2006-01-16 16:50:04 +00001198static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1199{
Robert Peterson2332c442007-06-18 14:50:20 -05001200 unsigned int reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +00001201 unsigned int unused;
Steven Whitehouse022ef4f2014-02-21 21:55:33 +00001202 unsigned int maxres;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203
1204 gfs2_log_lock(sdp);
1205
Steven Whitehoused69a3c62014-02-21 15:22:35 +00001206 if (sdp->sd_log_tr) {
Bob Peterson83d060c2020-06-04 14:28:58 -05001207 gfs2_merge_trans(sdp, tr);
Steven Whitehoused69a3c62014-02-21 15:22:35 +00001208 } else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
Andreas Gruenbacherc968f572021-01-29 16:45:33 +01001209 gfs2_assert_withdraw(sdp, !test_bit(TR_ONSTACK, &tr->tr_flags));
Benjamin Marzinski16ca9412013-04-05 20:31:46 -05001210 sdp->sd_log_tr = tr;
Bob Peterson9862ca02017-01-25 12:50:47 -05001211 set_bit(TR_ATTACHED, &tr->tr_flags);
Benjamin Marzinski16ca9412013-04-05 20:31:46 -05001212 }
Steven Whitehouse022ef4f2014-02-21 21:55:33 +00001213
Steven Whitehouse022ef4f2014-02-21 21:55:33 +00001214 reserved = calc_reserved(sdp);
1215 maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
1216 gfs2_assert_withdraw(sdp, maxres >= reserved);
1217 unused = maxres - reserved;
Andreas Gruenbacher5ae8fff2020-12-13 11:37:17 +01001218 if (unused)
1219 gfs2_log_release(sdp, unused);
Steven Whitehouse022ef4f2014-02-21 21:55:33 +00001220 sdp->sd_log_blks_reserved = reserved;
1221
David Teiglandb3b94fa2006-01-16 16:50:04 +00001222 gfs2_log_unlock(sdp);
1223}
1224
1225/**
1226 * gfs2_log_commit - Commit a transaction to the log
1227 * @sdp: the filesystem
1228 * @tr: the transaction
1229 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001230 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
1231 * or the total number of used blocks (pinned blocks plus AIL blocks)
1232 * is greater than thresh2.
1233 *
Bob Petersonb57bc0f2020-07-22 10:19:52 -05001234 * At mount time thresh1 is 2/5ths of journal size, thresh2 is 4/5ths of
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001235 * journal size.
1236 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237 * Returns: errno
1238 */
1239
1240void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1241{
1242 log_refund(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001243
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001244 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
1245 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
1246 atomic_read(&sdp->sd_log_thresh2)))
1247 wake_up(&sdp->sd_logd_waitq);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248}
1249
1250/**
1251 * gfs2_log_shutdown - write a shutdown header into a journal
1252 * @sdp: the filesystem
1253 *
1254 */
1255
Bob Petersonfeed98a2019-11-14 09:48:26 -05001256static void gfs2_log_shutdown(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001257{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001258 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001259 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001260 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
1261
Bob Peterson805c09072018-01-08 10:34:17 -05001262 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN);
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001263 log_pull_tail(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264
Steven Whitehousea74604b2006-04-21 15:10:46 -04001265 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
1266 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
Steven Whitehousea25311c2006-11-23 11:06:35 -05001267}
1268
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001269static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
1270{
Bob Petersonf07b3522017-01-05 16:01:45 -05001271 return (atomic_read(&sdp->sd_log_pinned) +
1272 atomic_read(&sdp->sd_log_blks_needed) >=
1273 atomic_read(&sdp->sd_log_thresh1));
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001274}
1275
1276static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
1277{
1278 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
Abhi Dasb066a4eeb2017-08-04 12:15:32 -05001279
1280 if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags))
1281 return 1;
1282
Bob Petersonf07b3522017-01-05 16:01:45 -05001283 return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >=
1284 atomic_read(&sdp->sd_log_thresh2);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001285}
Steven Whitehouseec69b182007-11-09 10:01:41 +00001286
1287/**
1288 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
Lee Jonesc551f662021-03-30 17:44:29 +01001289 * @data: Pointer to GFS2 superblock
Steven Whitehouseec69b182007-11-09 10:01:41 +00001290 *
1291 * Also, periodically check to make sure that we're using the most recent
1292 * journal index.
1293 */
1294
1295int gfs2_logd(void *data)
1296{
1297 struct gfs2_sbd *sdp = data;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001298 unsigned long t = 1;
1299 DEFINE_WAIT(wait);
Steven Whitehouseec69b182007-11-09 10:01:41 +00001300
1301 while (!kthread_should_stop()) {
Steven Whitehouseec69b182007-11-09 10:01:41 +00001302
Bob Petersond22f69a2020-04-24 12:17:33 -05001303 if (gfs2_withdrawn(sdp)) {
1304 msleep_interruptible(HZ);
1305 continue;
1306 }
Bob Peterson942b0cd2017-08-16 11:30:06 -05001307 /* Check for errors writing to the journal */
1308 if (sdp->sd_log_error) {
Andreas Gruenbacherbadb55e2020-01-23 18:41:00 +01001309 gfs2_lm(sdp,
1310 "GFS2: fsid=%s: error %d: "
1311 "withdrawing the file system to "
1312 "prevent further damage.\n",
1313 sdp->sd_fsname, sdp->sd_log_error);
1314 gfs2_withdraw(sdp);
Bob Petersond22f69a2020-04-24 12:17:33 -05001315 continue;
Bob Peterson942b0cd2017-08-16 11:30:06 -05001316 }
1317
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001318 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
Bob Peterson5e4c7632019-02-21 14:28:07 -07001319 gfs2_ail1_empty(sdp, 0);
Bob Peterson805c09072018-01-08 10:34:17 -05001320 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001321 GFS2_LFC_LOGD_JFLUSH_REQD);
Steven Whitehouseec69b182007-11-09 10:01:41 +00001322 }
1323
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001324 if (gfs2_ail_flush_reqd(sdp)) {
1325 gfs2_ail1_start(sdp);
Steven Whitehouse26b06a62011-05-21 19:21:07 +01001326 gfs2_ail1_wait(sdp);
Bob Peterson5e4c7632019-02-21 14:28:07 -07001327 gfs2_ail1_empty(sdp, 0);
Bob Peterson805c09072018-01-08 10:34:17 -05001328 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
Andreas Gruenbacherfe3e3972020-12-10 12:49:56 +01001329 GFS2_LFC_LOGD_AIL_FLUSH_REQD);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001330 }
1331
Steven Whitehouseec69b182007-11-09 10:01:41 +00001332 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
Tejun Heoa0acae02011-11-21 12:32:22 -08001333
1334 try_to_freeze();
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001335
1336 do {
1337 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
Steven Whitehouse5f487492010-09-09 14:45:00 +01001338 TASK_INTERRUPTIBLE);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001339 if (!gfs2_ail_flush_reqd(sdp) &&
1340 !gfs2_jrnl_flush_reqd(sdp) &&
1341 !kthread_should_stop())
1342 t = schedule_timeout(t);
1343 } while(t && !gfs2_ail_flush_reqd(sdp) &&
1344 !gfs2_jrnl_flush_reqd(sdp) &&
1345 !kthread_should_stop());
1346 finish_wait(&sdp->sd_logd_waitq, &wait);
Steven Whitehouseec69b182007-11-09 10:01:41 +00001347 }
1348
1349 return 0;
1350}
1351