]> git.ipfire.org Git - people/ms/linux.git/blame - fs/gfs2/log.c
gfs2: Get rid of current_tail()
[people/ms/linux.git] / fs / gfs2 / log.c
CommitLineData
7336d0e6 1// SPDX-License-Identifier: GPL-2.0-only
b3b94faa
DT
2/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
da6dd40d 4 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
b3b94faa
DT
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>
5c676f6d 12#include <linux/gfs2_ondisk.h>
71b86f56 13#include <linux/crc32.h>
c1696fb8 14#include <linux/crc32c.h>
a25311c8 15#include <linux/delay.h>
ec69b188
SW
16#include <linux/kthread.h>
17#include <linux/freezer.h>
254db57f 18#include <linux/bio.h>
885bceca 19#include <linux/blkdev.h>
4667a0ec 20#include <linux/writeback.h>
4a36d08d 21#include <linux/list_sort.h>
b3b94faa
DT
22
23#include "gfs2.h"
5c676f6d 24#include "incore.h"
b3b94faa
DT
25#include "bmap.h"
26#include "glock.h"
27#include "log.h"
28#include "lops.h"
29#include "meta_io.h"
5c676f6d 30#include "util.h"
71b86f56 31#include "dir.h"
63997775 32#include "trace_gfs2.h"
b839dada 33#include "trans.h"
b3b94faa 34
feed98a8
BP
35static void gfs2_log_shutdown(struct gfs2_sbd *sdp);
36
b3b94faa
DT
37/**
38 * gfs2_struct2blk - compute stuff
39 * @sdp: the filesystem
40 * @nstruct: the number of structures
b3b94faa
DT
41 *
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
2e9eeaa1 48unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct)
b3b94faa
DT
49{
50 unsigned int blks;
51 unsigned int first, second;
52
6188e877 53 /* The initial struct gfs2_log_descriptor block */
b3b94faa 54 blks = 1;
2e9eeaa1 55 first = sdp->sd_ldptrs;
b3b94faa
DT
56
57 if (nstruct > first) {
6188e877 58 /* Subsequent struct gfs2_meta_header blocks */
2e9eeaa1 59 second = sdp->sd_inptrs;
5c676f6d 60 blks += DIV_ROUND_UP(nstruct - first, second);
b3b94faa
DT
61 }
62
63 return blks;
64}
65
1e1a3d03
SW
66/**
67 * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
68 * @mapping: The associated mapping (maybe NULL)
69 * @bd: The gfs2_bufdata to remove
70 *
c618e87a 71 * The ail lock _must_ be held when calling this function
1e1a3d03
SW
72 *
73 */
74
68942870 75void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
1e1a3d03 76{
16ca9412 77 bd->bd_tr = NULL;
1ad38c43
SW
78 list_del_init(&bd->bd_ail_st_list);
79 list_del_init(&bd->bd_ail_gl_list);
1e1a3d03 80 atomic_dec(&bd->bd_gl->gl_ail_count);
1e1a3d03
SW
81 brelse(bd->bd_bh);
82}
83
ddacfaf7
SW
84/**
85 * gfs2_ail1_start_one - Start I/O on a part of the AIL
86 * @sdp: the filesystem
4667a0ec
SW
87 * @wbc: The writeback control structure
88 * @ai: The ail structure
ddacfaf7
SW
89 *
90 */
91
4f1de018
SW
92static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
93 struct writeback_control *wbc,
69511080 94 struct gfs2_trans *tr)
d6a079e8
DC
95__releases(&sdp->sd_ail_lock)
96__acquires(&sdp->sd_ail_lock)
ddacfaf7 97{
5ac048bb 98 struct gfs2_glock *gl = NULL;
4667a0ec 99 struct address_space *mapping;
ddacfaf7
SW
100 struct gfs2_bufdata *bd, *s;
101 struct buffer_head *bh;
b1676cbb 102 int ret = 0;
ddacfaf7 103
16ca9412 104 list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
4667a0ec 105 bh = bd->bd_bh;
ddacfaf7 106
16ca9412 107 gfs2_assert(sdp, bd->bd_tr == tr);
ddacfaf7 108
4667a0ec 109 if (!buffer_busy(bh)) {
30fe70a8
BP
110 if (buffer_uptodate(bh)) {
111 list_move(&bd->bd_ail_st_list,
112 &tr->tr_ail2_list);
113 continue;
114 }
036330c9 115 if (!cmpxchg(&sdp->sd_log_error, 0, -EIO)) {
4667a0ec 116 gfs2_io_error_bh(sdp, bh);
69511080 117 gfs2_withdraw_delayed(sdp);
9e1a9ecd 118 }
4667a0ec
SW
119 }
120
30fe70a8
BP
121 if (gfs2_withdrawn(sdp)) {
122 gfs2_remove_from_ail(bd);
123 continue;
124 }
4667a0ec
SW
125 if (!buffer_dirty(bh))
126 continue;
127 if (gl == bd->bd_gl)
128 continue;
129 gl = bd->bd_gl;
16ca9412 130 list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
4667a0ec 131 mapping = bh->b_page->mapping;
4f1de018
SW
132 if (!mapping)
133 continue;
4667a0ec 134 spin_unlock(&sdp->sd_ail_lock);
b1676cbb 135 ret = generic_writepages(mapping, wbc);
4667a0ec 136 spin_lock(&sdp->sd_ail_lock);
4e79e3f0
BP
137 if (ret == -ENODATA) /* if a jdata write into a new hole */
138 ret = 0; /* ignore it */
b1676cbb 139 if (ret || wbc->nr_to_write <= 0)
4667a0ec 140 break;
b1676cbb 141 return -EBUSY;
4667a0ec 142 }
4f1de018 143
b1676cbb 144 return ret;
4667a0ec 145}
ddacfaf7 146
9592ea80
BP
147static void dump_ail_list(struct gfs2_sbd *sdp)
148{
149 struct gfs2_trans *tr;
150 struct gfs2_bufdata *bd;
151 struct buffer_head *bh;
152
9592ea80
BP
153 list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
154 list_for_each_entry_reverse(bd, &tr->tr_ail1_list,
155 bd_ail_st_list) {
156 bh = bd->bd_bh;
157 fs_err(sdp, "bd %p: blk:0x%llx bh=%p ", bd,
158 (unsigned long long)bd->bd_blkno, bh);
159 if (!bh) {
160 fs_err(sdp, "\n");
161 continue;
162 }
163 fs_err(sdp, "0x%llx up2:%d dirt:%d lkd:%d req:%d "
164 "map:%d new:%d ar:%d aw:%d delay:%d "
165 "io err:%d unwritten:%d dfr:%d pin:%d esc:%d\n",
166 (unsigned long long)bh->b_blocknr,
167 buffer_uptodate(bh), buffer_dirty(bh),
168 buffer_locked(bh), buffer_req(bh),
169 buffer_mapped(bh), buffer_new(bh),
170 buffer_async_read(bh), buffer_async_write(bh),
171 buffer_delay(bh), buffer_write_io_error(bh),
172 buffer_unwritten(bh),
173 buffer_defer_completion(bh),
174 buffer_pinned(bh), buffer_escaped(bh));
175 }
176 }
177}
ddacfaf7 178
4667a0ec
SW
179/**
180 * gfs2_ail1_flush - start writeback of some ail1 entries
181 * @sdp: The super block
182 * @wbc: The writeback control structure
183 *
184 * Writes back some ail1 entries, according to the limits in the
185 * writeback control structure
186 */
ddacfaf7 187
4667a0ec
SW
188void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
189{
190 struct list_head *head = &sdp->sd_ail1_list;
16ca9412 191 struct gfs2_trans *tr;
885bceca 192 struct blk_plug plug;
75b46c43 193 int ret;
9592ea80 194 unsigned long flush_start = jiffies;
ddacfaf7 195
c83ae9ca 196 trace_gfs2_ail_flush(sdp, wbc, 1);
885bceca 197 blk_start_plug(&plug);
4667a0ec 198 spin_lock(&sdp->sd_ail_lock);
4f1de018 199restart:
75b46c43 200 ret = 0;
9592ea80 201 if (time_after(jiffies, flush_start + (HZ * 600))) {
d5dc3d96
BP
202 fs_err(sdp, "Error: In %s for ten minutes! t=%d\n",
203 __func__, current->journal_info ? 1 : 0);
9592ea80
BP
204 dump_ail_list(sdp);
205 goto out;
206 }
16ca9412 207 list_for_each_entry_reverse(tr, head, tr_list) {
4667a0ec 208 if (wbc->nr_to_write <= 0)
ddacfaf7 209 break;
b1676cbb
BP
210 ret = gfs2_ail1_start_one(sdp, wbc, tr);
211 if (ret) {
212 if (ret == -EBUSY)
213 goto restart;
214 break;
215 }
4667a0ec 216 }
9592ea80 217out:
4667a0ec 218 spin_unlock(&sdp->sd_ail_lock);
885bceca 219 blk_finish_plug(&plug);
49003128
BP
220 if (ret) {
221 gfs2_lm(sdp, "gfs2_ail1_start_one (generic_writepages) "
222 "returned: %d\n", ret);
badb55ec 223 gfs2_withdraw(sdp);
49003128 224 }
c83ae9ca 225 trace_gfs2_ail_flush(sdp, wbc, 0);
4667a0ec
SW
226}
227
228/**
229 * gfs2_ail1_start - start writeback of all ail1 entries
230 * @sdp: The superblock
231 */
232
233static void gfs2_ail1_start(struct gfs2_sbd *sdp)
234{
235 struct writeback_control wbc = {
236 .sync_mode = WB_SYNC_NONE,
237 .nr_to_write = LONG_MAX,
238 .range_start = 0,
239 .range_end = LLONG_MAX,
240 };
241
242 return gfs2_ail1_flush(sdp, &wbc);
ddacfaf7
SW
243}
244
5cb738b5
AG
245static void gfs2_log_update_flush_tail(struct gfs2_sbd *sdp)
246{
247 unsigned int new_flush_tail = sdp->sd_log_head;
248 struct gfs2_trans *tr;
249
250 if (!list_empty(&sdp->sd_ail1_list)) {
251 tr = list_last_entry(&sdp->sd_ail1_list,
252 struct gfs2_trans, tr_list);
253 new_flush_tail = tr->tr_first;
254 }
255 sdp->sd_log_flush_tail = new_flush_tail;
256}
257
258static void gfs2_log_update_head(struct gfs2_sbd *sdp)
259{
260 unsigned int new_head = sdp->sd_log_flush_head;
261
262 if (sdp->sd_log_flush_tail == sdp->sd_log_head)
263 sdp->sd_log_flush_tail = new_head;
264 sdp->sd_log_head = new_head;
265}
266
ddacfaf7
SW
267/**
268 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
269 * @sdp: the filesystem
5e4c7632
BP
270 * @tr: the transaction
271 * @max_revokes: If nonzero, issue revokes for the bd items for written buffers
ddacfaf7 272 *
36c78309 273 * returns: the transaction's count of remaining active items
ddacfaf7
SW
274 */
275
36c78309 276static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
5e4c7632 277 int *max_revokes)
ddacfaf7
SW
278{
279 struct gfs2_bufdata *bd, *s;
280 struct buffer_head *bh;
36c78309 281 int active_count = 0;
ddacfaf7 282
16ca9412 283 list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
ddacfaf7
SW
284 bd_ail_st_list) {
285 bh = bd->bd_bh;
16ca9412 286 gfs2_assert(sdp, bd->bd_tr == tr);
036330c9
BP
287 /*
288 * If another process flagged an io error, e.g. writing to the
289 * journal, error all other bhs and move them off the ail1 to
290 * prevent a tight loop when unmount tries to flush ail1,
291 * regardless of whether they're still busy. If no outside
292 * errors were found and the buffer is busy, move to the next.
293 * If the ail buffer is not busy and caught an error, flag it
294 * for others.
295 */
36c78309
BP
296 if (!sdp->sd_log_error && buffer_busy(bh)) {
297 active_count++;
4667a0ec 298 continue;
36c78309 299 }
b524abcc 300 if (!buffer_uptodate(bh) &&
036330c9 301 !cmpxchg(&sdp->sd_log_error, 0, -EIO)) {
ddacfaf7 302 gfs2_io_error_bh(sdp, bh);
69511080 303 gfs2_withdraw_delayed(sdp);
9e1a9ecd 304 }
5e4c7632
BP
305 /*
306 * If we have space for revokes and the bd is no longer on any
307 * buf list, we can just add a revoke for it immediately and
308 * avoid having to put it on the ail2 list, where it would need
309 * to be revoked later.
310 */
311 if (*max_revokes && list_empty(&bd->bd_list)) {
312 gfs2_add_revoke(sdp, bd);
313 (*max_revokes)--;
314 continue;
315 }
16ca9412 316 list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
ddacfaf7 317 }
36c78309 318 return active_count;
ddacfaf7
SW
319}
320
4667a0ec
SW
321/**
322 * gfs2_ail1_empty - Try to empty the ail1 lists
323 * @sdp: The superblock
5e4c7632 324 * @max_revokes: If non-zero, add revokes where appropriate
4667a0ec
SW
325 *
326 * Tries to empty the ail1 lists, starting with the oldest first
327 */
b3b94faa 328
5e4c7632 329static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int max_revokes)
b3b94faa 330{
16ca9412 331 struct gfs2_trans *tr, *s;
5d054964 332 int oldest_tr = 1;
b3b94faa
DT
333 int ret;
334
d6a079e8 335 spin_lock(&sdp->sd_ail_lock);
16ca9412 336 list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
36c78309 337 if (!gfs2_ail1_empty_one(sdp, tr, &max_revokes) && oldest_tr)
16ca9412 338 list_move(&tr->tr_list, &sdp->sd_ail2_list);
4667a0ec 339 else
5d054964 340 oldest_tr = 0;
b3b94faa 341 }
5cb738b5 342 gfs2_log_update_flush_tail(sdp);
b3b94faa 343 ret = list_empty(&sdp->sd_ail1_list);
d6a079e8 344 spin_unlock(&sdp->sd_ail_lock);
b3b94faa 345
69511080 346 if (test_bit(SDF_WITHDRAWING, &sdp->sd_flags)) {
badb55ec
AG
347 gfs2_lm(sdp, "fatal: I/O error(s)\n");
348 gfs2_withdraw(sdp);
349 }
9e1a9ecd 350
b3b94faa
DT
351 return ret;
352}
353
26b06a69
SW
354static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
355{
16ca9412 356 struct gfs2_trans *tr;
26b06a69
SW
357 struct gfs2_bufdata *bd;
358 struct buffer_head *bh;
359
360 spin_lock(&sdp->sd_ail_lock);
16ca9412
BM
361 list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
362 list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
26b06a69
SW
363 bh = bd->bd_bh;
364 if (!buffer_locked(bh))
365 continue;
366 get_bh(bh);
367 spin_unlock(&sdp->sd_ail_lock);
368 wait_on_buffer(bh);
369 brelse(bh);
370 return;
371 }
372 }
373 spin_unlock(&sdp->sd_ail_lock);
374}
ddacfaf7
SW
375
376/**
2ca0c2fb 377 * gfs2_ail_empty_tr - empty one of the ail lists for a transaction
ddacfaf7
SW
378 */
379
2ca0c2fb
BP
380static void gfs2_ail_empty_tr(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
381 struct list_head *head)
ddacfaf7 382{
ddacfaf7
SW
383 struct gfs2_bufdata *bd;
384
385 while (!list_empty(head)) {
2ca0c2fb
BP
386 bd = list_first_entry(head, struct gfs2_bufdata,
387 bd_ail_st_list);
16ca9412 388 gfs2_assert(sdp, bd->bd_tr == tr);
f91a0d3e 389 gfs2_remove_from_ail(bd);
ddacfaf7
SW
390 }
391}
392
6e80674a
AG
393static void __ail2_empty(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
394{
395 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
396 list_del(&tr->tr_list);
397 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
398 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
399 gfs2_trans_free(sdp, tr);
400}
401
b3b94faa
DT
402static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
403{
6e80674a 404 struct list_head *ail2_list = &sdp->sd_ail2_list;
b3b94faa 405 unsigned int old_tail = sdp->sd_log_tail;
6e80674a 406 struct gfs2_trans *tr, *safe;
b3b94faa 407
d6a079e8 408 spin_lock(&sdp->sd_ail_lock);
6e80674a
AG
409 if (old_tail <= new_tail) {
410 list_for_each_entry_safe(tr, safe, ail2_list, tr_list) {
411 if (old_tail <= tr->tr_first && tr->tr_first < new_tail)
412 __ail2_empty(sdp, tr);
413 }
414 } else {
415 list_for_each_entry_safe(tr, safe, ail2_list, tr_list) {
416 if (old_tail <= tr->tr_first || tr->tr_first < new_tail)
417 __ail2_empty(sdp, tr);
418 }
b3b94faa 419 }
d6a079e8 420 spin_unlock(&sdp->sd_ail_lock);
b3b94faa
DT
421}
422
f3708fb5
AG
423/**
424 * gfs2_log_is_empty - Check if the log is empty
425 * @sdp: The GFS2 superblock
426 */
427
428bool gfs2_log_is_empty(struct gfs2_sbd *sdp) {
429 return atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks;
430}
431
24972557
BM
432/**
433 * gfs2_log_release - Release a given number of log blocks
434 * @sdp: The GFS2 superblock
435 * @blks: The number of blocks
436 *
437 */
438
439void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
440{
24972557
BM
441 atomic_add(blks, &sdp->sd_log_blks_free);
442 trace_gfs2_log_blocks(sdp, blks);
443 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
444 sdp->sd_jdesc->jd_blocks);
5ae8fff8
AG
445 if (atomic_read(&sdp->sd_log_blks_needed))
446 wake_up(&sdp->sd_log_waitq);
24972557
BM
447}
448
b3b94faa
DT
449/**
450 * gfs2_log_reserve - Make a log reservation
451 * @sdp: The GFS2 superblock
452 * @blks: The number of blocks to reserve
453 *
89918647 454 * Note that we never give out the last few blocks of the journal. Thats
2332c443 455 * due to the fact that there is a small number of header blocks
b004157a
SW
456 * associated with each log flush. The exact number can't be known until
457 * flush time, so we ensure that we have just enough free blocks at all
458 * times to avoid running out during a log flush.
459 *
5e687eac
BM
460 * We no longer flush the log here, instead we wake up logd to do that
461 * for us. To avoid the thundering herd and to ensure that we deal fairly
462 * with queued waiters, we use an exclusive wait. This means that when we
463 * get woken with enough journal space to get our reservation, we need to
464 * wake the next waiter on the list.
b3b94faa
DT
465 */
466
c1eba1b0 467void gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
b3b94faa 468{
5d054964 469 unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize);
5e687eac 470 unsigned wanted = blks + reserved_blks;
5e687eac 471 unsigned int free_blocks;
b3b94faa 472
5e687eac 473 free_blocks = atomic_read(&sdp->sd_log_blks_free);
5ae8fff8
AG
474 while (free_blocks >= wanted) {
475 if (atomic_try_cmpxchg(&sdp->sd_log_blks_free, &free_blocks,
476 free_blocks - blks))
477 return;
478 }
479
480 atomic_add(blks, &sdp->sd_log_blks_needed);
481 for (;;) {
482 if (current != sdp->sd_logd_process)
5e687eac 483 wake_up(&sdp->sd_logd_waitq);
5ae8fff8
AG
484 io_wait_event(sdp->sd_log_waitq,
485 (free_blocks = atomic_read(&sdp->sd_log_blks_free),
486 free_blocks >= wanted));
487 do {
488 if (atomic_try_cmpxchg(&sdp->sd_log_blks_free,
489 &free_blocks,
490 free_blocks - blks))
491 goto reserved;
492 } while (free_blocks >= wanted);
b3b94faa 493 }
5e687eac 494
5ae8fff8
AG
495reserved:
496 trace_gfs2_log_blocks(sdp, -blks);
497 if (atomic_sub_return(blks, &sdp->sd_log_blks_needed))
5e687eac 498 wake_up(&sdp->sd_log_waitq);
b3b94faa
DT
499}
500
b3b94faa
DT
501/**
502 * log_distance - Compute distance between two journal blocks
503 * @sdp: The GFS2 superblock
504 * @newer: The most recent journal block of the pair
505 * @older: The older journal block of the pair
506 *
507 * Compute the distance (in the journal direction) between two
508 * blocks in the journal
509 *
510 * Returns: the distance in blocks
511 */
512
faa31ce8 513static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
b3b94faa
DT
514 unsigned int older)
515{
516 int dist;
517
518 dist = newer - older;
519 if (dist < 0)
520 dist += sdp->sd_jdesc->jd_blocks;
521
522 return dist;
523}
524
2332c443 525/**
6188e877 526 * calc_reserved - Calculate the number of blocks to keep reserved
2332c443
RP
527 * @sdp: The GFS2 superblock
528 *
529 * This is complex. We need to reserve room for all our currently used
6188e877
AG
530 * metadata blocks (e.g. normal file I/O rewriting file time stamps) and
531 * all our journaled data blocks for journaled files (e.g. files in the
2332c443 532 * meta_fs like rindex, or files for which chattr +j was done.)
6188e877 533 * If we don't reserve enough space, corruption will follow.
2332c443 534 *
6188e877
AG
535 * We can have metadata blocks and jdata blocks in the same journal. Each
536 * type gets its own log descriptor, for which we need to reserve a block.
537 * In fact, each type has the potential for needing more than one log descriptor
538 * in cases where we have more blocks than will fit in a log descriptor.
2332c443 539 * Metadata journal entries take up half the space of journaled buffer entries.
2332c443
RP
540 *
541 * Also, we need to reserve blocks for revoke journal entries and one for an
542 * overall header for the lot.
543 *
544 * Returns: the number of blocks reserved
545 */
546static unsigned int calc_reserved(struct gfs2_sbd *sdp)
547{
548 unsigned int reserved = 0;
022ef4fe
SW
549 unsigned int mbuf;
550 unsigned int dbuf;
551 struct gfs2_trans *tr = sdp->sd_log_tr;
2332c443 552
022ef4fe
SW
553 if (tr) {
554 mbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
555 dbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
556 reserved = mbuf + dbuf;
557 /* Account for header blocks */
558 reserved += DIV_ROUND_UP(mbuf, buf_limit(sdp));
559 reserved += DIV_ROUND_UP(dbuf, databuf_limit(sdp));
560 }
2332c443 561
5d439758
AG
562 if (sdp->sd_log_committed_revoke > 0)
563 reserved += gfs2_struct2blk(sdp, sdp->sd_log_committed_revoke);
2332c443
RP
564 /* One for the overall header */
565 if (reserved)
566 reserved++;
567 return reserved;
568}
569
5cb738b5 570static void log_pull_tail(struct gfs2_sbd *sdp)
b3b94faa 571{
5cb738b5
AG
572 unsigned int new_tail = sdp->sd_log_flush_tail;
573 unsigned int dist;
b3b94faa 574
5cb738b5
AG
575 if (new_tail == sdp->sd_log_tail)
576 return;
577 dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
b3b94faa 578 ail2_empty(sdp, new_tail);
c1eba1b0 579 gfs2_log_release(sdp, dist);
b3b94faa
DT
580 sdp->sd_log_tail = new_tail;
581}
582
b3b94faa 583
9ff78289 584void log_flush_wait(struct gfs2_sbd *sdp)
b3b94faa 585{
16615be1
SW
586 DEFINE_WAIT(wait);
587
588 if (atomic_read(&sdp->sd_log_in_flight)) {
589 do {
590 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
591 TASK_UNINTERRUPTIBLE);
592 if (atomic_read(&sdp->sd_log_in_flight))
593 io_schedule();
594 } while(atomic_read(&sdp->sd_log_in_flight));
595 finish_wait(&sdp->sd_log_flush_wait, &wait);
b3b94faa 596 }
b3b94faa
DT
597}
598
45138990 599static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
4a36d08d 600{
45138990 601 struct gfs2_inode *ipa, *ipb;
4a36d08d 602
45138990
SW
603 ipa = list_entry(a, struct gfs2_inode, i_ordered);
604 ipb = list_entry(b, struct gfs2_inode, i_ordered);
4a36d08d 605
45138990 606 if (ipa->i_no_addr < ipb->i_no_addr)
4a36d08d 607 return -1;
45138990 608 if (ipa->i_no_addr > ipb->i_no_addr)
4a36d08d
BP
609 return 1;
610 return 0;
611}
612
7542486b
BP
613static void __ordered_del_inode(struct gfs2_inode *ip)
614{
615 if (!list_empty(&ip->i_ordered))
616 list_del_init(&ip->i_ordered);
617}
618
d7b616e2
SW
619static void gfs2_ordered_write(struct gfs2_sbd *sdp)
620{
45138990 621 struct gfs2_inode *ip;
d7b616e2
SW
622 LIST_HEAD(written);
623
45138990 624 spin_lock(&sdp->sd_ordered_lock);
a5b1d3fc
AG
625 list_sort(NULL, &sdp->sd_log_ordered, &ip_cmp);
626 while (!list_empty(&sdp->sd_log_ordered)) {
969183bc 627 ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
1f23bc78 628 if (ip->i_inode.i_mapping->nrpages == 0) {
7542486b 629 __ordered_del_inode(ip);
d7b616e2 630 continue;
1f23bc78
AD
631 }
632 list_move(&ip->i_ordered, &written);
45138990
SW
633 spin_unlock(&sdp->sd_ordered_lock);
634 filemap_fdatawrite(ip->i_inode.i_mapping);
635 spin_lock(&sdp->sd_ordered_lock);
d7b616e2 636 }
a5b1d3fc 637 list_splice(&written, &sdp->sd_log_ordered);
45138990 638 spin_unlock(&sdp->sd_ordered_lock);
d7b616e2
SW
639}
640
641static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
642{
45138990 643 struct gfs2_inode *ip;
d7b616e2 644
45138990 645 spin_lock(&sdp->sd_ordered_lock);
a5b1d3fc 646 while (!list_empty(&sdp->sd_log_ordered)) {
969183bc 647 ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
7542486b 648 __ordered_del_inode(ip);
45138990 649 if (ip->i_inode.i_mapping->nrpages == 0)
d7b616e2 650 continue;
45138990
SW
651 spin_unlock(&sdp->sd_ordered_lock);
652 filemap_fdatawait(ip->i_inode.i_mapping);
653 spin_lock(&sdp->sd_ordered_lock);
d7b616e2 654 }
45138990
SW
655 spin_unlock(&sdp->sd_ordered_lock);
656}
657
658void gfs2_ordered_del_inode(struct gfs2_inode *ip)
659{
660 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
661
662 spin_lock(&sdp->sd_ordered_lock);
7542486b 663 __ordered_del_inode(ip);
45138990 664 spin_unlock(&sdp->sd_ordered_lock);
d7b616e2
SW
665}
666
5d054964
BM
667void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
668{
669 struct buffer_head *bh = bd->bd_bh;
670 struct gfs2_glock *gl = bd->bd_gl;
671
f4e2f5e1
AG
672 sdp->sd_log_num_revoke++;
673 if (atomic_inc_return(&gl->gl_revokes) == 1)
674 gfs2_glock_hold(gl);
5d054964
BM
675 bh->b_private = NULL;
676 bd->bd_blkno = bh->b_blocknr;
9290a9a7
BP
677 gfs2_remove_from_ail(bd); /* drops ref on bh */
678 bd->bd_bh = NULL;
5d054964 679 set_bit(GLF_LFLUSH, &gl->gl_flags);
a5b1d3fc 680 list_add(&bd->bd_list, &sdp->sd_log_revokes);
5d054964
BM
681}
682
fe5e7ba1
BP
683void gfs2_glock_remove_revoke(struct gfs2_glock *gl)
684{
685 if (atomic_dec_return(&gl->gl_revokes) == 0) {
686 clear_bit(GLF_LFLUSH, &gl->gl_flags);
687 gfs2_glock_queue_put(gl);
688 }
689}
690
5e4c7632 691/**
e7501bf8 692 * gfs2_flush_revokes - Add as many revokes to the system transaction as we can
5e4c7632
BP
693 * @sdp: The GFS2 superblock
694 *
695 * Our usual strategy is to defer writing revokes as much as we can in the hope
696 * that we'll eventually overwrite the journal, which will make those revokes
697 * go away. This changes when we flush the log: at that point, there will
698 * likely be some left-over space in the last revoke block of that transaction.
699 * We can fill that space with additional revokes for blocks that have already
700 * been written back. This will basically come at no cost now, and will save
701 * us from having to keep track of those blocks on the AIL2 list later.
702 */
e7501bf8 703void gfs2_flush_revokes(struct gfs2_sbd *sdp)
5d054964 704{
5e4c7632 705 /* number of revokes we still have room for */
5a4e9c60 706 unsigned int max_revokes;
5d054964 707
5e4c7632 708 gfs2_log_lock(sdp);
5a4e9c60
AG
709 max_revokes = sdp->sd_ldptrs;
710 if (sdp->sd_log_num_revoke > sdp->sd_ldptrs)
711 max_revokes += roundup(sdp->sd_log_num_revoke - sdp->sd_ldptrs,
712 sdp->sd_inptrs);
5d054964
BM
713 max_revokes -= sdp->sd_log_num_revoke;
714 if (!sdp->sd_log_num_revoke) {
715 atomic_dec(&sdp->sd_log_blks_free);
716 /* If no blocks have been reserved, we need to also
717 * reserve a block for the header */
77650bdb 718 if (!sdp->sd_log_blks_reserved) {
5d054964 719 atomic_dec(&sdp->sd_log_blks_free);
77650bdb
BP
720 trace_gfs2_log_blocks(sdp, -2);
721 } else {
722 trace_gfs2_log_blocks(sdp, -1);
723 }
5d054964 724 }
5e4c7632 725 gfs2_ail1_empty(sdp, max_revokes);
5d054964
BM
726 gfs2_log_unlock(sdp);
727
728 if (!sdp->sd_log_num_revoke) {
729 atomic_inc(&sdp->sd_log_blks_free);
77650bdb 730 if (!sdp->sd_log_blks_reserved) {
5d054964 731 atomic_inc(&sdp->sd_log_blks_free);
77650bdb
BP
732 trace_gfs2_log_blocks(sdp, 2);
733 } else {
734 trace_gfs2_log_blocks(sdp, 1);
735 }
5d054964
BM
736 }
737}
738
34cc1781 739/**
7c70b896 740 * gfs2_write_log_header - Write a journal log header buffer at lblock
34cc1781 741 * @sdp: The GFS2 superblock
c1696fb8 742 * @jd: journal descriptor of the journal to which we are writing
588bff95
BP
743 * @seq: sequence number
744 * @tail: tail of the log
7c70b896 745 * @lblock: value for lh_blkno (block number relative to start of journal)
c1696fb8 746 * @flags: log header flags GFS2_LOG_HEAD_*
588bff95 747 * @op_flags: flags to pass to the bio
34cc1781
SW
748 *
749 * Returns: the initialized log buffer descriptor
750 */
751
c1696fb8 752void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
7c70b896
BP
753 u64 seq, u32 tail, u32 lblock, u32 flags,
754 int op_flags)
34cc1781 755{
34cc1781 756 struct gfs2_log_header *lh;
c1696fb8 757 u32 hash, crc;
ade48088 758 struct page *page;
c1696fb8
BP
759 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
760 struct timespec64 tv;
761 struct super_block *sb = sdp->sd_vfs;
7c70b896 762 u64 dblock;
588bff95 763
ade48088 764 if (gfs2_withdrawn(sdp))
4a3d049d 765 return;
ade48088
BP
766
767 page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
e8c92ed7
SW
768 lh = page_address(page);
769 clear_page(lh);
34cc1781 770
34cc1781
SW
771 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
772 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
773 lh->lh_header.__pad0 = cpu_to_be64(0);
774 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
775 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
588bff95 776 lh->lh_sequence = cpu_to_be64(seq);
34cc1781
SW
777 lh->lh_flags = cpu_to_be32(flags);
778 lh->lh_tail = cpu_to_be32(tail);
7c70b896 779 lh->lh_blkno = cpu_to_be32(lblock);
c1696fb8 780 hash = ~crc32(~0, lh, LH_V1_SIZE);
34cc1781
SW
781 lh->lh_hash = cpu_to_be32(hash);
782
ee9c7f9a 783 ktime_get_coarse_real_ts64(&tv);
c1696fb8
BP
784 lh->lh_nsec = cpu_to_be32(tv.tv_nsec);
785 lh->lh_sec = cpu_to_be64(tv.tv_sec);
7c70b896 786 if (!list_empty(&jd->extent_list))
19ebc050 787 dblock = gfs2_log_bmap(jd, lblock);
7c70b896
BP
788 else {
789 int ret = gfs2_lblk_to_dblk(jd->jd_inode, lblock, &dblock);
790 if (gfs2_assert_withdraw(sdp, ret == 0))
791 return;
792 }
793 lh->lh_addr = cpu_to_be64(dblock);
c1696fb8
BP
794 lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr);
795
796 /* We may only write local statfs, quota, etc., when writing to our
797 own journal. The values are left 0 when recovering a journal
798 different from our own. */
799 if (!(flags & GFS2_LOG_HEAD_RECOVERY)) {
800 lh->lh_statfs_addr =
801 cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr);
802 lh->lh_quota_addr =
803 cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr);
804
805 spin_lock(&sdp->sd_statfs_spin);
806 lh->lh_local_total = cpu_to_be64(l_sc->sc_total);
807 lh->lh_local_free = cpu_to_be64(l_sc->sc_free);
808 lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes);
809 spin_unlock(&sdp->sd_statfs_spin);
810 }
811
812 BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE);
813
814 crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
815 sb->s_blocksize - LH_V1_SIZE - 4);
816 lh->lh_crc = cpu_to_be32(crc);
817
7c70b896 818 gfs2_log_write(sdp, page, sb->s_blocksize, 0, dblock);
f4686c26 819 gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE | op_flags);
588bff95
BP
820}
821
822/**
823 * log_write_header - Get and initialize a journal header buffer
824 * @sdp: The GFS2 superblock
c1696fb8 825 * @flags: The log header flags, including log header origin
588bff95
BP
826 *
827 * Returns: the initialized log buffer descriptor
828 */
829
830static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
831{
588bff95
BP
832 int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
833 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
834
835 gfs2_assert_withdraw(sdp, (state != SFS_FROZEN));
588bff95 836
34cc1781
SW
837 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
838 gfs2_ordered_wait(sdp);
839 log_flush_wait(sdp);
70fd7614 840 op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
34cc1781 841 }
5cb738b5
AG
842 sdp->sd_log_idle = (sdp->sd_log_flush_tail == sdp->sd_log_flush_head);
843 gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++,
844 sdp->sd_log_flush_tail, sdp->sd_log_flush_head,
845 flags, op_flags);
19ebc050 846 gfs2_log_incr_head(sdp);
4a3d049d 847 log_flush_wait(sdp);
5cb738b5 848 log_pull_tail(sdp);
34cc1781
SW
849}
850
2ca0c2fb
BP
851/**
852 * ail_drain - drain the ail lists after a withdraw
853 * @sdp: Pointer to GFS2 superblock
854 */
855static void ail_drain(struct gfs2_sbd *sdp)
856{
857 struct gfs2_trans *tr;
858
859 spin_lock(&sdp->sd_ail_lock);
860 /*
861 * For transactions on the sd_ail1_list we need to drain both the
862 * ail1 and ail2 lists. That's because function gfs2_ail1_start_one
863 * (temporarily) moves items from its tr_ail1 list to tr_ail2 list
864 * before revokes are sent for that block. Items on the sd_ail2_list
865 * should have already gotten beyond that point, so no need.
866 */
867 while (!list_empty(&sdp->sd_ail1_list)) {
868 tr = list_first_entry(&sdp->sd_ail1_list, struct gfs2_trans,
869 tr_list);
870 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail1_list);
871 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
872 list_del(&tr->tr_list);
b839dada 873 gfs2_trans_free(sdp, tr);
2ca0c2fb
BP
874 }
875 while (!list_empty(&sdp->sd_ail2_list)) {
876 tr = list_first_entry(&sdp->sd_ail2_list, struct gfs2_trans,
877 tr_list);
878 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
879 list_del(&tr->tr_list);
b839dada 880 gfs2_trans_free(sdp, tr);
2ca0c2fb
BP
881 }
882 spin_unlock(&sdp->sd_ail_lock);
883}
884
d5dc3d96
BP
885/**
886 * empty_ail1_list - try to start IO and empty the ail1 list
887 * @sdp: Pointer to GFS2 superblock
888 */
889static void empty_ail1_list(struct gfs2_sbd *sdp)
890{
891 unsigned long start = jiffies;
892
893 for (;;) {
894 if (time_after(jiffies, start + (HZ * 600))) {
895 fs_err(sdp, "Error: In %s for 10 minutes! t=%d\n",
896 __func__, current->journal_info ? 1 : 0);
897 dump_ail_list(sdp);
898 return;
899 }
900 gfs2_ail1_start(sdp);
901 gfs2_ail1_wait(sdp);
902 if (gfs2_ail1_empty(sdp, 0))
903 return;
904 }
905}
906
462582b9 907/**
521031fa 908 * trans_drain - drain the buf and databuf queue for a failed transaction
462582b9
BP
909 * @tr: the transaction to drain
910 *
911 * When this is called, we're taking an error exit for a log write that failed
912 * but since we bypassed the after_commit functions, we need to remove the
913 * items from the buf and databuf queue.
914 */
915static void trans_drain(struct gfs2_trans *tr)
916{
917 struct gfs2_bufdata *bd;
918 struct list_head *head;
919
920 if (!tr)
921 return;
922
923 head = &tr->tr_buf;
924 while (!list_empty(head)) {
925 bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
926 list_del_init(&bd->bd_list);
927 kmem_cache_free(gfs2_bufdata_cachep, bd);
928 }
929 head = &tr->tr_databuf;
930 while (!list_empty(head)) {
931 bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
932 list_del_init(&bd->bd_list);
933 kmem_cache_free(gfs2_bufdata_cachep, bd);
934 }
935}
936
b3b94faa 937/**
b09e593d 938 * gfs2_log_flush - flush incore transaction(s)
b3b94faa
DT
939 * @sdp: the filesystem
940 * @gl: The glock structure to flush. If NULL, flush the whole incore log
805c0907 941 * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
b3b94faa
DT
942 *
943 */
944
c1696fb8 945void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
b3b94faa 946{
2ca0c2fb 947 struct gfs2_trans *tr = NULL;
2e60d768 948 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
b3b94faa 949
484adff8 950 down_write(&sdp->sd_log_flush_lock);
f55ab26a 951
2ca0c2fb
BP
952 /*
953 * Do this check while holding the log_flush_lock to prevent new
954 * buffers from being added to the ail via gfs2_pin()
955 */
956 if (gfs2_withdrawn(sdp))
957 goto out;
958
2bcd610d 959 /* Log might have been flushed while we waited for the flush lock */
5a61ae14
AG
960 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags))
961 goto out;
805c0907 962 trace_gfs2_log_flush(sdp, 1, flags);
f55ab26a 963
c1696fb8 964 if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN)
400ac52e
BM
965 clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
966
b1ab1e44 967 sdp->sd_log_flush_head = sdp->sd_log_head;
16ca9412
BM
968 tr = sdp->sd_log_tr;
969 if (tr) {
970 sdp->sd_log_tr = NULL;
b1ab1e44 971 tr->tr_first = sdp->sd_log_flush_head;
2e60d768 972 if (unlikely (state == SFS_FROZEN))
ca399c96
BP
973 if (gfs2_assert_withdraw_delayed(sdp,
974 !tr->tr_num_buf_new && !tr->tr_num_databuf_new))
5a61ae14 975 goto out_withdraw;
16ca9412 976 }
b3b94faa 977
2e60d768 978 if (unlikely(state == SFS_FROZEN))
ca399c96 979 if (gfs2_assert_withdraw_delayed(sdp, !sdp->sd_log_num_revoke))
5a61ae14 980 goto out_withdraw;
ca399c96
BP
981 if (gfs2_assert_withdraw_delayed(sdp,
982 sdp->sd_log_num_revoke == sdp->sd_log_committed_revoke))
5a61ae14 983 goto out_withdraw;
b3b94faa 984
d7b616e2 985 gfs2_ordered_write(sdp);
2ca0c2fb 986 if (gfs2_withdrawn(sdp))
5a61ae14 987 goto out_withdraw;
d69a3c65 988 lops_before_commit(sdp, tr);
2ca0c2fb 989 if (gfs2_withdrawn(sdp))
5a61ae14 990 goto out_withdraw;
f4686c26 991 gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE);
2ca0c2fb 992 if (gfs2_withdrawn(sdp))
5a61ae14 993 goto out_withdraw;
d7b616e2 994
34cc1781 995 if (sdp->sd_log_head != sdp->sd_log_flush_head) {
428fd95d 996 log_flush_wait(sdp);
c1696fb8 997 log_write_header(sdp, flags);
5cb738b5 998 } else if (sdp->sd_log_tail != sdp->sd_log_flush_tail && !sdp->sd_log_idle) {
fd041f0b 999 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
63997775 1000 trace_gfs2_log_blocks(sdp, -1);
c1696fb8 1001 log_write_header(sdp, flags);
2332c443 1002 }
2ca0c2fb 1003 if (gfs2_withdrawn(sdp))
5a61ae14 1004 goto out_withdraw;
16ca9412 1005 lops_after_commit(sdp, tr);
b09e593d 1006
fe1a698f 1007 gfs2_log_lock(sdp);
5cb738b5 1008 gfs2_log_update_head(sdp);
faa31ce8 1009 sdp->sd_log_blks_reserved = 0;
5d439758 1010 sdp->sd_log_committed_revoke = 0;
b3b94faa 1011
d6a079e8 1012 spin_lock(&sdp->sd_ail_lock);
16ca9412
BM
1013 if (tr && !list_empty(&tr->tr_ail1_list)) {
1014 list_add(&tr->tr_list, &sdp->sd_ail1_list);
1015 tr = NULL;
b3b94faa 1016 }
d6a079e8 1017 spin_unlock(&sdp->sd_ail_lock);
b3b94faa 1018 gfs2_log_unlock(sdp);
24972557 1019
c1696fb8 1020 if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
24972557 1021 if (!sdp->sd_log_idle) {
d5dc3d96 1022 empty_ail1_list(sdp);
30fe70a8 1023 if (gfs2_withdrawn(sdp))
5a61ae14 1024 goto out_withdraw;
24972557
BM
1025 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
1026 trace_gfs2_log_blocks(sdp, -1);
c1696fb8 1027 log_write_header(sdp, flags);
5cb738b5 1028 gfs2_log_update_head(sdp);
24972557 1029 }
c1696fb8
BP
1030 if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
1031 GFS2_LOG_HEAD_FLUSH_FREEZE))
24972557 1032 gfs2_log_shutdown(sdp);
c1696fb8 1033 if (flags & GFS2_LOG_HEAD_FLUSH_FREEZE)
2e60d768 1034 atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
24972557
BM
1035 }
1036
5a61ae14 1037out_end:
805c0907 1038 trace_gfs2_log_flush(sdp, 0, flags);
5a61ae14 1039out:
484adff8 1040 up_write(&sdp->sd_log_flush_lock);
b839dada 1041 gfs2_trans_free(sdp, tr);
5a61ae14
AG
1042 if (gfs2_withdrawing(sdp))
1043 gfs2_withdraw(sdp);
1044 return;
1045
1046out_withdraw:
1047 trans_drain(tr);
1048 /**
1049 * If the tr_list is empty, we're withdrawing during a log
1050 * flush that targets a transaction, but the transaction was
1051 * never queued onto any of the ail lists. Here we add it to
1052 * ail1 just so that ail_drain() will find and free it.
1053 */
1054 spin_lock(&sdp->sd_ail_lock);
1055 if (tr && list_empty(&tr->tr_list))
1056 list_add(&tr->tr_list, &sdp->sd_ail1_list);
1057 spin_unlock(&sdp->sd_ail_lock);
1058 ail_drain(sdp); /* frees all transactions */
1059 tr = NULL;
1060 goto out_end;
b3b94faa
DT
1061}
1062
d69a3c65
SW
1063/**
1064 * gfs2_merge_trans - Merge a new transaction into a cached transaction
1065 * @old: Original transaction to be expanded
1066 * @new: New transaction to be merged
1067 */
1068
83d060ca 1069static void gfs2_merge_trans(struct gfs2_sbd *sdp, struct gfs2_trans *new)
d69a3c65 1070{
83d060ca
BP
1071 struct gfs2_trans *old = sdp->sd_log_tr;
1072
9862ca05 1073 WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
d69a3c65
SW
1074
1075 old->tr_num_buf_new += new->tr_num_buf_new;
1076 old->tr_num_databuf_new += new->tr_num_databuf_new;
1077 old->tr_num_buf_rm += new->tr_num_buf_rm;
1078 old->tr_num_databuf_rm += new->tr_num_databuf_rm;
1079 old->tr_num_revoke += new->tr_num_revoke;
a31b4ec5 1080 old->tr_num_revoke_rm += new->tr_num_revoke_rm;
d69a3c65
SW
1081
1082 list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
1083 list_splice_tail_init(&new->tr_buf, &old->tr_buf);
83d060ca
BP
1084
1085 spin_lock(&sdp->sd_ail_lock);
1086 list_splice_tail_init(&new->tr_ail1_list, &old->tr_ail1_list);
1087 list_splice_tail_init(&new->tr_ail2_list, &old->tr_ail2_list);
1088 spin_unlock(&sdp->sd_ail_lock);
d69a3c65
SW
1089}
1090
b3b94faa
DT
1091static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1092{
2332c443 1093 unsigned int reserved;
ac39aadd 1094 unsigned int unused;
022ef4fe 1095 unsigned int maxres;
b3b94faa
DT
1096
1097 gfs2_log_lock(sdp);
1098
022ef4fe 1099 if (sdp->sd_log_tr) {
83d060ca 1100 gfs2_merge_trans(sdp, tr);
022ef4fe 1101 } else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
c968f578 1102 gfs2_assert_withdraw(sdp, !test_bit(TR_ONSTACK, &tr->tr_flags));
022ef4fe 1103 sdp->sd_log_tr = tr;
9862ca05 1104 set_bit(TR_ATTACHED, &tr->tr_flags);
022ef4fe
SW
1105 }
1106
a31b4ec5 1107 sdp->sd_log_committed_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
2332c443 1108 reserved = calc_reserved(sdp);
022ef4fe
SW
1109 maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
1110 gfs2_assert_withdraw(sdp, maxres >= reserved);
1111 unused = maxres - reserved;
5ae8fff8
AG
1112 if (unused)
1113 gfs2_log_release(sdp, unused);
b3b94faa
DT
1114 sdp->sd_log_blks_reserved = reserved;
1115
1116 gfs2_log_unlock(sdp);
1117}
1118
1119/**
1120 * gfs2_log_commit - Commit a transaction to the log
1121 * @sdp: the filesystem
1122 * @tr: the transaction
1123 *
5e687eac
BM
1124 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
1125 * or the total number of used blocks (pinned blocks plus AIL blocks)
1126 * is greater than thresh2.
1127 *
b57bc0fb 1128 * At mount time thresh1 is 2/5ths of journal size, thresh2 is 4/5ths of
5e687eac
BM
1129 * journal size.
1130 *
b3b94faa
DT
1131 * Returns: errno
1132 */
1133
1134void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1135{
1136 log_refund(sdp, tr);
b3b94faa 1137
5e687eac
BM
1138 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
1139 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
1140 atomic_read(&sdp->sd_log_thresh2)))
1141 wake_up(&sdp->sd_logd_waitq);
b3b94faa
DT
1142}
1143
1144/**
1145 * gfs2_log_shutdown - write a shutdown header into a journal
1146 * @sdp: the filesystem
1147 *
1148 */
1149
feed98a8 1150static void gfs2_log_shutdown(struct gfs2_sbd *sdp)
b3b94faa 1151{
b3b94faa 1152 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
b3b94faa 1153 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
b3b94faa
DT
1154 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
1155
1156 sdp->sd_log_flush_head = sdp->sd_log_head;
b3b94faa 1157
805c0907 1158 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN);
b3b94faa 1159
a74604be
SW
1160 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
1161 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
b3b94faa 1162
5cb738b5 1163 gfs2_log_update_head(sdp);
b3b94faa 1164 sdp->sd_log_tail = sdp->sd_log_head;
a25311c8
SW
1165}
1166
5e687eac
BM
1167static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
1168{
f07b3520
BP
1169 return (atomic_read(&sdp->sd_log_pinned) +
1170 atomic_read(&sdp->sd_log_blks_needed) >=
1171 atomic_read(&sdp->sd_log_thresh1));
5e687eac
BM
1172}
1173
1174static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
1175{
1176 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
b066a4ee
AD
1177
1178 if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags))
1179 return 1;
1180
f07b3520
BP
1181 return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >=
1182 atomic_read(&sdp->sd_log_thresh2);
5e687eac 1183}
ec69b188
SW
1184
1185/**
1186 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
1187 * @sdp: Pointer to GFS2 superblock
1188 *
1189 * Also, periodically check to make sure that we're using the most recent
1190 * journal index.
1191 */
1192
1193int gfs2_logd(void *data)
1194{
1195 struct gfs2_sbd *sdp = data;
5e687eac
BM
1196 unsigned long t = 1;
1197 DEFINE_WAIT(wait);
ec69b188
SW
1198
1199 while (!kthread_should_stop()) {
ec69b188 1200
d22f69a0
BP
1201 if (gfs2_withdrawn(sdp)) {
1202 msleep_interruptible(HZ);
1203 continue;
1204 }
942b0cdd
BP
1205 /* Check for errors writing to the journal */
1206 if (sdp->sd_log_error) {
badb55ec
AG
1207 gfs2_lm(sdp,
1208 "GFS2: fsid=%s: error %d: "
1209 "withdrawing the file system to "
1210 "prevent further damage.\n",
1211 sdp->sd_fsname, sdp->sd_log_error);
1212 gfs2_withdraw(sdp);
d22f69a0 1213 continue;
942b0cdd
BP
1214 }
1215
5e687eac 1216 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
5e4c7632 1217 gfs2_ail1_empty(sdp, 0);
805c0907
BP
1218 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1219 GFS2_LFC_LOGD_JFLUSH_REQD);
5e687eac 1220 }
ec69b188 1221
5e687eac
BM
1222 if (gfs2_ail_flush_reqd(sdp)) {
1223 gfs2_ail1_start(sdp);
26b06a69 1224 gfs2_ail1_wait(sdp);
5e4c7632 1225 gfs2_ail1_empty(sdp, 0);
805c0907
BP
1226 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1227 GFS2_LFC_LOGD_AIL_FLUSH_REQD);
ec69b188
SW
1228 }
1229
ec69b188 1230 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
a0acae0e
TH
1231
1232 try_to_freeze();
5e687eac
BM
1233
1234 do {
1235 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
5f487490 1236 TASK_INTERRUPTIBLE);
5e687eac
BM
1237 if (!gfs2_ail_flush_reqd(sdp) &&
1238 !gfs2_jrnl_flush_reqd(sdp) &&
1239 !kthread_should_stop())
1240 t = schedule_timeout(t);
1241 } while(t && !gfs2_ail_flush_reqd(sdp) &&
1242 !gfs2_jrnl_flush_reqd(sdp) &&
1243 !kthread_should_stop());
1244 finish_wait(&sdp->sd_logd_waitq, &wait);
ec69b188
SW
1245 }
1246
1247 return 0;
1248}
1249