]> Git Repo - linux.git/blame - fs/gfs2/log.c
gfs2: Minor calc_reserved cleanup
[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
76fce654
AG
267/**
268 * gfs2_ail_empty_tr - empty one of the ail lists of a transaction
269 */
270
271static void gfs2_ail_empty_tr(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
272 struct list_head *head)
273{
274 struct gfs2_bufdata *bd;
275
276 while (!list_empty(head)) {
277 bd = list_first_entry(head, struct gfs2_bufdata,
278 bd_ail_st_list);
279 gfs2_assert(sdp, bd->bd_tr == tr);
280 gfs2_remove_from_ail(bd);
281 }
282}
283
ddacfaf7
SW
284/**
285 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
286 * @sdp: the filesystem
5e4c7632
BP
287 * @tr: the transaction
288 * @max_revokes: If nonzero, issue revokes for the bd items for written buffers
ddacfaf7 289 *
36c78309 290 * returns: the transaction's count of remaining active items
ddacfaf7
SW
291 */
292
36c78309 293static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
5e4c7632 294 int *max_revokes)
ddacfaf7
SW
295{
296 struct gfs2_bufdata *bd, *s;
297 struct buffer_head *bh;
36c78309 298 int active_count = 0;
ddacfaf7 299
16ca9412 300 list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
ddacfaf7
SW
301 bd_ail_st_list) {
302 bh = bd->bd_bh;
16ca9412 303 gfs2_assert(sdp, bd->bd_tr == tr);
036330c9
BP
304 /*
305 * If another process flagged an io error, e.g. writing to the
306 * journal, error all other bhs and move them off the ail1 to
307 * prevent a tight loop when unmount tries to flush ail1,
308 * regardless of whether they're still busy. If no outside
309 * errors were found and the buffer is busy, move to the next.
310 * If the ail buffer is not busy and caught an error, flag it
311 * for others.
312 */
36c78309
BP
313 if (!sdp->sd_log_error && buffer_busy(bh)) {
314 active_count++;
4667a0ec 315 continue;
36c78309 316 }
b524abcc 317 if (!buffer_uptodate(bh) &&
036330c9 318 !cmpxchg(&sdp->sd_log_error, 0, -EIO)) {
ddacfaf7 319 gfs2_io_error_bh(sdp, bh);
69511080 320 gfs2_withdraw_delayed(sdp);
9e1a9ecd 321 }
5e4c7632
BP
322 /*
323 * If we have space for revokes and the bd is no longer on any
324 * buf list, we can just add a revoke for it immediately and
325 * avoid having to put it on the ail2 list, where it would need
326 * to be revoked later.
327 */
328 if (*max_revokes && list_empty(&bd->bd_list)) {
329 gfs2_add_revoke(sdp, bd);
330 (*max_revokes)--;
331 continue;
332 }
16ca9412 333 list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
ddacfaf7 334 }
36c78309 335 return active_count;
ddacfaf7
SW
336}
337
4667a0ec
SW
338/**
339 * gfs2_ail1_empty - Try to empty the ail1 lists
340 * @sdp: The superblock
5e4c7632 341 * @max_revokes: If non-zero, add revokes where appropriate
4667a0ec
SW
342 *
343 * Tries to empty the ail1 lists, starting with the oldest first
344 */
b3b94faa 345
5e4c7632 346static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int max_revokes)
b3b94faa 347{
16ca9412 348 struct gfs2_trans *tr, *s;
5d054964 349 int oldest_tr = 1;
b3b94faa
DT
350 int ret;
351
d6a079e8 352 spin_lock(&sdp->sd_ail_lock);
16ca9412 353 list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
36c78309 354 if (!gfs2_ail1_empty_one(sdp, tr, &max_revokes) && oldest_tr)
16ca9412 355 list_move(&tr->tr_list, &sdp->sd_ail2_list);
4667a0ec 356 else
5d054964 357 oldest_tr = 0;
b3b94faa 358 }
5cb738b5 359 gfs2_log_update_flush_tail(sdp);
b3b94faa 360 ret = list_empty(&sdp->sd_ail1_list);
d6a079e8 361 spin_unlock(&sdp->sd_ail_lock);
b3b94faa 362
69511080 363 if (test_bit(SDF_WITHDRAWING, &sdp->sd_flags)) {
badb55ec
AG
364 gfs2_lm(sdp, "fatal: I/O error(s)\n");
365 gfs2_withdraw(sdp);
366 }
9e1a9ecd 367
b3b94faa
DT
368 return ret;
369}
370
26b06a69
SW
371static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
372{
16ca9412 373 struct gfs2_trans *tr;
26b06a69
SW
374 struct gfs2_bufdata *bd;
375 struct buffer_head *bh;
376
377 spin_lock(&sdp->sd_ail_lock);
16ca9412
BM
378 list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
379 list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
26b06a69
SW
380 bh = bd->bd_bh;
381 if (!buffer_locked(bh))
382 continue;
383 get_bh(bh);
384 spin_unlock(&sdp->sd_ail_lock);
385 wait_on_buffer(bh);
386 brelse(bh);
387 return;
388 }
389 }
390 spin_unlock(&sdp->sd_ail_lock);
391}
ddacfaf7 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;
71b219f4 549 unsigned int blocks;
022ef4fe 550 struct gfs2_trans *tr = sdp->sd_log_tr;
2332c443 551
022ef4fe 552 if (tr) {
71b219f4
AG
553 blocks = tr->tr_num_buf_new - tr->tr_num_buf_rm;
554 reserved += blocks + DIV_ROUND_UP(blocks, buf_limit(sdp));
555 blocks = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
556 reserved += blocks + DIV_ROUND_UP(blocks, databuf_limit(sdp));
022ef4fe 557 }
2332c443 558
5d439758
AG
559 if (sdp->sd_log_committed_revoke > 0)
560 reserved += gfs2_struct2blk(sdp, sdp->sd_log_committed_revoke);
2332c443
RP
561 /* One for the overall header */
562 if (reserved)
563 reserved++;
564 return reserved;
565}
566
5cb738b5 567static void log_pull_tail(struct gfs2_sbd *sdp)
b3b94faa 568{
5cb738b5
AG
569 unsigned int new_tail = sdp->sd_log_flush_tail;
570 unsigned int dist;
b3b94faa 571
5cb738b5
AG
572 if (new_tail == sdp->sd_log_tail)
573 return;
574 dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
b3b94faa 575 ail2_empty(sdp, new_tail);
c1eba1b0 576 gfs2_log_release(sdp, dist);
b3b94faa
DT
577 sdp->sd_log_tail = new_tail;
578}
579
b3b94faa 580
9ff78289 581void log_flush_wait(struct gfs2_sbd *sdp)
b3b94faa 582{
16615be1
SW
583 DEFINE_WAIT(wait);
584
585 if (atomic_read(&sdp->sd_log_in_flight)) {
586 do {
587 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
588 TASK_UNINTERRUPTIBLE);
589 if (atomic_read(&sdp->sd_log_in_flight))
590 io_schedule();
591 } while(atomic_read(&sdp->sd_log_in_flight));
592 finish_wait(&sdp->sd_log_flush_wait, &wait);
b3b94faa 593 }
b3b94faa
DT
594}
595
45138990 596static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
4a36d08d 597{
45138990 598 struct gfs2_inode *ipa, *ipb;
4a36d08d 599
45138990
SW
600 ipa = list_entry(a, struct gfs2_inode, i_ordered);
601 ipb = list_entry(b, struct gfs2_inode, i_ordered);
4a36d08d 602
45138990 603 if (ipa->i_no_addr < ipb->i_no_addr)
4a36d08d 604 return -1;
45138990 605 if (ipa->i_no_addr > ipb->i_no_addr)
4a36d08d
BP
606 return 1;
607 return 0;
608}
609
7542486b
BP
610static void __ordered_del_inode(struct gfs2_inode *ip)
611{
612 if (!list_empty(&ip->i_ordered))
613 list_del_init(&ip->i_ordered);
614}
615
d7b616e2
SW
616static void gfs2_ordered_write(struct gfs2_sbd *sdp)
617{
45138990 618 struct gfs2_inode *ip;
d7b616e2
SW
619 LIST_HEAD(written);
620
45138990 621 spin_lock(&sdp->sd_ordered_lock);
a5b1d3fc
AG
622 list_sort(NULL, &sdp->sd_log_ordered, &ip_cmp);
623 while (!list_empty(&sdp->sd_log_ordered)) {
969183bc 624 ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
1f23bc78 625 if (ip->i_inode.i_mapping->nrpages == 0) {
7542486b 626 __ordered_del_inode(ip);
d7b616e2 627 continue;
1f23bc78
AD
628 }
629 list_move(&ip->i_ordered, &written);
45138990
SW
630 spin_unlock(&sdp->sd_ordered_lock);
631 filemap_fdatawrite(ip->i_inode.i_mapping);
632 spin_lock(&sdp->sd_ordered_lock);
d7b616e2 633 }
a5b1d3fc 634 list_splice(&written, &sdp->sd_log_ordered);
45138990 635 spin_unlock(&sdp->sd_ordered_lock);
d7b616e2
SW
636}
637
638static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
639{
45138990 640 struct gfs2_inode *ip;
d7b616e2 641
45138990 642 spin_lock(&sdp->sd_ordered_lock);
a5b1d3fc 643 while (!list_empty(&sdp->sd_log_ordered)) {
969183bc 644 ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
7542486b 645 __ordered_del_inode(ip);
45138990 646 if (ip->i_inode.i_mapping->nrpages == 0)
d7b616e2 647 continue;
45138990
SW
648 spin_unlock(&sdp->sd_ordered_lock);
649 filemap_fdatawait(ip->i_inode.i_mapping);
650 spin_lock(&sdp->sd_ordered_lock);
d7b616e2 651 }
45138990
SW
652 spin_unlock(&sdp->sd_ordered_lock);
653}
654
655void gfs2_ordered_del_inode(struct gfs2_inode *ip)
656{
657 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
658
659 spin_lock(&sdp->sd_ordered_lock);
7542486b 660 __ordered_del_inode(ip);
45138990 661 spin_unlock(&sdp->sd_ordered_lock);
d7b616e2
SW
662}
663
5d054964
BM
664void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
665{
666 struct buffer_head *bh = bd->bd_bh;
667 struct gfs2_glock *gl = bd->bd_gl;
668
f4e2f5e1
AG
669 sdp->sd_log_num_revoke++;
670 if (atomic_inc_return(&gl->gl_revokes) == 1)
671 gfs2_glock_hold(gl);
5d054964
BM
672 bh->b_private = NULL;
673 bd->bd_blkno = bh->b_blocknr;
9290a9a7
BP
674 gfs2_remove_from_ail(bd); /* drops ref on bh */
675 bd->bd_bh = NULL;
5d054964 676 set_bit(GLF_LFLUSH, &gl->gl_flags);
a5b1d3fc 677 list_add(&bd->bd_list, &sdp->sd_log_revokes);
5d054964
BM
678}
679
fe5e7ba1
BP
680void gfs2_glock_remove_revoke(struct gfs2_glock *gl)
681{
682 if (atomic_dec_return(&gl->gl_revokes) == 0) {
683 clear_bit(GLF_LFLUSH, &gl->gl_flags);
684 gfs2_glock_queue_put(gl);
685 }
686}
687
5e4c7632 688/**
e7501bf8 689 * gfs2_flush_revokes - Add as many revokes to the system transaction as we can
5e4c7632
BP
690 * @sdp: The GFS2 superblock
691 *
692 * Our usual strategy is to defer writing revokes as much as we can in the hope
693 * that we'll eventually overwrite the journal, which will make those revokes
694 * go away. This changes when we flush the log: at that point, there will
695 * likely be some left-over space in the last revoke block of that transaction.
696 * We can fill that space with additional revokes for blocks that have already
697 * been written back. This will basically come at no cost now, and will save
698 * us from having to keep track of those blocks on the AIL2 list later.
699 */
e7501bf8 700void gfs2_flush_revokes(struct gfs2_sbd *sdp)
5d054964 701{
5e4c7632 702 /* number of revokes we still have room for */
5a4e9c60 703 unsigned int max_revokes;
5d054964 704
5e4c7632 705 gfs2_log_lock(sdp);
5a4e9c60
AG
706 max_revokes = sdp->sd_ldptrs;
707 if (sdp->sd_log_num_revoke > sdp->sd_ldptrs)
708 max_revokes += roundup(sdp->sd_log_num_revoke - sdp->sd_ldptrs,
709 sdp->sd_inptrs);
5d054964
BM
710 max_revokes -= sdp->sd_log_num_revoke;
711 if (!sdp->sd_log_num_revoke) {
712 atomic_dec(&sdp->sd_log_blks_free);
713 /* If no blocks have been reserved, we need to also
714 * reserve a block for the header */
77650bdb 715 if (!sdp->sd_log_blks_reserved) {
5d054964 716 atomic_dec(&sdp->sd_log_blks_free);
77650bdb
BP
717 trace_gfs2_log_blocks(sdp, -2);
718 } else {
719 trace_gfs2_log_blocks(sdp, -1);
720 }
5d054964 721 }
5e4c7632 722 gfs2_ail1_empty(sdp, max_revokes);
5d054964
BM
723 gfs2_log_unlock(sdp);
724
725 if (!sdp->sd_log_num_revoke) {
726 atomic_inc(&sdp->sd_log_blks_free);
77650bdb 727 if (!sdp->sd_log_blks_reserved) {
5d054964 728 atomic_inc(&sdp->sd_log_blks_free);
77650bdb
BP
729 trace_gfs2_log_blocks(sdp, 2);
730 } else {
731 trace_gfs2_log_blocks(sdp, 1);
732 }
5d054964
BM
733 }
734}
735
34cc1781 736/**
7c70b896 737 * gfs2_write_log_header - Write a journal log header buffer at lblock
34cc1781 738 * @sdp: The GFS2 superblock
c1696fb8 739 * @jd: journal descriptor of the journal to which we are writing
588bff95
BP
740 * @seq: sequence number
741 * @tail: tail of the log
7c70b896 742 * @lblock: value for lh_blkno (block number relative to start of journal)
c1696fb8 743 * @flags: log header flags GFS2_LOG_HEAD_*
588bff95 744 * @op_flags: flags to pass to the bio
34cc1781
SW
745 *
746 * Returns: the initialized log buffer descriptor
747 */
748
c1696fb8 749void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
7c70b896
BP
750 u64 seq, u32 tail, u32 lblock, u32 flags,
751 int op_flags)
34cc1781 752{
34cc1781 753 struct gfs2_log_header *lh;
c1696fb8 754 u32 hash, crc;
ade48088 755 struct page *page;
c1696fb8
BP
756 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
757 struct timespec64 tv;
758 struct super_block *sb = sdp->sd_vfs;
7c70b896 759 u64 dblock;
588bff95 760
ade48088 761 if (gfs2_withdrawn(sdp))
4a3d049d 762 return;
ade48088
BP
763
764 page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
e8c92ed7
SW
765 lh = page_address(page);
766 clear_page(lh);
34cc1781 767
34cc1781
SW
768 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
769 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
770 lh->lh_header.__pad0 = cpu_to_be64(0);
771 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
772 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
588bff95 773 lh->lh_sequence = cpu_to_be64(seq);
34cc1781
SW
774 lh->lh_flags = cpu_to_be32(flags);
775 lh->lh_tail = cpu_to_be32(tail);
7c70b896 776 lh->lh_blkno = cpu_to_be32(lblock);
c1696fb8 777 hash = ~crc32(~0, lh, LH_V1_SIZE);
34cc1781
SW
778 lh->lh_hash = cpu_to_be32(hash);
779
ee9c7f9a 780 ktime_get_coarse_real_ts64(&tv);
c1696fb8
BP
781 lh->lh_nsec = cpu_to_be32(tv.tv_nsec);
782 lh->lh_sec = cpu_to_be64(tv.tv_sec);
7c70b896 783 if (!list_empty(&jd->extent_list))
19ebc050 784 dblock = gfs2_log_bmap(jd, lblock);
7c70b896
BP
785 else {
786 int ret = gfs2_lblk_to_dblk(jd->jd_inode, lblock, &dblock);
787 if (gfs2_assert_withdraw(sdp, ret == 0))
788 return;
789 }
790 lh->lh_addr = cpu_to_be64(dblock);
c1696fb8
BP
791 lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr);
792
793 /* We may only write local statfs, quota, etc., when writing to our
794 own journal. The values are left 0 when recovering a journal
795 different from our own. */
796 if (!(flags & GFS2_LOG_HEAD_RECOVERY)) {
797 lh->lh_statfs_addr =
798 cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr);
799 lh->lh_quota_addr =
800 cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr);
801
802 spin_lock(&sdp->sd_statfs_spin);
803 lh->lh_local_total = cpu_to_be64(l_sc->sc_total);
804 lh->lh_local_free = cpu_to_be64(l_sc->sc_free);
805 lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes);
806 spin_unlock(&sdp->sd_statfs_spin);
807 }
808
809 BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE);
810
811 crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
812 sb->s_blocksize - LH_V1_SIZE - 4);
813 lh->lh_crc = cpu_to_be32(crc);
814
7c70b896 815 gfs2_log_write(sdp, page, sb->s_blocksize, 0, dblock);
f4686c26 816 gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE | op_flags);
588bff95
BP
817}
818
819/**
820 * log_write_header - Get and initialize a journal header buffer
821 * @sdp: The GFS2 superblock
c1696fb8 822 * @flags: The log header flags, including log header origin
588bff95
BP
823 *
824 * Returns: the initialized log buffer descriptor
825 */
826
827static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
828{
588bff95
BP
829 int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
830 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
831
832 gfs2_assert_withdraw(sdp, (state != SFS_FROZEN));
588bff95 833
34cc1781
SW
834 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
835 gfs2_ordered_wait(sdp);
836 log_flush_wait(sdp);
70fd7614 837 op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
34cc1781 838 }
5cb738b5
AG
839 sdp->sd_log_idle = (sdp->sd_log_flush_tail == sdp->sd_log_flush_head);
840 gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++,
841 sdp->sd_log_flush_tail, sdp->sd_log_flush_head,
842 flags, op_flags);
19ebc050 843 gfs2_log_incr_head(sdp);
4a3d049d 844 log_flush_wait(sdp);
5cb738b5 845 log_pull_tail(sdp);
34cc1781
SW
846}
847
2ca0c2fb
BP
848/**
849 * ail_drain - drain the ail lists after a withdraw
850 * @sdp: Pointer to GFS2 superblock
851 */
852static void ail_drain(struct gfs2_sbd *sdp)
853{
854 struct gfs2_trans *tr;
855
856 spin_lock(&sdp->sd_ail_lock);
857 /*
858 * For transactions on the sd_ail1_list we need to drain both the
859 * ail1 and ail2 lists. That's because function gfs2_ail1_start_one
860 * (temporarily) moves items from its tr_ail1 list to tr_ail2 list
861 * before revokes are sent for that block. Items on the sd_ail2_list
862 * should have already gotten beyond that point, so no need.
863 */
864 while (!list_empty(&sdp->sd_ail1_list)) {
865 tr = list_first_entry(&sdp->sd_ail1_list, struct gfs2_trans,
866 tr_list);
867 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail1_list);
868 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
869 list_del(&tr->tr_list);
b839dada 870 gfs2_trans_free(sdp, tr);
2ca0c2fb
BP
871 }
872 while (!list_empty(&sdp->sd_ail2_list)) {
873 tr = list_first_entry(&sdp->sd_ail2_list, struct gfs2_trans,
874 tr_list);
875 gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
876 list_del(&tr->tr_list);
b839dada 877 gfs2_trans_free(sdp, tr);
2ca0c2fb
BP
878 }
879 spin_unlock(&sdp->sd_ail_lock);
880}
881
d5dc3d96
BP
882/**
883 * empty_ail1_list - try to start IO and empty the ail1 list
884 * @sdp: Pointer to GFS2 superblock
885 */
886static void empty_ail1_list(struct gfs2_sbd *sdp)
887{
888 unsigned long start = jiffies;
889
890 for (;;) {
891 if (time_after(jiffies, start + (HZ * 600))) {
892 fs_err(sdp, "Error: In %s for 10 minutes! t=%d\n",
893 __func__, current->journal_info ? 1 : 0);
894 dump_ail_list(sdp);
895 return;
896 }
897 gfs2_ail1_start(sdp);
898 gfs2_ail1_wait(sdp);
899 if (gfs2_ail1_empty(sdp, 0))
900 return;
901 }
902}
903
462582b9 904/**
521031fa 905 * trans_drain - drain the buf and databuf queue for a failed transaction
462582b9
BP
906 * @tr: the transaction to drain
907 *
908 * When this is called, we're taking an error exit for a log write that failed
909 * but since we bypassed the after_commit functions, we need to remove the
910 * items from the buf and databuf queue.
911 */
912static void trans_drain(struct gfs2_trans *tr)
913{
914 struct gfs2_bufdata *bd;
915 struct list_head *head;
916
917 if (!tr)
918 return;
919
920 head = &tr->tr_buf;
921 while (!list_empty(head)) {
922 bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
923 list_del_init(&bd->bd_list);
924 kmem_cache_free(gfs2_bufdata_cachep, bd);
925 }
926 head = &tr->tr_databuf;
927 while (!list_empty(head)) {
928 bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
929 list_del_init(&bd->bd_list);
930 kmem_cache_free(gfs2_bufdata_cachep, bd);
931 }
932}
933
b3b94faa 934/**
b09e593d 935 * gfs2_log_flush - flush incore transaction(s)
b3b94faa
DT
936 * @sdp: the filesystem
937 * @gl: The glock structure to flush. If NULL, flush the whole incore log
805c0907 938 * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
b3b94faa
DT
939 *
940 */
941
c1696fb8 942void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
b3b94faa 943{
2ca0c2fb 944 struct gfs2_trans *tr = NULL;
2e60d768 945 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
b3b94faa 946
484adff8 947 down_write(&sdp->sd_log_flush_lock);
f55ab26a 948
2ca0c2fb
BP
949 /*
950 * Do this check while holding the log_flush_lock to prevent new
951 * buffers from being added to the ail via gfs2_pin()
952 */
953 if (gfs2_withdrawn(sdp))
954 goto out;
955
2bcd610d 956 /* Log might have been flushed while we waited for the flush lock */
5a61ae14
AG
957 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags))
958 goto out;
805c0907 959 trace_gfs2_log_flush(sdp, 1, flags);
f55ab26a 960
c1696fb8 961 if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN)
400ac52e
BM
962 clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
963
b1ab1e44 964 sdp->sd_log_flush_head = sdp->sd_log_head;
16ca9412
BM
965 tr = sdp->sd_log_tr;
966 if (tr) {
967 sdp->sd_log_tr = NULL;
b1ab1e44 968 tr->tr_first = sdp->sd_log_flush_head;
2e60d768 969 if (unlikely (state == SFS_FROZEN))
ca399c96
BP
970 if (gfs2_assert_withdraw_delayed(sdp,
971 !tr->tr_num_buf_new && !tr->tr_num_databuf_new))
5a61ae14 972 goto out_withdraw;
16ca9412 973 }
b3b94faa 974
2e60d768 975 if (unlikely(state == SFS_FROZEN))
ca399c96 976 if (gfs2_assert_withdraw_delayed(sdp, !sdp->sd_log_num_revoke))
5a61ae14 977 goto out_withdraw;
ca399c96
BP
978 if (gfs2_assert_withdraw_delayed(sdp,
979 sdp->sd_log_num_revoke == sdp->sd_log_committed_revoke))
5a61ae14 980 goto out_withdraw;
b3b94faa 981
d7b616e2 982 gfs2_ordered_write(sdp);
2ca0c2fb 983 if (gfs2_withdrawn(sdp))
5a61ae14 984 goto out_withdraw;
d69a3c65 985 lops_before_commit(sdp, tr);
2ca0c2fb 986 if (gfs2_withdrawn(sdp))
5a61ae14 987 goto out_withdraw;
f4686c26 988 gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE);
2ca0c2fb 989 if (gfs2_withdrawn(sdp))
5a61ae14 990 goto out_withdraw;
d7b616e2 991
34cc1781 992 if (sdp->sd_log_head != sdp->sd_log_flush_head) {
428fd95d 993 log_flush_wait(sdp);
c1696fb8 994 log_write_header(sdp, flags);
5cb738b5 995 } else if (sdp->sd_log_tail != sdp->sd_log_flush_tail && !sdp->sd_log_idle) {
fd041f0b 996 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
63997775 997 trace_gfs2_log_blocks(sdp, -1);
c1696fb8 998 log_write_header(sdp, flags);
2332c443 999 }
2ca0c2fb 1000 if (gfs2_withdrawn(sdp))
5a61ae14 1001 goto out_withdraw;
16ca9412 1002 lops_after_commit(sdp, tr);
b09e593d 1003
fe1a698f 1004 gfs2_log_lock(sdp);
5cb738b5 1005 gfs2_log_update_head(sdp);
faa31ce8 1006 sdp->sd_log_blks_reserved = 0;
5d439758 1007 sdp->sd_log_committed_revoke = 0;
b3b94faa 1008
d6a079e8 1009 spin_lock(&sdp->sd_ail_lock);
16ca9412
BM
1010 if (tr && !list_empty(&tr->tr_ail1_list)) {
1011 list_add(&tr->tr_list, &sdp->sd_ail1_list);
1012 tr = NULL;
b3b94faa 1013 }
d6a079e8 1014 spin_unlock(&sdp->sd_ail_lock);
b3b94faa 1015 gfs2_log_unlock(sdp);
24972557 1016
c1696fb8 1017 if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
24972557 1018 if (!sdp->sd_log_idle) {
d5dc3d96 1019 empty_ail1_list(sdp);
30fe70a8 1020 if (gfs2_withdrawn(sdp))
5a61ae14 1021 goto out_withdraw;
24972557
BM
1022 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
1023 trace_gfs2_log_blocks(sdp, -1);
c1696fb8 1024 log_write_header(sdp, flags);
5cb738b5 1025 gfs2_log_update_head(sdp);
24972557 1026 }
c1696fb8
BP
1027 if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
1028 GFS2_LOG_HEAD_FLUSH_FREEZE))
24972557 1029 gfs2_log_shutdown(sdp);
c1696fb8 1030 if (flags & GFS2_LOG_HEAD_FLUSH_FREEZE)
2e60d768 1031 atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
24972557
BM
1032 }
1033
5a61ae14 1034out_end:
805c0907 1035 trace_gfs2_log_flush(sdp, 0, flags);
5a61ae14 1036out:
484adff8 1037 up_write(&sdp->sd_log_flush_lock);
b839dada 1038 gfs2_trans_free(sdp, tr);
5a61ae14
AG
1039 if (gfs2_withdrawing(sdp))
1040 gfs2_withdraw(sdp);
1041 return;
1042
1043out_withdraw:
1044 trans_drain(tr);
1045 /**
1046 * If the tr_list is empty, we're withdrawing during a log
1047 * flush that targets a transaction, but the transaction was
1048 * never queued onto any of the ail lists. Here we add it to
1049 * ail1 just so that ail_drain() will find and free it.
1050 */
1051 spin_lock(&sdp->sd_ail_lock);
1052 if (tr && list_empty(&tr->tr_list))
1053 list_add(&tr->tr_list, &sdp->sd_ail1_list);
1054 spin_unlock(&sdp->sd_ail_lock);
1055 ail_drain(sdp); /* frees all transactions */
1056 tr = NULL;
1057 goto out_end;
b3b94faa
DT
1058}
1059
d69a3c65
SW
1060/**
1061 * gfs2_merge_trans - Merge a new transaction into a cached transaction
1062 * @old: Original transaction to be expanded
1063 * @new: New transaction to be merged
1064 */
1065
83d060ca 1066static void gfs2_merge_trans(struct gfs2_sbd *sdp, struct gfs2_trans *new)
d69a3c65 1067{
83d060ca
BP
1068 struct gfs2_trans *old = sdp->sd_log_tr;
1069
9862ca05 1070 WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
d69a3c65
SW
1071
1072 old->tr_num_buf_new += new->tr_num_buf_new;
1073 old->tr_num_databuf_new += new->tr_num_databuf_new;
1074 old->tr_num_buf_rm += new->tr_num_buf_rm;
1075 old->tr_num_databuf_rm += new->tr_num_databuf_rm;
1076 old->tr_num_revoke += new->tr_num_revoke;
a31b4ec5 1077 old->tr_num_revoke_rm += new->tr_num_revoke_rm;
d69a3c65
SW
1078
1079 list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
1080 list_splice_tail_init(&new->tr_buf, &old->tr_buf);
83d060ca
BP
1081
1082 spin_lock(&sdp->sd_ail_lock);
1083 list_splice_tail_init(&new->tr_ail1_list, &old->tr_ail1_list);
1084 list_splice_tail_init(&new->tr_ail2_list, &old->tr_ail2_list);
1085 spin_unlock(&sdp->sd_ail_lock);
d69a3c65
SW
1086}
1087
b3b94faa
DT
1088static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1089{
2332c443 1090 unsigned int reserved;
ac39aadd 1091 unsigned int unused;
022ef4fe 1092 unsigned int maxres;
b3b94faa
DT
1093
1094 gfs2_log_lock(sdp);
1095
022ef4fe 1096 if (sdp->sd_log_tr) {
83d060ca 1097 gfs2_merge_trans(sdp, tr);
022ef4fe 1098 } else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
c968f578 1099 gfs2_assert_withdraw(sdp, !test_bit(TR_ONSTACK, &tr->tr_flags));
022ef4fe 1100 sdp->sd_log_tr = tr;
9862ca05 1101 set_bit(TR_ATTACHED, &tr->tr_flags);
022ef4fe
SW
1102 }
1103
a31b4ec5 1104 sdp->sd_log_committed_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
2332c443 1105 reserved = calc_reserved(sdp);
022ef4fe
SW
1106 maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
1107 gfs2_assert_withdraw(sdp, maxres >= reserved);
1108 unused = maxres - reserved;
5ae8fff8
AG
1109 if (unused)
1110 gfs2_log_release(sdp, unused);
b3b94faa
DT
1111 sdp->sd_log_blks_reserved = reserved;
1112
1113 gfs2_log_unlock(sdp);
1114}
1115
1116/**
1117 * gfs2_log_commit - Commit a transaction to the log
1118 * @sdp: the filesystem
1119 * @tr: the transaction
1120 *
5e687eac
BM
1121 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
1122 * or the total number of used blocks (pinned blocks plus AIL blocks)
1123 * is greater than thresh2.
1124 *
b57bc0fb 1125 * At mount time thresh1 is 2/5ths of journal size, thresh2 is 4/5ths of
5e687eac
BM
1126 * journal size.
1127 *
b3b94faa
DT
1128 * Returns: errno
1129 */
1130
1131void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1132{
1133 log_refund(sdp, tr);
b3b94faa 1134
5e687eac
BM
1135 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
1136 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
1137 atomic_read(&sdp->sd_log_thresh2)))
1138 wake_up(&sdp->sd_logd_waitq);
b3b94faa
DT
1139}
1140
1141/**
1142 * gfs2_log_shutdown - write a shutdown header into a journal
1143 * @sdp: the filesystem
1144 *
1145 */
1146
feed98a8 1147static void gfs2_log_shutdown(struct gfs2_sbd *sdp)
b3b94faa 1148{
b3b94faa 1149 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
b3b94faa 1150 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
b3b94faa
DT
1151 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
1152
1153 sdp->sd_log_flush_head = sdp->sd_log_head;
b3b94faa 1154
805c0907 1155 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN);
b3b94faa 1156
a74604be
SW
1157 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
1158 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
b3b94faa 1159
5cb738b5 1160 gfs2_log_update_head(sdp);
b3b94faa 1161 sdp->sd_log_tail = sdp->sd_log_head;
a25311c8
SW
1162}
1163
5e687eac
BM
1164static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
1165{
f07b3520
BP
1166 return (atomic_read(&sdp->sd_log_pinned) +
1167 atomic_read(&sdp->sd_log_blks_needed) >=
1168 atomic_read(&sdp->sd_log_thresh1));
5e687eac
BM
1169}
1170
1171static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
1172{
1173 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
b066a4ee
AD
1174
1175 if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags))
1176 return 1;
1177
f07b3520
BP
1178 return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >=
1179 atomic_read(&sdp->sd_log_thresh2);
5e687eac 1180}
ec69b188
SW
1181
1182/**
1183 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
1184 * @sdp: Pointer to GFS2 superblock
1185 *
1186 * Also, periodically check to make sure that we're using the most recent
1187 * journal index.
1188 */
1189
1190int gfs2_logd(void *data)
1191{
1192 struct gfs2_sbd *sdp = data;
5e687eac
BM
1193 unsigned long t = 1;
1194 DEFINE_WAIT(wait);
ec69b188
SW
1195
1196 while (!kthread_should_stop()) {
ec69b188 1197
d22f69a0
BP
1198 if (gfs2_withdrawn(sdp)) {
1199 msleep_interruptible(HZ);
1200 continue;
1201 }
942b0cdd
BP
1202 /* Check for errors writing to the journal */
1203 if (sdp->sd_log_error) {
badb55ec
AG
1204 gfs2_lm(sdp,
1205 "GFS2: fsid=%s: error %d: "
1206 "withdrawing the file system to "
1207 "prevent further damage.\n",
1208 sdp->sd_fsname, sdp->sd_log_error);
1209 gfs2_withdraw(sdp);
d22f69a0 1210 continue;
942b0cdd
BP
1211 }
1212
5e687eac 1213 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
5e4c7632 1214 gfs2_ail1_empty(sdp, 0);
805c0907
BP
1215 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1216 GFS2_LFC_LOGD_JFLUSH_REQD);
5e687eac 1217 }
ec69b188 1218
5e687eac
BM
1219 if (gfs2_ail_flush_reqd(sdp)) {
1220 gfs2_ail1_start(sdp);
26b06a69 1221 gfs2_ail1_wait(sdp);
5e4c7632 1222 gfs2_ail1_empty(sdp, 0);
805c0907
BP
1223 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1224 GFS2_LFC_LOGD_AIL_FLUSH_REQD);
ec69b188
SW
1225 }
1226
ec69b188 1227 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
a0acae0e
TH
1228
1229 try_to_freeze();
5e687eac
BM
1230
1231 do {
1232 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
5f487490 1233 TASK_INTERRUPTIBLE);
5e687eac
BM
1234 if (!gfs2_ail_flush_reqd(sdp) &&
1235 !gfs2_jrnl_flush_reqd(sdp) &&
1236 !kthread_should_stop())
1237 t = schedule_timeout(t);
1238 } while(t && !gfs2_ail_flush_reqd(sdp) &&
1239 !gfs2_jrnl_flush_reqd(sdp) &&
1240 !kthread_should_stop());
1241 finish_wait(&sdp->sd_logd_waitq, &wait);
ec69b188
SW
1242 }
1243
1244 return 0;
1245}
1246
This page took 1.011074 seconds and 4 git commands to generate.