blob: b8fe7b739c278642e68e0cd9e66c75a3ecffe5a2 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersonda6dd402007-12-11 18:49:21 -06003 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050016#include <linux/crc32.h>
Steven Whitehousea25311c2006-11-23 11:06:35 -050017#include <linux/delay.h>
Steven Whitehouseec69b182007-11-09 10:01:41 +000018#include <linux/kthread.h>
19#include <linux/freezer.h>
Steven Whitehouse254db572008-09-26 10:23:22 +010020#include <linux/bio.h>
Steven Whitehouse4667a0e2011-04-18 14:18:09 +010021#include <linux/writeback.h>
Bob Peterson4a36d082012-02-14 14:49:57 -050022#include <linux/list_sort.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000023
24#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include "bmap.h"
27#include "glock.h"
28#include "log.h"
29#include "lops.h"
30#include "meta_io.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050031#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050032#include "dir.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010033#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000034
35#define PULL 1
36
David Teiglandb3b94fa2006-01-16 16:50:04 +000037/**
38 * gfs2_struct2blk - compute stuff
39 * @sdp: the filesystem
40 * @nstruct: the number of structures
41 * @ssize: the size of the structures
42 *
43 * Compute the number of log descriptor blocks needed to hold a certain number
44 * of structures of a certain size.
45 *
46 * Returns: the number of blocks needed (minimum is always 1)
47 */
48
49unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
50 unsigned int ssize)
51{
52 unsigned int blks;
53 unsigned int first, second;
54
55 blks = 1;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -040056 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
David Teiglandb3b94fa2006-01-16 16:50:04 +000057
58 if (nstruct > first) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -050059 second = (sdp->sd_sb.sb_bsize -
60 sizeof(struct gfs2_meta_header)) / ssize;
Steven Whitehouse5c676f62006-02-27 17:23:27 -050061 blks += DIV_ROUND_UP(nstruct - first, second);
David Teiglandb3b94fa2006-01-16 16:50:04 +000062 }
63
64 return blks;
65}
66
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040067/**
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010068 * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
69 * @mapping: The associated mapping (maybe NULL)
70 * @bd: The gfs2_bufdata to remove
71 *
Steven Whitehousec618e872011-03-14 12:40:29 +000072 * The ail lock _must_ be held when calling this function
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010073 *
74 */
75
Steven Whitehousef91a0d32007-10-15 16:29:05 +010076void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010077{
78 bd->bd_ail = NULL;
Steven Whitehouse1ad38c42007-09-03 11:01:33 +010079 list_del_init(&bd->bd_ail_st_list);
80 list_del_init(&bd->bd_ail_gl_list);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010081 atomic_dec(&bd->bd_gl->gl_ail_count);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010082 brelse(bd->bd_bh);
83}
84
85/**
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040086 * gfs2_ail1_start_one - Start I/O on a part of the AIL
87 * @sdp: the filesystem
Steven Whitehouse4667a0e2011-04-18 14:18:09 +010088 * @wbc: The writeback control structure
89 * @ai: The ail structure
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040090 *
91 */
92
Steven Whitehouse4f1de012011-04-26 10:23:56 +010093static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
94 struct writeback_control *wbc,
95 struct gfs2_ail *ai)
Dave Chinnerd6a079e2011-03-11 11:52:25 +000096__releases(&sdp->sd_ail_lock)
97__acquires(&sdp->sd_ail_lock)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040098{
Steven Whitehouse5ac048b2011-03-30 16:25:51 +010099 struct gfs2_glock *gl = NULL;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100100 struct address_space *mapping;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400101 struct gfs2_bufdata *bd, *s;
102 struct buffer_head *bh;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400103
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100104 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, bd_ail_st_list) {
105 bh = bd->bd_bh;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400106
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100107 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400108
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100109 if (!buffer_busy(bh)) {
110 if (!buffer_uptodate(bh))
111 gfs2_io_error_bh(sdp, bh);
112 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
113 continue;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400114 }
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100115
116 if (!buffer_dirty(bh))
117 continue;
118 if (gl == bd->bd_gl)
119 continue;
120 gl = bd->bd_gl;
121 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
122 mapping = bh->b_page->mapping;
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100123 if (!mapping)
124 continue;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100125 spin_unlock(&sdp->sd_ail_lock);
126 generic_writepages(mapping, wbc);
127 spin_lock(&sdp->sd_ail_lock);
128 if (wbc->nr_to_write <= 0)
129 break;
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100130 return 1;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100131 }
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100132
133 return 0;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100134}
135
136
137/**
138 * gfs2_ail1_flush - start writeback of some ail1 entries
139 * @sdp: The super block
140 * @wbc: The writeback control structure
141 *
142 * Writes back some ail1 entries, according to the limits in the
143 * writeback control structure
144 */
145
146void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
147{
148 struct list_head *head = &sdp->sd_ail1_list;
149 struct gfs2_ail *ai;
150
Steven Whitehousec83ae9c2011-04-18 14:18:38 +0100151 trace_gfs2_ail_flush(sdp, wbc, 1);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100152 spin_lock(&sdp->sd_ail_lock);
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100153restart:
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100154 list_for_each_entry_reverse(ai, head, ai_list) {
155 if (wbc->nr_to_write <= 0)
156 break;
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100157 if (gfs2_ail1_start_one(sdp, wbc, ai))
158 goto restart;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100159 }
160 spin_unlock(&sdp->sd_ail_lock);
Steven Whitehousec83ae9c2011-04-18 14:18:38 +0100161 trace_gfs2_ail_flush(sdp, wbc, 0);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100162}
163
164/**
165 * gfs2_ail1_start - start writeback of all ail1 entries
166 * @sdp: The superblock
167 */
168
169static void gfs2_ail1_start(struct gfs2_sbd *sdp)
170{
171 struct writeback_control wbc = {
172 .sync_mode = WB_SYNC_NONE,
173 .nr_to_write = LONG_MAX,
174 .range_start = 0,
175 .range_end = LLONG_MAX,
176 };
177
178 return gfs2_ail1_flush(sdp, &wbc);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400179}
180
181/**
182 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
183 * @sdp: the filesystem
184 * @ai: the AIL entry
185 *
186 */
187
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100188static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400189{
190 struct gfs2_bufdata *bd, *s;
191 struct buffer_head *bh;
192
193 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
194 bd_ail_st_list) {
195 bh = bd->bd_bh;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400196 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100197 if (buffer_busy(bh))
198 continue;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400199 if (!buffer_uptodate(bh))
200 gfs2_io_error_bh(sdp, bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400201 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
202 }
203
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400204}
205
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100206/**
207 * gfs2_ail1_empty - Try to empty the ail1 lists
208 * @sdp: The superblock
209 *
210 * Tries to empty the ail1 lists, starting with the oldest first
211 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100213static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000214{
215 struct gfs2_ail *ai, *s;
216 int ret;
217
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000218 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000219 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100220 gfs2_ail1_empty_one(sdp, ai);
221 if (list_empty(&ai->ai_ail1_list))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222 list_move(&ai->ai_list, &sdp->sd_ail2_list);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100223 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224 break;
225 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000226 ret = list_empty(&sdp->sd_ail1_list);
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000227 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000228
229 return ret;
230}
231
Steven Whitehouse26b06a62011-05-21 19:21:07 +0100232static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
233{
234 struct gfs2_ail *ai;
235 struct gfs2_bufdata *bd;
236 struct buffer_head *bh;
237
238 spin_lock(&sdp->sd_ail_lock);
239 list_for_each_entry_reverse(ai, &sdp->sd_ail1_list, ai_list) {
240 list_for_each_entry(bd, &ai->ai_ail1_list, bd_ail_st_list) {
241 bh = bd->bd_bh;
242 if (!buffer_locked(bh))
243 continue;
244 get_bh(bh);
245 spin_unlock(&sdp->sd_ail_lock);
246 wait_on_buffer(bh);
247 brelse(bh);
248 return;
249 }
250 }
251 spin_unlock(&sdp->sd_ail_lock);
252}
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400253
254/**
255 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
256 * @sdp: the filesystem
257 * @ai: the AIL entry
258 *
259 */
260
261static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
262{
263 struct list_head *head = &ai->ai_ail2_list;
264 struct gfs2_bufdata *bd;
265
266 while (!list_empty(head)) {
267 bd = list_entry(head->prev, struct gfs2_bufdata,
268 bd_ail_st_list);
269 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100270 gfs2_remove_from_ail(bd);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400271 }
272}
273
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
275{
276 struct gfs2_ail *ai, *safe;
277 unsigned int old_tail = sdp->sd_log_tail;
278 int wrap = (new_tail < old_tail);
279 int a, b, rm;
280
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000281 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282
283 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
284 a = (old_tail <= ai->ai_first);
285 b = (ai->ai_first < new_tail);
286 rm = (wrap) ? (a || b) : (a && b);
287 if (!rm)
288 continue;
289
290 gfs2_ail2_empty_one(sdp, ai);
291 list_del(&ai->ai_list);
292 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
293 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
294 kfree(ai);
295 }
296
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000297 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298}
299
300/**
301 * gfs2_log_reserve - Make a log reservation
302 * @sdp: The GFS2 superblock
303 * @blks: The number of blocks to reserve
304 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100305 * Note that we never give out the last few blocks of the journal. Thats
Robert Peterson2332c442007-06-18 14:50:20 -0500306 * due to the fact that there is a small number of header blocks
Steven Whitehouseb0041572006-11-23 10:51:34 -0500307 * associated with each log flush. The exact number can't be known until
308 * flush time, so we ensure that we have just enough free blocks at all
309 * times to avoid running out during a log flush.
310 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500311 * We no longer flush the log here, instead we wake up logd to do that
312 * for us. To avoid the thundering herd and to ensure that we deal fairly
313 * with queued waiters, we use an exclusive wait. This means that when we
314 * get woken with enough journal space to get our reservation, we need to
315 * wake the next waiter on the list.
316 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317 * Returns: errno
318 */
319
320int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
321{
Steven Whitehouse89918642007-06-01 15:19:33 +0100322 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500323 unsigned wanted = blks + reserved_blks;
324 DEFINE_WAIT(wait);
325 int did_wait = 0;
326 unsigned int free_blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327
328 if (gfs2_assert_warn(sdp, blks) ||
329 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
330 return -EINVAL;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500331retry:
332 free_blocks = atomic_read(&sdp->sd_log_blks_free);
333 if (unlikely(free_blocks <= wanted)) {
334 do {
335 prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
336 TASK_UNINTERRUPTIBLE);
337 wake_up(&sdp->sd_logd_waitq);
338 did_wait = 1;
339 if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
340 io_schedule();
341 free_blocks = atomic_read(&sdp->sd_log_blks_free);
342 } while(free_blocks <= wanted);
343 finish_wait(&sdp->sd_log_waitq, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344 }
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500345 if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
346 free_blocks - blks) != free_blocks)
347 goto retry;
Steven Whitehouse63997772009-06-12 08:49:20 +0100348 trace_gfs2_log_blocks(sdp, -blks);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500349
350 /*
351 * If we waited, then so might others, wake them up _after_ we get
352 * our share of the log.
353 */
354 if (unlikely(did_wait))
355 wake_up(&sdp->sd_log_waitq);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500356
357 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358
359 return 0;
360}
361
Steven Whitehouse47ac5532012-02-03 15:21:59 +0000362u64 gfs2_log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000363{
Bob Petersonda6dd402007-12-11 18:49:21 -0600364 struct gfs2_journal_extent *je;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365
Bob Petersonda6dd402007-12-11 18:49:21 -0600366 list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
367 if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
Steven Whitehouseff91cc92007-12-14 14:04:34 +0000368 return je->dblock + lbn - je->lblock;
Bob Petersonda6dd402007-12-11 18:49:21 -0600369 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370
Bob Petersonda6dd402007-12-11 18:49:21 -0600371 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372}
373
374/**
375 * log_distance - Compute distance between two journal blocks
376 * @sdp: The GFS2 superblock
377 * @newer: The most recent journal block of the pair
378 * @older: The older journal block of the pair
379 *
380 * Compute the distance (in the journal direction) between two
381 * blocks in the journal
382 *
383 * Returns: the distance in blocks
384 */
385
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400386static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 unsigned int older)
388{
389 int dist;
390
391 dist = newer - older;
392 if (dist < 0)
393 dist += sdp->sd_jdesc->jd_blocks;
394
395 return dist;
396}
397
Robert Peterson2332c442007-06-18 14:50:20 -0500398/**
399 * calc_reserved - Calculate the number of blocks to reserve when
400 * refunding a transaction's unused buffers.
401 * @sdp: The GFS2 superblock
402 *
403 * This is complex. We need to reserve room for all our currently used
404 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
405 * all our journaled data buffers for journaled files (e.g. files in the
406 * meta_fs like rindex, or files for which chattr +j was done.)
407 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
408 * will count it as free space (sd_log_blks_free) and corruption will follow.
409 *
410 * We can have metadata bufs and jdata bufs in the same journal. So each
411 * type gets its own log header, for which we need to reserve a block.
412 * In fact, each type has the potential for needing more than one header
413 * in cases where we have more buffers than will fit on a journal page.
414 * Metadata journal entries take up half the space of journaled buffer entries.
415 * Thus, metadata entries have buf_limit (502) and journaled buffers have
416 * databuf_limit (251) before they cause a wrap around.
417 *
418 * Also, we need to reserve blocks for revoke journal entries and one for an
419 * overall header for the lot.
420 *
421 * Returns: the number of blocks reserved
422 */
423static unsigned int calc_reserved(struct gfs2_sbd *sdp)
424{
425 unsigned int reserved = 0;
426 unsigned int mbuf_limit, metabufhdrs_needed;
427 unsigned int dbuf_limit, databufhdrs_needed;
428 unsigned int revokes = 0;
429
430 mbuf_limit = buf_limit(sdp);
431 metabufhdrs_needed = (sdp->sd_log_commited_buf +
432 (mbuf_limit - 1)) / mbuf_limit;
433 dbuf_limit = databuf_limit(sdp);
434 databufhdrs_needed = (sdp->sd_log_commited_databuf +
435 (dbuf_limit - 1)) / dbuf_limit;
436
Benjamin Marzinski2e95e3f2010-03-10 18:10:19 -0600437 if (sdp->sd_log_commited_revoke > 0)
Robert Peterson2332c442007-06-18 14:50:20 -0500438 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
439 sizeof(u64));
440
441 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
442 sdp->sd_log_commited_databuf + databufhdrs_needed +
443 revokes;
444 /* One for the overall header */
445 if (reserved)
446 reserved++;
447 return reserved;
448}
449
David Teiglandb3b94fa2006-01-16 16:50:04 +0000450static unsigned int current_tail(struct gfs2_sbd *sdp)
451{
452 struct gfs2_ail *ai;
453 unsigned int tail;
454
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000455 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400457 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400459 } else {
460 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461 tail = ai->ai_first;
462 }
463
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000464 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000465
466 return tail;
467}
468
Steven Whitehouse16615be2007-09-17 10:59:52 +0100469void gfs2_log_incr_head(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000470{
Steven Whitehouse47ac5532012-02-03 15:21:59 +0000471 BUG_ON((sdp->sd_log_flush_head == sdp->sd_log_tail) &&
472 (sdp->sd_log_flush_head != sdp->sd_log_head));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473
474 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
475 sdp->sd_log_flush_head = 0;
476 sdp->sd_log_flush_wrapped = 1;
477 }
478}
479
Robert Peterson2332c442007-06-18 14:50:20 -0500480static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481{
482 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
483
484 ail2_empty(sdp, new_tail);
485
Steven Whitehousefd041f02007-11-08 14:55:03 +0000486 atomic_add(dist, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100487 trace_gfs2_log_blocks(sdp, dist);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500488 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
489 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490
491 sdp->sd_log_tail = new_tail;
492}
493
494/**
495 * log_write_header - Get and initialize a journal header buffer
496 * @sdp: The GFS2 superblock
497 *
498 * Returns: the initialized log buffer descriptor
499 */
500
Steven Whitehousecd915492006-09-04 12:49:07 -0400501static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502{
Steven Whitehouse47ac5532012-02-03 15:21:59 +0000503 u64 blkno = gfs2_log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000504 struct buffer_head *bh;
505 struct gfs2_log_header *lh;
506 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400507 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508
David Teiglandb3b94fa2006-01-16 16:50:04 +0000509 bh = sb_getblk(sdp->sd_vfs, blkno);
510 lock_buffer(bh);
511 memset(bh->b_data, 0, bh->b_size);
512 set_buffer_uptodate(bh);
513 clear_buffer_dirty(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000514
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100515 gfs2_ail1_empty(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 tail = current_tail(sdp);
517
518 lh = (struct gfs2_log_header *)bh->b_data;
519 memset(lh, 0, sizeof(struct gfs2_log_header));
520 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500521 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000522 lh->lh_header.__pad0 = cpu_to_be64(0);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500523 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000524 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400525 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
526 lh->lh_flags = cpu_to_be32(flags);
527 lh->lh_tail = cpu_to_be32(tail);
528 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
530 lh->lh_hash = cpu_to_be32(hash);
531
Steven Whitehouse254db572008-09-26 10:23:22 +0100532 bh->b_end_io = end_buffer_write_sync;
Steven Whitehouse254db572008-09-26 10:23:22 +0100533 get_bh(bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400534 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
Christoph Hellwig65299a32011-08-23 14:50:29 +0200535 submit_bh(WRITE_SYNC | REQ_META | REQ_PRIO, bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400536 else
Steven Whitehouse20ed0532011-10-31 09:52:02 +0000537 submit_bh(WRITE_FLUSH_FUA | REQ_META, bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400538 wait_on_buffer(bh);
539
Steven Whitehouse254db572008-09-26 10:23:22 +0100540 if (!buffer_uptodate(bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 gfs2_io_error_bh(sdp, bh);
542 brelse(bh);
543
544 if (sdp->sd_log_tail != tail)
Robert Peterson2332c442007-06-18 14:50:20 -0500545 log_pull_tail(sdp, tail);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546 else
547 gfs2_assert_withdraw(sdp, !pull);
548
549 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100550 gfs2_log_incr_head(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551}
552
553static void log_flush_commit(struct gfs2_sbd *sdp)
554{
Steven Whitehouse16615be2007-09-17 10:59:52 +0100555 DEFINE_WAIT(wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556
Steven Whitehouse16615be2007-09-17 10:59:52 +0100557 if (atomic_read(&sdp->sd_log_in_flight)) {
558 do {
559 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
560 TASK_UNINTERRUPTIBLE);
561 if (atomic_read(&sdp->sd_log_in_flight))
562 io_schedule();
563 } while(atomic_read(&sdp->sd_log_in_flight));
564 finish_wait(&sdp->sd_log_flush_wait, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 }
566
Steven Whitehouse16615be2007-09-17 10:59:52 +0100567 log_write_header(sdp, 0, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568}
569
Bob Peterson4a36d082012-02-14 14:49:57 -0500570int bd_cmp(void *priv, struct list_head *a, struct list_head *b)
571{
572 struct gfs2_bufdata *bda, *bdb;
573
574 bda = list_entry(a, struct gfs2_bufdata, bd_le.le_list);
575 bdb = list_entry(b, struct gfs2_bufdata, bd_le.le_list);
576
577 if (bda->bd_bh->b_blocknr < bdb->bd_bh->b_blocknr)
578 return -1;
579 if (bda->bd_bh->b_blocknr > bdb->bd_bh->b_blocknr)
580 return 1;
581 return 0;
582}
583
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100584static void gfs2_ordered_write(struct gfs2_sbd *sdp)
585{
586 struct gfs2_bufdata *bd;
587 struct buffer_head *bh;
588 LIST_HEAD(written);
589
590 gfs2_log_lock(sdp);
Bob Peterson4a36d082012-02-14 14:49:57 -0500591 list_sort(NULL, &sdp->sd_log_le_ordered, &bd_cmp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100592 while (!list_empty(&sdp->sd_log_le_ordered)) {
593 bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
594 list_move(&bd->bd_le.le_list, &written);
595 bh = bd->bd_bh;
596 if (!buffer_dirty(bh))
597 continue;
598 get_bh(bh);
599 gfs2_log_unlock(sdp);
600 lock_buffer(bh);
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100601 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100602 bh->b_end_io = end_buffer_write_sync;
Jens Axboe721a9602011-03-09 11:56:30 +0100603 submit_bh(WRITE_SYNC, bh);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100604 } else {
605 unlock_buffer(bh);
606 brelse(bh);
607 }
608 gfs2_log_lock(sdp);
609 }
610 list_splice(&written, &sdp->sd_log_le_ordered);
611 gfs2_log_unlock(sdp);
612}
613
614static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
615{
616 struct gfs2_bufdata *bd;
617 struct buffer_head *bh;
618
619 gfs2_log_lock(sdp);
620 while (!list_empty(&sdp->sd_log_le_ordered)) {
621 bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
622 bh = bd->bd_bh;
623 if (buffer_locked(bh)) {
624 get_bh(bh);
625 gfs2_log_unlock(sdp);
626 wait_on_buffer(bh);
627 brelse(bh);
628 gfs2_log_lock(sdp);
629 continue;
630 }
631 list_del_init(&bd->bd_le.le_list);
632 }
633 gfs2_log_unlock(sdp);
634}
635
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400637 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638 * @sdp: the filesystem
639 * @gl: The glock structure to flush. If NULL, flush the whole incore log
640 *
641 */
642
Bob Petersoned4878e2010-05-20 23:30:11 -0400643void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644{
645 struct gfs2_ail *ai;
646
Steven Whitehouse484adff2006-03-29 09:12:12 -0500647 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000648
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000649 /* Log might have been flushed while we waited for the flush lock */
650 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
651 up_write(&sdp->sd_log_flush_lock);
652 return;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000653 }
Steven Whitehouse63997772009-06-12 08:49:20 +0100654 trace_gfs2_log_flush(sdp, 1);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000655
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400656 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
657 INIT_LIST_HEAD(&ai->ai_ail1_list);
658 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659
Steven Whitehouse16615be2007-09-17 10:59:52 +0100660 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
661 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
662 sdp->sd_log_commited_buf);
663 gfs2_assert_withdraw(sdp, 0);
664 }
665 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
666 printk(KERN_INFO "GFS2: log databuf %u %u\n",
667 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
668 gfs2_assert_withdraw(sdp, 0);
669 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 gfs2_assert_withdraw(sdp,
671 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
672
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673 sdp->sd_log_flush_head = sdp->sd_log_head;
674 sdp->sd_log_flush_wrapped = 0;
675 ai->ai_first = sdp->sd_log_flush_head;
676
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100677 gfs2_ordered_write(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 lops_before_commit(sdp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100679 gfs2_ordered_wait(sdp);
680
Steven Whitehouse16615be2007-09-17 10:59:52 +0100681 if (sdp->sd_log_head != sdp->sd_log_flush_head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682 log_flush_commit(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500683 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
684 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000685 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
Steven Whitehouse63997772009-06-12 08:49:20 +0100686 trace_gfs2_log_blocks(sdp, -1);
Robert Peterson2332c442007-06-18 14:50:20 -0500687 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688 log_write_header(sdp, 0, PULL);
Robert Peterson2332c442007-06-18 14:50:20 -0500689 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400691
692 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400694 sdp->sd_log_blks_reserved = 0;
695 sdp->sd_log_commited_buf = 0;
Robert Peterson2332c442007-06-18 14:50:20 -0500696 sdp->sd_log_commited_databuf = 0;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400697 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000698
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000699 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700 if (!list_empty(&ai->ai_ail1_list)) {
701 list_add(&ai->ai_list, &sdp->sd_ail1_list);
702 ai = NULL;
703 }
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000704 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 gfs2_log_unlock(sdp);
Steven Whitehouse63997772009-06-12 08:49:20 +0100706 trace_gfs2_log_flush(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500707 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708
709 kfree(ai);
710}
711
712static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
713{
Robert Peterson2332c442007-06-18 14:50:20 -0500714 unsigned int reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000715 unsigned int unused;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716
717 gfs2_log_lock(sdp);
718
719 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500720 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
721 tr->tr_num_databuf_rm;
722 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
723 (((int)sdp->sd_log_commited_databuf) >= 0));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500725 reserved = calc_reserved(sdp);
Roel Kluin62be1f72008-04-17 17:25:37 +0200726 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000727 unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000728 atomic_add(unused, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100729 trace_gfs2_log_blocks(sdp, unused);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000730 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
Robert Peterson2332c442007-06-18 14:50:20 -0500731 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 sdp->sd_log_blks_reserved = reserved;
733
734 gfs2_log_unlock(sdp);
735}
736
Bob Petersond0109bf2008-01-28 11:20:10 -0600737static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
738{
739 struct list_head *head = &tr->tr_list_buf;
740 struct gfs2_bufdata *bd;
741
742 gfs2_log_lock(sdp);
743 while (!list_empty(head)) {
744 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
745 list_del_init(&bd->bd_list_tr);
746 tr->tr_num_buf--;
747 }
748 gfs2_log_unlock(sdp);
749 gfs2_assert_warn(sdp, !tr->tr_num_buf);
750}
751
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752/**
753 * gfs2_log_commit - Commit a transaction to the log
754 * @sdp: the filesystem
755 * @tr: the transaction
756 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500757 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
758 * or the total number of used blocks (pinned blocks plus AIL blocks)
759 * is greater than thresh2.
760 *
761 * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
762 * journal size.
763 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 * Returns: errno
765 */
766
767void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
768{
769 log_refund(sdp, tr);
Bob Petersond0109bf2008-01-28 11:20:10 -0600770 buf_lo_incore_commit(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771
Steven Whitehouse484adff2006-03-29 09:12:12 -0500772 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500774 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
775 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
776 atomic_read(&sdp->sd_log_thresh2)))
777 wake_up(&sdp->sd_logd_waitq);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778}
779
780/**
781 * gfs2_log_shutdown - write a shutdown header into a journal
782 * @sdp: the filesystem
783 *
784 */
785
786void gfs2_log_shutdown(struct gfs2_sbd *sdp)
787{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500788 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
793 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
794 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
795 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
796
797 sdp->sd_log_flush_head = sdp->sd_log_head;
798 sdp->sd_log_flush_wrapped = 0;
799
Robert Peterson2332c442007-06-18 14:50:20 -0500800 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
801 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802
Steven Whitehousefd041f02007-11-08 14:55:03 +0000803 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400804 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
805 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806
807 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808 sdp->sd_log_tail = sdp->sd_log_head;
809
Steven Whitehouse484adff2006-03-29 09:12:12 -0500810 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811}
812
Steven Whitehousea25311c2006-11-23 11:06:35 -0500813
814/**
815 * gfs2_meta_syncfs - sync all the buffers in a filesystem
816 * @sdp: the filesystem
817 *
818 */
819
820void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
821{
822 gfs2_log_flush(sdp, NULL);
823 for (;;) {
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500824 gfs2_ail1_start(sdp);
Steven Whitehouse26b06a62011-05-21 19:21:07 +0100825 gfs2_ail1_wait(sdp);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100826 if (gfs2_ail1_empty(sdp))
Steven Whitehousea25311c2006-11-23 11:06:35 -0500827 break;
Steven Whitehousea25311c2006-11-23 11:06:35 -0500828 }
Steven Whitehouse380f7c62011-07-14 08:59:44 +0100829 gfs2_log_flush(sdp, NULL);
Steven Whitehousea25311c2006-11-23 11:06:35 -0500830}
831
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500832static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
833{
834 return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
835}
836
837static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
838{
839 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
840 return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
841}
Steven Whitehouseec69b182007-11-09 10:01:41 +0000842
843/**
844 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
845 * @sdp: Pointer to GFS2 superblock
846 *
847 * Also, periodically check to make sure that we're using the most recent
848 * journal index.
849 */
850
851int gfs2_logd(void *data)
852{
853 struct gfs2_sbd *sdp = data;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500854 unsigned long t = 1;
855 DEFINE_WAIT(wait);
856 unsigned preflush;
Steven Whitehouseec69b182007-11-09 10:01:41 +0000857
858 while (!kthread_should_stop()) {
Steven Whitehouseec69b182007-11-09 10:01:41 +0000859
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500860 preflush = atomic_read(&sdp->sd_log_pinned);
861 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100862 gfs2_ail1_empty(sdp);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000863 gfs2_log_flush(sdp, NULL);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000864 }
865
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500866 if (gfs2_ail_flush_reqd(sdp)) {
867 gfs2_ail1_start(sdp);
Steven Whitehouse26b06a62011-05-21 19:21:07 +0100868 gfs2_ail1_wait(sdp);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100869 gfs2_ail1_empty(sdp);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500870 gfs2_log_flush(sdp, NULL);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500871 }
872
Steven Whitehouse26b06a62011-05-21 19:21:07 +0100873 if (!gfs2_ail_flush_reqd(sdp))
874 wake_up(&sdp->sd_log_waitq);
875
Steven Whitehouseec69b182007-11-09 10:01:41 +0000876 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
Tejun Heoa0acae02011-11-21 12:32:22 -0800877
878 try_to_freeze();
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500879
880 do {
881 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
Steven Whitehouse5f487492010-09-09 14:45:00 +0100882 TASK_INTERRUPTIBLE);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500883 if (!gfs2_ail_flush_reqd(sdp) &&
884 !gfs2_jrnl_flush_reqd(sdp) &&
885 !kthread_should_stop())
886 t = schedule_timeout(t);
887 } while(t && !gfs2_ail_flush_reqd(sdp) &&
888 !gfs2_jrnl_flush_reqd(sdp) &&
889 !kthread_should_stop());
890 finish_wait(&sdp->sd_logd_waitq, &wait);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000891 }
892
893 return 0;
894}
895