]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/gfs2/log.c
GFS2: Move log buffer lists into transaction
[mirror_ubuntu-bionic-kernel.git] / fs / gfs2 / log.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
da6dd40d 3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
5c676f6d 15#include <linux/gfs2_ondisk.h>
71b86f56 16#include <linux/crc32.h>
a25311c8 17#include <linux/delay.h>
ec69b188
SW
18#include <linux/kthread.h>
19#include <linux/freezer.h>
254db57f 20#include <linux/bio.h>
885bceca 21#include <linux/blkdev.h>
4667a0ec 22#include <linux/writeback.h>
4a36d08d 23#include <linux/list_sort.h>
b3b94faa
DT
24
25#include "gfs2.h"
5c676f6d 26#include "incore.h"
b3b94faa
DT
27#include "bmap.h"
28#include "glock.h"
29#include "log.h"
30#include "lops.h"
31#include "meta_io.h"
5c676f6d 32#include "util.h"
71b86f56 33#include "dir.h"
63997775 34#include "trace_gfs2.h"
b3b94faa 35
b3b94faa
DT
36/**
37 * gfs2_struct2blk - compute stuff
38 * @sdp: the filesystem
39 * @nstruct: the number of structures
40 * @ssize: the size of the structures
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
48unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
49 unsigned int ssize)
50{
51 unsigned int blks;
52 unsigned int first, second;
53
54 blks = 1;
faa31ce8 55 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
b3b94faa
DT
56
57 if (nstruct > first) {
568f4c96
SW
58 second = (sdp->sd_sb.sb_bsize -
59 sizeof(struct gfs2_meta_header)) / ssize;
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
f91a0d3e 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,
16ca9412 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;
ddacfaf7 102
16ca9412 103 list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
4667a0ec 104 bh = bd->bd_bh;
ddacfaf7 105
16ca9412 106 gfs2_assert(sdp, bd->bd_tr == tr);
ddacfaf7 107
4667a0ec
SW
108 if (!buffer_busy(bh)) {
109 if (!buffer_uptodate(bh))
110 gfs2_io_error_bh(sdp, bh);
16ca9412 111 list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
4667a0ec
SW
112 continue;
113 }
114
115 if (!buffer_dirty(bh))
116 continue;
117 if (gl == bd->bd_gl)
118 continue;
119 gl = bd->bd_gl;
16ca9412 120 list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
4667a0ec 121 mapping = bh->b_page->mapping;
4f1de018
SW
122 if (!mapping)
123 continue;
4667a0ec
SW
124 spin_unlock(&sdp->sd_ail_lock);
125 generic_writepages(mapping, wbc);
126 spin_lock(&sdp->sd_ail_lock);
127 if (wbc->nr_to_write <= 0)
128 break;
4f1de018 129 return 1;
4667a0ec 130 }
4f1de018
SW
131
132 return 0;
4667a0ec 133}
ddacfaf7 134
ddacfaf7 135
4667a0ec
SW
136/**
137 * gfs2_ail1_flush - start writeback of some ail1 entries
138 * @sdp: The super block
139 * @wbc: The writeback control structure
140 *
141 * Writes back some ail1 entries, according to the limits in the
142 * writeback control structure
143 */
ddacfaf7 144
4667a0ec
SW
145void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
146{
147 struct list_head *head = &sdp->sd_ail1_list;
16ca9412 148 struct gfs2_trans *tr;
885bceca 149 struct blk_plug plug;
ddacfaf7 150
c83ae9ca 151 trace_gfs2_ail_flush(sdp, wbc, 1);
885bceca 152 blk_start_plug(&plug);
4667a0ec 153 spin_lock(&sdp->sd_ail_lock);
4f1de018 154restart:
16ca9412 155 list_for_each_entry_reverse(tr, head, tr_list) {
4667a0ec 156 if (wbc->nr_to_write <= 0)
ddacfaf7 157 break;
16ca9412 158 if (gfs2_ail1_start_one(sdp, wbc, tr))
4f1de018 159 goto restart;
4667a0ec
SW
160 }
161 spin_unlock(&sdp->sd_ail_lock);
885bceca 162 blk_finish_plug(&plug);
c83ae9ca 163 trace_gfs2_ail_flush(sdp, wbc, 0);
4667a0ec
SW
164}
165
166/**
167 * gfs2_ail1_start - start writeback of all ail1 entries
168 * @sdp: The superblock
169 */
170
171static void gfs2_ail1_start(struct gfs2_sbd *sdp)
172{
173 struct writeback_control wbc = {
174 .sync_mode = WB_SYNC_NONE,
175 .nr_to_write = LONG_MAX,
176 .range_start = 0,
177 .range_end = LLONG_MAX,
178 };
179
180 return gfs2_ail1_flush(sdp, &wbc);
ddacfaf7
SW
181}
182
183/**
184 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
185 * @sdp: the filesystem
186 * @ai: the AIL entry
187 *
188 */
189
16ca9412 190static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
ddacfaf7
SW
191{
192 struct gfs2_bufdata *bd, *s;
193 struct buffer_head *bh;
194
16ca9412 195 list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
ddacfaf7
SW
196 bd_ail_st_list) {
197 bh = bd->bd_bh;
16ca9412 198 gfs2_assert(sdp, bd->bd_tr == tr);
4667a0ec
SW
199 if (buffer_busy(bh))
200 continue;
ddacfaf7
SW
201 if (!buffer_uptodate(bh))
202 gfs2_io_error_bh(sdp, bh);
16ca9412 203 list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
ddacfaf7
SW
204 }
205
ddacfaf7
SW
206}
207
4667a0ec
SW
208/**
209 * gfs2_ail1_empty - Try to empty the ail1 lists
210 * @sdp: The superblock
211 *
212 * Tries to empty the ail1 lists, starting with the oldest first
213 */
b3b94faa 214
4667a0ec 215static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
b3b94faa 216{
16ca9412 217 struct gfs2_trans *tr, *s;
5d054964 218 int oldest_tr = 1;
b3b94faa
DT
219 int ret;
220
d6a079e8 221 spin_lock(&sdp->sd_ail_lock);
16ca9412
BM
222 list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
223 gfs2_ail1_empty_one(sdp, tr);
5d054964 224 if (list_empty(&tr->tr_ail1_list) && oldest_tr)
16ca9412 225 list_move(&tr->tr_list, &sdp->sd_ail2_list);
4667a0ec 226 else
5d054964 227 oldest_tr = 0;
b3b94faa 228 }
b3b94faa 229 ret = list_empty(&sdp->sd_ail1_list);
d6a079e8 230 spin_unlock(&sdp->sd_ail_lock);
b3b94faa
DT
231
232 return ret;
233}
234
26b06a69
SW
235static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
236{
16ca9412 237 struct gfs2_trans *tr;
26b06a69
SW
238 struct gfs2_bufdata *bd;
239 struct buffer_head *bh;
240
241 spin_lock(&sdp->sd_ail_lock);
16ca9412
BM
242 list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
243 list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
26b06a69
SW
244 bh = bd->bd_bh;
245 if (!buffer_locked(bh))
246 continue;
247 get_bh(bh);
248 spin_unlock(&sdp->sd_ail_lock);
249 wait_on_buffer(bh);
250 brelse(bh);
251 return;
252 }
253 }
254 spin_unlock(&sdp->sd_ail_lock);
255}
ddacfaf7
SW
256
257/**
258 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
259 * @sdp: the filesystem
260 * @ai: the AIL entry
261 *
262 */
263
16ca9412 264static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
ddacfaf7 265{
16ca9412 266 struct list_head *head = &tr->tr_ail2_list;
ddacfaf7
SW
267 struct gfs2_bufdata *bd;
268
269 while (!list_empty(head)) {
270 bd = list_entry(head->prev, struct gfs2_bufdata,
271 bd_ail_st_list);
16ca9412 272 gfs2_assert(sdp, bd->bd_tr == tr);
f91a0d3e 273 gfs2_remove_from_ail(bd);
ddacfaf7
SW
274 }
275}
276
b3b94faa
DT
277static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
278{
16ca9412 279 struct gfs2_trans *tr, *safe;
b3b94faa
DT
280 unsigned int old_tail = sdp->sd_log_tail;
281 int wrap = (new_tail < old_tail);
282 int a, b, rm;
283
d6a079e8 284 spin_lock(&sdp->sd_ail_lock);
b3b94faa 285
16ca9412
BM
286 list_for_each_entry_safe(tr, safe, &sdp->sd_ail2_list, tr_list) {
287 a = (old_tail <= tr->tr_first);
288 b = (tr->tr_first < new_tail);
b3b94faa
DT
289 rm = (wrap) ? (a || b) : (a && b);
290 if (!rm)
291 continue;
292
16ca9412
BM
293 gfs2_ail2_empty_one(sdp, tr);
294 list_del(&tr->tr_list);
295 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
296 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
297 kfree(tr);
b3b94faa
DT
298 }
299
d6a079e8 300 spin_unlock(&sdp->sd_ail_lock);
b3b94faa
DT
301}
302
303/**
304 * gfs2_log_reserve - Make a log reservation
305 * @sdp: The GFS2 superblock
306 * @blks: The number of blocks to reserve
307 *
89918647 308 * Note that we never give out the last few blocks of the journal. Thats
2332c443 309 * due to the fact that there is a small number of header blocks
b004157a
SW
310 * associated with each log flush. The exact number can't be known until
311 * flush time, so we ensure that we have just enough free blocks at all
312 * times to avoid running out during a log flush.
313 *
5e687eac
BM
314 * We no longer flush the log here, instead we wake up logd to do that
315 * for us. To avoid the thundering herd and to ensure that we deal fairly
316 * with queued waiters, we use an exclusive wait. This means that when we
317 * get woken with enough journal space to get our reservation, we need to
318 * wake the next waiter on the list.
319 *
b3b94faa
DT
320 * Returns: errno
321 */
322
323int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
324{
5d054964 325 unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize);
5e687eac
BM
326 unsigned wanted = blks + reserved_blks;
327 DEFINE_WAIT(wait);
328 int did_wait = 0;
329 unsigned int free_blocks;
b3b94faa
DT
330
331 if (gfs2_assert_warn(sdp, blks) ||
332 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
333 return -EINVAL;
5e687eac
BM
334retry:
335 free_blocks = atomic_read(&sdp->sd_log_blks_free);
336 if (unlikely(free_blocks <= wanted)) {
337 do {
338 prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
339 TASK_UNINTERRUPTIBLE);
340 wake_up(&sdp->sd_logd_waitq);
341 did_wait = 1;
342 if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
343 io_schedule();
344 free_blocks = atomic_read(&sdp->sd_log_blks_free);
345 } while(free_blocks <= wanted);
346 finish_wait(&sdp->sd_log_waitq, &wait);
b3b94faa 347 }
5e687eac
BM
348 if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
349 free_blocks - blks) != free_blocks)
350 goto retry;
63997775 351 trace_gfs2_log_blocks(sdp, -blks);
5e687eac
BM
352
353 /*
354 * If we waited, then so might others, wake them up _after_ we get
355 * our share of the log.
356 */
357 if (unlikely(did_wait))
358 wake_up(&sdp->sd_log_waitq);
484adff8
SW
359
360 down_read(&sdp->sd_log_flush_lock);
b3b94faa
DT
361
362 return 0;
363}
364
b3b94faa
DT
365/**
366 * log_distance - Compute distance between two journal blocks
367 * @sdp: The GFS2 superblock
368 * @newer: The most recent journal block of the pair
369 * @older: The older journal block of the pair
370 *
371 * Compute the distance (in the journal direction) between two
372 * blocks in the journal
373 *
374 * Returns: the distance in blocks
375 */
376
faa31ce8 377static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
b3b94faa
DT
378 unsigned int older)
379{
380 int dist;
381
382 dist = newer - older;
383 if (dist < 0)
384 dist += sdp->sd_jdesc->jd_blocks;
385
386 return dist;
387}
388
2332c443
RP
389/**
390 * calc_reserved - Calculate the number of blocks to reserve when
391 * refunding a transaction's unused buffers.
392 * @sdp: The GFS2 superblock
393 *
394 * This is complex. We need to reserve room for all our currently used
395 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
396 * all our journaled data buffers for journaled files (e.g. files in the
397 * meta_fs like rindex, or files for which chattr +j was done.)
398 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
399 * will count it as free space (sd_log_blks_free) and corruption will follow.
400 *
401 * We can have metadata bufs and jdata bufs in the same journal. So each
402 * type gets its own log header, for which we need to reserve a block.
403 * In fact, each type has the potential for needing more than one header
404 * in cases where we have more buffers than will fit on a journal page.
405 * Metadata journal entries take up half the space of journaled buffer entries.
406 * Thus, metadata entries have buf_limit (502) and journaled buffers have
407 * databuf_limit (251) before they cause a wrap around.
408 *
409 * Also, we need to reserve blocks for revoke journal entries and one for an
410 * overall header for the lot.
411 *
412 * Returns: the number of blocks reserved
413 */
414static unsigned int calc_reserved(struct gfs2_sbd *sdp)
415{
416 unsigned int reserved = 0;
417 unsigned int mbuf_limit, metabufhdrs_needed;
418 unsigned int dbuf_limit, databufhdrs_needed;
419 unsigned int revokes = 0;
420
421 mbuf_limit = buf_limit(sdp);
422 metabufhdrs_needed = (sdp->sd_log_commited_buf +
423 (mbuf_limit - 1)) / mbuf_limit;
424 dbuf_limit = databuf_limit(sdp);
425 databufhdrs_needed = (sdp->sd_log_commited_databuf +
426 (dbuf_limit - 1)) / dbuf_limit;
427
2e95e3f6 428 if (sdp->sd_log_commited_revoke > 0)
2332c443
RP
429 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
430 sizeof(u64));
431
432 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
433 sdp->sd_log_commited_databuf + databufhdrs_needed +
434 revokes;
435 /* One for the overall header */
436 if (reserved)
437 reserved++;
438 return reserved;
439}
440
b3b94faa
DT
441static unsigned int current_tail(struct gfs2_sbd *sdp)
442{
16ca9412 443 struct gfs2_trans *tr;
b3b94faa
DT
444 unsigned int tail;
445
d6a079e8 446 spin_lock(&sdp->sd_ail_lock);
b3b94faa 447
faa31ce8 448 if (list_empty(&sdp->sd_ail1_list)) {
b3b94faa 449 tail = sdp->sd_log_head;
faa31ce8 450 } else {
16ca9412
BM
451 tr = list_entry(sdp->sd_ail1_list.prev, struct gfs2_trans,
452 tr_list);
453 tail = tr->tr_first;
b3b94faa
DT
454 }
455
d6a079e8 456 spin_unlock(&sdp->sd_ail_lock);
b3b94faa
DT
457
458 return tail;
459}
460
2332c443 461static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
b3b94faa
DT
462{
463 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
464
465 ail2_empty(sdp, new_tail);
466
fd041f0b 467 atomic_add(dist, &sdp->sd_log_blks_free);
63997775 468 trace_gfs2_log_blocks(sdp, dist);
5e687eac
BM
469 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
470 sdp->sd_jdesc->jd_blocks);
b3b94faa
DT
471
472 sdp->sd_log_tail = new_tail;
473}
474
b3b94faa 475
34cc1781 476static void log_flush_wait(struct gfs2_sbd *sdp)
b3b94faa 477{
16615be1
SW
478 DEFINE_WAIT(wait);
479
480 if (atomic_read(&sdp->sd_log_in_flight)) {
481 do {
482 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
483 TASK_UNINTERRUPTIBLE);
484 if (atomic_read(&sdp->sd_log_in_flight))
485 io_schedule();
486 } while(atomic_read(&sdp->sd_log_in_flight));
487 finish_wait(&sdp->sd_log_flush_wait, &wait);
b3b94faa 488 }
b3b94faa
DT
489}
490
45138990 491static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
4a36d08d 492{
45138990 493 struct gfs2_inode *ipa, *ipb;
4a36d08d 494
45138990
SW
495 ipa = list_entry(a, struct gfs2_inode, i_ordered);
496 ipb = list_entry(b, struct gfs2_inode, i_ordered);
4a36d08d 497
45138990 498 if (ipa->i_no_addr < ipb->i_no_addr)
4a36d08d 499 return -1;
45138990 500 if (ipa->i_no_addr > ipb->i_no_addr)
4a36d08d
BP
501 return 1;
502 return 0;
503}
504
d7b616e2
SW
505static void gfs2_ordered_write(struct gfs2_sbd *sdp)
506{
45138990 507 struct gfs2_inode *ip;
d7b616e2
SW
508 LIST_HEAD(written);
509
45138990
SW
510 spin_lock(&sdp->sd_ordered_lock);
511 list_sort(NULL, &sdp->sd_log_le_ordered, &ip_cmp);
d7b616e2 512 while (!list_empty(&sdp->sd_log_le_ordered)) {
45138990
SW
513 ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
514 list_move(&ip->i_ordered, &written);
515 if (ip->i_inode.i_mapping->nrpages == 0)
d7b616e2 516 continue;
45138990
SW
517 spin_unlock(&sdp->sd_ordered_lock);
518 filemap_fdatawrite(ip->i_inode.i_mapping);
519 spin_lock(&sdp->sd_ordered_lock);
d7b616e2
SW
520 }
521 list_splice(&written, &sdp->sd_log_le_ordered);
45138990 522 spin_unlock(&sdp->sd_ordered_lock);
d7b616e2
SW
523}
524
525static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
526{
45138990 527 struct gfs2_inode *ip;
d7b616e2 528
45138990 529 spin_lock(&sdp->sd_ordered_lock);
d7b616e2 530 while (!list_empty(&sdp->sd_log_le_ordered)) {
45138990
SW
531 ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
532 list_del(&ip->i_ordered);
533 WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags));
534 if (ip->i_inode.i_mapping->nrpages == 0)
d7b616e2 535 continue;
45138990
SW
536 spin_unlock(&sdp->sd_ordered_lock);
537 filemap_fdatawait(ip->i_inode.i_mapping);
538 spin_lock(&sdp->sd_ordered_lock);
d7b616e2 539 }
45138990
SW
540 spin_unlock(&sdp->sd_ordered_lock);
541}
542
543void gfs2_ordered_del_inode(struct gfs2_inode *ip)
544{
545 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
546
547 spin_lock(&sdp->sd_ordered_lock);
548 if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags))
549 list_del(&ip->i_ordered);
550 spin_unlock(&sdp->sd_ordered_lock);
d7b616e2
SW
551}
552
5d054964
BM
553void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
554{
555 struct buffer_head *bh = bd->bd_bh;
556 struct gfs2_glock *gl = bd->bd_gl;
557
5d054964
BM
558 bh->b_private = NULL;
559 bd->bd_blkno = bh->b_blocknr;
9290a9a7
BP
560 gfs2_remove_from_ail(bd); /* drops ref on bh */
561 bd->bd_bh = NULL;
5d054964
BM
562 bd->bd_ops = &gfs2_revoke_lops;
563 sdp->sd_log_num_revoke++;
564 atomic_inc(&gl->gl_revokes);
565 set_bit(GLF_LFLUSH, &gl->gl_flags);
566 list_add(&bd->bd_list, &sdp->sd_log_le_revoke);
567}
568
569void gfs2_write_revokes(struct gfs2_sbd *sdp)
570{
571 struct gfs2_trans *tr;
572 struct gfs2_bufdata *bd, *tmp;
573 int have_revokes = 0;
574 int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
575
576 gfs2_ail1_empty(sdp);
577 spin_lock(&sdp->sd_ail_lock);
578 list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
579 list_for_each_entry(bd, &tr->tr_ail2_list, bd_ail_st_list) {
580 if (list_empty(&bd->bd_list)) {
581 have_revokes = 1;
582 goto done;
583 }
584 }
585 }
586done:
587 spin_unlock(&sdp->sd_ail_lock);
588 if (have_revokes == 0)
589 return;
590 while (sdp->sd_log_num_revoke > max_revokes)
591 max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
592 max_revokes -= sdp->sd_log_num_revoke;
593 if (!sdp->sd_log_num_revoke) {
594 atomic_dec(&sdp->sd_log_blks_free);
595 /* If no blocks have been reserved, we need to also
596 * reserve a block for the header */
597 if (!sdp->sd_log_blks_reserved)
598 atomic_dec(&sdp->sd_log_blks_free);
599 }
600 gfs2_log_lock(sdp);
601 spin_lock(&sdp->sd_ail_lock);
602 list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
603 list_for_each_entry_safe(bd, tmp, &tr->tr_ail2_list, bd_ail_st_list) {
604 if (max_revokes == 0)
605 goto out_of_blocks;
606 if (!list_empty(&bd->bd_list))
607 continue;
608 gfs2_add_revoke(sdp, bd);
609 max_revokes--;
610 }
611 }
612out_of_blocks:
613 spin_unlock(&sdp->sd_ail_lock);
614 gfs2_log_unlock(sdp);
615
616 if (!sdp->sd_log_num_revoke) {
617 atomic_inc(&sdp->sd_log_blks_free);
618 if (!sdp->sd_log_blks_reserved)
619 atomic_inc(&sdp->sd_log_blks_free);
620 }
621}
622
34cc1781
SW
623/**
624 * log_write_header - Get and initialize a journal header buffer
625 * @sdp: The GFS2 superblock
626 *
627 * Returns: the initialized log buffer descriptor
628 */
629
fdb76a42 630static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
34cc1781 631{
34cc1781
SW
632 struct gfs2_log_header *lh;
633 unsigned int tail;
634 u32 hash;
e8c92ed7
SW
635 int rw = WRITE_FLUSH_FUA | REQ_META;
636 struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
637 lh = page_address(page);
638 clear_page(lh);
34cc1781 639
34cc1781
SW
640 tail = current_tail(sdp);
641
34cc1781
SW
642 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
643 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
644 lh->lh_header.__pad0 = cpu_to_be64(0);
645 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
646 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
647 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
648 lh->lh_flags = cpu_to_be32(flags);
649 lh->lh_tail = cpu_to_be32(tail);
650 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
e8c92ed7 651 hash = gfs2_disk_hash(page_address(page), sizeof(struct gfs2_log_header));
34cc1781
SW
652 lh->lh_hash = cpu_to_be32(hash);
653
34cc1781
SW
654 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
655 gfs2_ordered_wait(sdp);
656 log_flush_wait(sdp);
e8c92ed7 657 rw = WRITE_SYNC | REQ_META | REQ_PRIO;
34cc1781 658 }
34cc1781 659
e8c92ed7
SW
660 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
661 gfs2_log_write_page(sdp, page);
662 gfs2_log_flush_bio(sdp, rw);
663 log_flush_wait(sdp);
34cc1781
SW
664
665 if (sdp->sd_log_tail != tail)
666 log_pull_tail(sdp, tail);
34cc1781
SW
667}
668
b3b94faa 669/**
b09e593d 670 * gfs2_log_flush - flush incore transaction(s)
b3b94faa
DT
671 * @sdp: the filesystem
672 * @gl: The glock structure to flush. If NULL, flush the whole incore log
673 *
674 */
675
ed4878e8 676void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
b3b94faa 677{
16ca9412 678 struct gfs2_trans *tr;
b3b94faa 679
484adff8 680 down_write(&sdp->sd_log_flush_lock);
f55ab26a 681
2bcd610d
SW
682 /* Log might have been flushed while we waited for the flush lock */
683 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
684 up_write(&sdp->sd_log_flush_lock);
685 return;
f55ab26a 686 }
63997775 687 trace_gfs2_log_flush(sdp, 1);
f55ab26a 688
16ca9412
BM
689 tr = sdp->sd_log_tr;
690 if (tr) {
691 sdp->sd_log_tr = NULL;
692 INIT_LIST_HEAD(&tr->tr_ail1_list);
693 INIT_LIST_HEAD(&tr->tr_ail2_list);
694 }
b3b94faa 695
16615be1
SW
696 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
697 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
698 sdp->sd_log_commited_buf);
699 gfs2_assert_withdraw(sdp, 0);
700 }
701 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
702 printk(KERN_INFO "GFS2: log databuf %u %u\n",
703 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
704 gfs2_assert_withdraw(sdp, 0);
705 }
b3b94faa
DT
706 gfs2_assert_withdraw(sdp,
707 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
708
b3b94faa
DT
709 sdp->sd_log_flush_head = sdp->sd_log_head;
710 sdp->sd_log_flush_wrapped = 0;
16ca9412
BM
711 if (tr)
712 tr->tr_first = sdp->sd_log_flush_head;
b3b94faa 713
d7b616e2 714 gfs2_ordered_write(sdp);
d69a3c65 715 lops_before_commit(sdp, tr);
e8c92ed7 716 gfs2_log_flush_bio(sdp, WRITE);
d7b616e2 717
34cc1781 718 if (sdp->sd_log_head != sdp->sd_log_flush_head) {
fdb76a42 719 log_write_header(sdp, 0);
34cc1781 720 } else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
fd041f0b 721 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
63997775 722 trace_gfs2_log_blocks(sdp, -1);
fdb76a42 723 log_write_header(sdp, 0);
2332c443 724 }
16ca9412 725 lops_after_commit(sdp, tr);
b09e593d 726
fe1a698f
SW
727 gfs2_log_lock(sdp);
728 sdp->sd_log_head = sdp->sd_log_flush_head;
faa31ce8
SW
729 sdp->sd_log_blks_reserved = 0;
730 sdp->sd_log_commited_buf = 0;
2332c443 731 sdp->sd_log_commited_databuf = 0;
faa31ce8 732 sdp->sd_log_commited_revoke = 0;
b3b94faa 733
d6a079e8 734 spin_lock(&sdp->sd_ail_lock);
16ca9412
BM
735 if (tr && !list_empty(&tr->tr_ail1_list)) {
736 list_add(&tr->tr_list, &sdp->sd_ail1_list);
737 tr = NULL;
b3b94faa 738 }
d6a079e8 739 spin_unlock(&sdp->sd_ail_lock);
b3b94faa 740 gfs2_log_unlock(sdp);
63997775 741 trace_gfs2_log_flush(sdp, 0);
484adff8 742 up_write(&sdp->sd_log_flush_lock);
b3b94faa 743
16ca9412 744 kfree(tr);
b3b94faa
DT
745}
746
d69a3c65
SW
747/**
748 * gfs2_merge_trans - Merge a new transaction into a cached transaction
749 * @old: Original transaction to be expanded
750 * @new: New transaction to be merged
751 */
752
753static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new)
754{
755 WARN_ON_ONCE(old->tr_attached != 1);
756
757 old->tr_num_buf_new += new->tr_num_buf_new;
758 old->tr_num_databuf_new += new->tr_num_databuf_new;
759 old->tr_num_buf_rm += new->tr_num_buf_rm;
760 old->tr_num_databuf_rm += new->tr_num_databuf_rm;
761 old->tr_num_revoke += new->tr_num_revoke;
762 old->tr_num_revoke_rm += new->tr_num_revoke_rm;
763
764 list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
765 list_splice_tail_init(&new->tr_buf, &old->tr_buf);
766}
767
b3b94faa
DT
768static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
769{
2332c443 770 unsigned int reserved;
ac39aadd 771 unsigned int unused;
b3b94faa
DT
772
773 gfs2_log_lock(sdp);
774
775 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
2332c443
RP
776 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
777 tr->tr_num_databuf_rm;
778 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
779 (((int)sdp->sd_log_commited_databuf) >= 0));
b3b94faa 780 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
2332c443 781 reserved = calc_reserved(sdp);
62be1f71 782 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
ac39aadd 783 unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
ac39aadd 784 atomic_add(unused, &sdp->sd_log_blks_free);
63997775 785 trace_gfs2_log_blocks(sdp, unused);
fd041f0b 786 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
2332c443 787 sdp->sd_jdesc->jd_blocks);
b3b94faa
DT
788 sdp->sd_log_blks_reserved = reserved;
789
d69a3c65
SW
790 if (sdp->sd_log_tr) {
791 gfs2_merge_trans(sdp->sd_log_tr, tr);
792 } else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
16ca9412
BM
793 gfs2_assert_withdraw(sdp, tr->tr_t_gh.gh_gl);
794 sdp->sd_log_tr = tr;
795 tr->tr_attached = 1;
796 }
b3b94faa
DT
797 gfs2_log_unlock(sdp);
798}
799
800/**
801 * gfs2_log_commit - Commit a transaction to the log
802 * @sdp: the filesystem
803 * @tr: the transaction
804 *
5e687eac
BM
805 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
806 * or the total number of used blocks (pinned blocks plus AIL blocks)
807 * is greater than thresh2.
808 *
809 * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
810 * journal size.
811 *
b3b94faa
DT
812 * Returns: errno
813 */
814
815void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
816{
817 log_refund(sdp, tr);
b3b94faa 818
5e687eac
BM
819 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
820 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
821 atomic_read(&sdp->sd_log_thresh2)))
822 wake_up(&sdp->sd_logd_waitq);
b3b94faa
DT
823}
824
825/**
826 * gfs2_log_shutdown - write a shutdown header into a journal
827 * @sdp: the filesystem
828 *
829 */
830
831void gfs2_log_shutdown(struct gfs2_sbd *sdp)
832{
484adff8 833 down_write(&sdp->sd_log_flush_lock);
b3b94faa 834
b3b94faa 835 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
b3b94faa
DT
836 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
837 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
838 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
839 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
840 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
841
842 sdp->sd_log_flush_head = sdp->sd_log_head;
843 sdp->sd_log_flush_wrapped = 0;
844
fdb76a42 845 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT);
b3b94faa 846
fd041f0b 847 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
a74604be
SW
848 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
849 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
b3b94faa
DT
850
851 sdp->sd_log_head = sdp->sd_log_flush_head;
b3b94faa
DT
852 sdp->sd_log_tail = sdp->sd_log_head;
853
484adff8 854 up_write(&sdp->sd_log_flush_lock);
b3b94faa
DT
855}
856
a25311c8
SW
857
858/**
859 * gfs2_meta_syncfs - sync all the buffers in a filesystem
860 * @sdp: the filesystem
861 *
862 */
863
864void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
865{
866 gfs2_log_flush(sdp, NULL);
867 for (;;) {
5e687eac 868 gfs2_ail1_start(sdp);
26b06a69 869 gfs2_ail1_wait(sdp);
4667a0ec 870 if (gfs2_ail1_empty(sdp))
a25311c8 871 break;
a25311c8 872 }
380f7c65 873 gfs2_log_flush(sdp, NULL);
a25311c8
SW
874}
875
5e687eac
BM
876static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
877{
878 return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
879}
880
881static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
882{
883 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
884 return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
885}
ec69b188
SW
886
887/**
888 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
889 * @sdp: Pointer to GFS2 superblock
890 *
891 * Also, periodically check to make sure that we're using the most recent
892 * journal index.
893 */
894
895int gfs2_logd(void *data)
896{
897 struct gfs2_sbd *sdp = data;
5e687eac
BM
898 unsigned long t = 1;
899 DEFINE_WAIT(wait);
ec69b188
SW
900
901 while (!kthread_should_stop()) {
ec69b188 902
5e687eac 903 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
4667a0ec 904 gfs2_ail1_empty(sdp);
5e687eac 905 gfs2_log_flush(sdp, NULL);
5e687eac 906 }
ec69b188 907
5e687eac
BM
908 if (gfs2_ail_flush_reqd(sdp)) {
909 gfs2_ail1_start(sdp);
26b06a69 910 gfs2_ail1_wait(sdp);
4667a0ec 911 gfs2_ail1_empty(sdp);
ec69b188 912 gfs2_log_flush(sdp, NULL);
ec69b188
SW
913 }
914
26b06a69
SW
915 if (!gfs2_ail_flush_reqd(sdp))
916 wake_up(&sdp->sd_log_waitq);
917
ec69b188 918 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
a0acae0e
TH
919
920 try_to_freeze();
5e687eac
BM
921
922 do {
923 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
5f487490 924 TASK_INTERRUPTIBLE);
5e687eac
BM
925 if (!gfs2_ail_flush_reqd(sdp) &&
926 !gfs2_jrnl_flush_reqd(sdp) &&
927 !kthread_should_stop())
928 t = schedule_timeout(t);
929 } while(t && !gfs2_ail_flush_reqd(sdp) &&
930 !gfs2_jrnl_flush_reqd(sdp) &&
931 !kthread_should_stop());
932 finish_wait(&sdp->sd_logd_waitq, &wait);
ec69b188
SW
933 }
934
935 return 0;
936}
937