blob: 03e00417061b79d23d903630219726fbe42ddc86 [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022
23#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050024#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "bmap.h"
26#include "glock.h"
27#include "log.h"
28#include "lops.h"
29#include "meta_io.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050030#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050031#include "dir.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010032#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000033
34#define PULL 1
35
David Teiglandb3b94fa2006-01-16 16:50:04 +000036/**
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;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -040055 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
David Teiglandb3b94fa2006-01-16 16:50:04 +000056
57 if (nstruct > first) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -050058 second = (sdp->sd_sb.sb_bsize -
59 sizeof(struct gfs2_meta_header)) / ssize;
Steven Whitehouse5c676f62006-02-27 17:23:27 -050060 blks += DIV_ROUND_UP(nstruct - first, second);
David Teiglandb3b94fa2006-01-16 16:50:04 +000061 }
62
63 return blks;
64}
65
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040066/**
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010067 * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
68 * @mapping: The associated mapping (maybe NULL)
69 * @bd: The gfs2_bufdata to remove
70 *
Steven Whitehousec618e872011-03-14 12:40:29 +000071 * The ail lock _must_ be held when calling this function
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010072 *
73 */
74
Steven Whitehousef91a0d32007-10-15 16:29:05 +010075void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010076{
77 bd->bd_ail = NULL;
Steven Whitehouse1ad38c42007-09-03 11:01:33 +010078 list_del_init(&bd->bd_ail_st_list);
79 list_del_init(&bd->bd_ail_gl_list);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010080 atomic_dec(&bd->bd_gl->gl_ail_count);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010081 brelse(bd->bd_bh);
82}
83
84/**
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040085 * gfs2_ail1_start_one - Start I/O on a part of the AIL
86 * @sdp: the filesystem
Steven Whitehouse4667a0e2011-04-18 14:18:09 +010087 * @wbc: The writeback control structure
88 * @ai: The ail structure
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040089 *
90 */
91
Steven Whitehouse4667a0e2011-04-18 14:18:09 +010092static void gfs2_ail1_start_one(struct gfs2_sbd *sdp,
93 struct writeback_control *wbc,
94 struct gfs2_ail *ai)
Dave Chinnerd6a079e2011-03-11 11:52:25 +000095__releases(&sdp->sd_ail_lock)
96__acquires(&sdp->sd_ail_lock)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040097{
Steven Whitehouse5ac048b2011-03-30 16:25:51 +010098 struct gfs2_glock *gl = NULL;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +010099 struct address_space *mapping;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400100 struct gfs2_bufdata *bd, *s;
101 struct buffer_head *bh;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400102
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100103restart:
104 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;
123 spin_unlock(&sdp->sd_ail_lock);
124 generic_writepages(mapping, wbc);
125 spin_lock(&sdp->sd_ail_lock);
126 if (wbc->nr_to_write <= 0)
127 break;
128 goto restart;
129 }
130}
131
132
133/**
134 * gfs2_ail1_flush - start writeback of some ail1 entries
135 * @sdp: The super block
136 * @wbc: The writeback control structure
137 *
138 * Writes back some ail1 entries, according to the limits in the
139 * writeback control structure
140 */
141
142void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
143{
144 struct list_head *head = &sdp->sd_ail1_list;
145 struct gfs2_ail *ai;
146
147 spin_lock(&sdp->sd_ail_lock);
148 list_for_each_entry_reverse(ai, head, ai_list) {
149 if (wbc->nr_to_write <= 0)
150 break;
151 gfs2_ail1_start_one(sdp, wbc, ai); /* This may drop ail lock */
152 }
153 spin_unlock(&sdp->sd_ail_lock);
154}
155
156/**
157 * gfs2_ail1_start - start writeback of all ail1 entries
158 * @sdp: The superblock
159 */
160
161static void gfs2_ail1_start(struct gfs2_sbd *sdp)
162{
163 struct writeback_control wbc = {
164 .sync_mode = WB_SYNC_NONE,
165 .nr_to_write = LONG_MAX,
166 .range_start = 0,
167 .range_end = LLONG_MAX,
168 };
169
170 return gfs2_ail1_flush(sdp, &wbc);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400171}
172
173/**
174 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
175 * @sdp: the filesystem
176 * @ai: the AIL entry
177 *
178 */
179
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100180static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400181{
182 struct gfs2_bufdata *bd, *s;
183 struct buffer_head *bh;
184
185 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
186 bd_ail_st_list) {
187 bh = bd->bd_bh;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400188 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100189 if (buffer_busy(bh))
190 continue;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400191 if (!buffer_uptodate(bh))
192 gfs2_io_error_bh(sdp, bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400193 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
194 }
195
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400196}
197
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100198/**
199 * gfs2_ail1_empty - Try to empty the ail1 lists
200 * @sdp: The superblock
201 *
202 * Tries to empty the ail1 lists, starting with the oldest first
203 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100205static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206{
207 struct gfs2_ail *ai, *s;
208 int ret;
209
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000210 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100212 gfs2_ail1_empty_one(sdp, ai);
213 if (list_empty(&ai->ai_ail1_list))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000214 list_move(&ai->ai_list, &sdp->sd_ail2_list);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100215 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 break;
217 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000218 ret = list_empty(&sdp->sd_ail1_list);
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000219 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000220
221 return ret;
222}
223
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400224
225/**
226 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
227 * @sdp: the filesystem
228 * @ai: the AIL entry
229 *
230 */
231
232static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
233{
234 struct list_head *head = &ai->ai_ail2_list;
235 struct gfs2_bufdata *bd;
236
237 while (!list_empty(head)) {
238 bd = list_entry(head->prev, struct gfs2_bufdata,
239 bd_ail_st_list);
240 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100241 gfs2_remove_from_ail(bd);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400242 }
243}
244
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
246{
247 struct gfs2_ail *ai, *safe;
248 unsigned int old_tail = sdp->sd_log_tail;
249 int wrap = (new_tail < old_tail);
250 int a, b, rm;
251
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000252 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253
254 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
255 a = (old_tail <= ai->ai_first);
256 b = (ai->ai_first < new_tail);
257 rm = (wrap) ? (a || b) : (a && b);
258 if (!rm)
259 continue;
260
261 gfs2_ail2_empty_one(sdp, ai);
262 list_del(&ai->ai_list);
263 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
264 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
265 kfree(ai);
266 }
267
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000268 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269}
270
271/**
272 * gfs2_log_reserve - Make a log reservation
273 * @sdp: The GFS2 superblock
274 * @blks: The number of blocks to reserve
275 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100276 * Note that we never give out the last few blocks of the journal. Thats
Robert Peterson2332c442007-06-18 14:50:20 -0500277 * due to the fact that there is a small number of header blocks
Steven Whitehouseb0041572006-11-23 10:51:34 -0500278 * associated with each log flush. The exact number can't be known until
279 * flush time, so we ensure that we have just enough free blocks at all
280 * times to avoid running out during a log flush.
281 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500282 * We no longer flush the log here, instead we wake up logd to do that
283 * for us. To avoid the thundering herd and to ensure that we deal fairly
284 * with queued waiters, we use an exclusive wait. This means that when we
285 * get woken with enough journal space to get our reservation, we need to
286 * wake the next waiter on the list.
287 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288 * Returns: errno
289 */
290
291int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
292{
Steven Whitehouse89918642007-06-01 15:19:33 +0100293 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500294 unsigned wanted = blks + reserved_blks;
295 DEFINE_WAIT(wait);
296 int did_wait = 0;
297 unsigned int free_blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298
299 if (gfs2_assert_warn(sdp, blks) ||
300 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
301 return -EINVAL;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500302retry:
303 free_blocks = atomic_read(&sdp->sd_log_blks_free);
304 if (unlikely(free_blocks <= wanted)) {
305 do {
306 prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
307 TASK_UNINTERRUPTIBLE);
308 wake_up(&sdp->sd_logd_waitq);
309 did_wait = 1;
310 if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
311 io_schedule();
312 free_blocks = atomic_read(&sdp->sd_log_blks_free);
313 } while(free_blocks <= wanted);
314 finish_wait(&sdp->sd_log_waitq, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315 }
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500316 if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
317 free_blocks - blks) != free_blocks)
318 goto retry;
Steven Whitehouse63997772009-06-12 08:49:20 +0100319 trace_gfs2_log_blocks(sdp, -blks);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500320
321 /*
322 * If we waited, then so might others, wake them up _after_ we get
323 * our share of the log.
324 */
325 if (unlikely(did_wait))
326 wake_up(&sdp->sd_log_waitq);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500327
328 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329
330 return 0;
331}
332
Steven Whitehousecd915492006-09-04 12:49:07 -0400333static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334{
Bob Petersonda6dd402007-12-11 18:49:21 -0600335 struct gfs2_journal_extent *je;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336
Bob Petersonda6dd402007-12-11 18:49:21 -0600337 list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
338 if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
Steven Whitehouseff91cc92007-12-14 14:04:34 +0000339 return je->dblock + lbn - je->lblock;
Bob Petersonda6dd402007-12-11 18:49:21 -0600340 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341
Bob Petersonda6dd402007-12-11 18:49:21 -0600342 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343}
344
345/**
346 * log_distance - Compute distance between two journal blocks
347 * @sdp: The GFS2 superblock
348 * @newer: The most recent journal block of the pair
349 * @older: The older journal block of the pair
350 *
351 * Compute the distance (in the journal direction) between two
352 * blocks in the journal
353 *
354 * Returns: the distance in blocks
355 */
356
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400357static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358 unsigned int older)
359{
360 int dist;
361
362 dist = newer - older;
363 if (dist < 0)
364 dist += sdp->sd_jdesc->jd_blocks;
365
366 return dist;
367}
368
Robert Peterson2332c442007-06-18 14:50:20 -0500369/**
370 * calc_reserved - Calculate the number of blocks to reserve when
371 * refunding a transaction's unused buffers.
372 * @sdp: The GFS2 superblock
373 *
374 * This is complex. We need to reserve room for all our currently used
375 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
376 * all our journaled data buffers for journaled files (e.g. files in the
377 * meta_fs like rindex, or files for which chattr +j was done.)
378 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
379 * will count it as free space (sd_log_blks_free) and corruption will follow.
380 *
381 * We can have metadata bufs and jdata bufs in the same journal. So each
382 * type gets its own log header, for which we need to reserve a block.
383 * In fact, each type has the potential for needing more than one header
384 * in cases where we have more buffers than will fit on a journal page.
385 * Metadata journal entries take up half the space of journaled buffer entries.
386 * Thus, metadata entries have buf_limit (502) and journaled buffers have
387 * databuf_limit (251) before they cause a wrap around.
388 *
389 * Also, we need to reserve blocks for revoke journal entries and one for an
390 * overall header for the lot.
391 *
392 * Returns: the number of blocks reserved
393 */
394static unsigned int calc_reserved(struct gfs2_sbd *sdp)
395{
396 unsigned int reserved = 0;
397 unsigned int mbuf_limit, metabufhdrs_needed;
398 unsigned int dbuf_limit, databufhdrs_needed;
399 unsigned int revokes = 0;
400
401 mbuf_limit = buf_limit(sdp);
402 metabufhdrs_needed = (sdp->sd_log_commited_buf +
403 (mbuf_limit - 1)) / mbuf_limit;
404 dbuf_limit = databuf_limit(sdp);
405 databufhdrs_needed = (sdp->sd_log_commited_databuf +
406 (dbuf_limit - 1)) / dbuf_limit;
407
Benjamin Marzinski2e95e3f2010-03-10 18:10:19 -0600408 if (sdp->sd_log_commited_revoke > 0)
Robert Peterson2332c442007-06-18 14:50:20 -0500409 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
410 sizeof(u64));
411
412 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
413 sdp->sd_log_commited_databuf + databufhdrs_needed +
414 revokes;
415 /* One for the overall header */
416 if (reserved)
417 reserved++;
418 return reserved;
419}
420
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421static unsigned int current_tail(struct gfs2_sbd *sdp)
422{
423 struct gfs2_ail *ai;
424 unsigned int tail;
425
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000426 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400428 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000429 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400430 } else {
431 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432 tail = ai->ai_first;
433 }
434
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000435 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436
437 return tail;
438}
439
Steven Whitehouse16615be2007-09-17 10:59:52 +0100440void gfs2_log_incr_head(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441{
442 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehouse16615be2007-09-17 10:59:52 +0100443 BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444
445 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
446 sdp->sd_log_flush_head = 0;
447 sdp->sd_log_flush_wrapped = 1;
448 }
449}
450
451/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100452 * gfs2_log_write_endio - End of I/O for a log buffer
453 * @bh: The buffer head
454 * @uptodate: I/O Status
455 *
456 */
457
458static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
459{
460 struct gfs2_sbd *sdp = bh->b_private;
461 bh->b_private = NULL;
462
463 end_buffer_write_sync(bh, uptodate);
464 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
465 wake_up(&sdp->sd_log_flush_wait);
466}
467
468/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
470 * @sdp: The GFS2 superblock
471 *
472 * Returns: the buffer_head
473 */
474
475struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
476{
Steven Whitehousecd915492006-09-04 12:49:07 -0400477 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478 struct buffer_head *bh;
479
Steven Whitehouse16615be2007-09-17 10:59:52 +0100480 bh = sb_getblk(sdp->sd_vfs, blkno);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481 lock_buffer(bh);
482 memset(bh->b_data, 0, bh->b_size);
483 set_buffer_uptodate(bh);
484 clear_buffer_dirty(bh);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100485 gfs2_log_incr_head(sdp);
486 atomic_inc(&sdp->sd_log_in_flight);
487 bh->b_private = sdp;
488 bh->b_end_io = gfs2_log_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489
490 return bh;
491}
492
493/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100494 * gfs2_fake_write_endio -
495 * @bh: The buffer head
496 * @uptodate: The I/O Status
497 *
498 */
499
500static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
501{
502 struct buffer_head *real_bh = bh->b_private;
Steven Whitehouse5a60c532007-09-26 09:39:31 +0100503 struct gfs2_bufdata *bd = real_bh->b_private;
504 struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100505
506 end_buffer_write_sync(bh, uptodate);
507 free_buffer_head(bh);
508 unlock_buffer(real_bh);
509 brelse(real_bh);
510 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
511 wake_up(&sdp->sd_log_flush_wait);
512}
513
514/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
516 * @sdp: the filesystem
517 * @data: the data the buffer_head should point to
518 *
519 * Returns: the log buffer descriptor
520 */
521
522struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
523 struct buffer_head *real)
524{
Steven Whitehousecd915492006-09-04 12:49:07 -0400525 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526 struct buffer_head *bh;
527
Steven Whitehouse16615be2007-09-17 10:59:52 +0100528 bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529 atomic_set(&bh->b_count, 1);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100530 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000531 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532 bh->b_blocknr = blkno;
533 bh->b_size = sdp->sd_sb.sb_bsize;
534 bh->b_bdev = sdp->sd_vfs->s_bdev;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100535 bh->b_private = real;
536 bh->b_end_io = gfs2_fake_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537
Steven Whitehouse16615be2007-09-17 10:59:52 +0100538 gfs2_log_incr_head(sdp);
539 atomic_inc(&sdp->sd_log_in_flight);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540
541 return bh;
542}
543
Robert Peterson2332c442007-06-18 14:50:20 -0500544static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545{
546 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
547
548 ail2_empty(sdp, new_tail);
549
Steven Whitehousefd041f02007-11-08 14:55:03 +0000550 atomic_add(dist, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100551 trace_gfs2_log_blocks(sdp, dist);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500552 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
553 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554
555 sdp->sd_log_tail = new_tail;
556}
557
558/**
559 * log_write_header - Get and initialize a journal header buffer
560 * @sdp: The GFS2 superblock
561 *
562 * Returns: the initialized log buffer descriptor
563 */
564
Steven Whitehousecd915492006-09-04 12:49:07 -0400565static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566{
Steven Whitehousecd915492006-09-04 12:49:07 -0400567 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 struct buffer_head *bh;
569 struct gfs2_log_header *lh;
570 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400571 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573 bh = sb_getblk(sdp->sd_vfs, blkno);
574 lock_buffer(bh);
575 memset(bh->b_data, 0, bh->b_size);
576 set_buffer_uptodate(bh);
577 clear_buffer_dirty(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100579 gfs2_ail1_empty(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 tail = current_tail(sdp);
581
582 lh = (struct gfs2_log_header *)bh->b_data;
583 memset(lh, 0, sizeof(struct gfs2_log_header));
584 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500585 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000586 lh->lh_header.__pad0 = cpu_to_be64(0);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500587 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000588 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400589 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
590 lh->lh_flags = cpu_to_be32(flags);
591 lh->lh_tail = cpu_to_be32(tail);
592 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
594 lh->lh_hash = cpu_to_be32(hash);
595
Steven Whitehouse254db572008-09-26 10:23:22 +0100596 bh->b_end_io = end_buffer_write_sync;
Steven Whitehouse254db572008-09-26 10:23:22 +0100597 get_bh(bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400598 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200599 submit_bh(WRITE_SYNC | REQ_META, bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400600 else
601 submit_bh(WRITE_FLUSH_FUA | REQ_META, bh);
602 wait_on_buffer(bh);
603
Steven Whitehouse254db572008-09-26 10:23:22 +0100604 if (!buffer_uptodate(bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605 gfs2_io_error_bh(sdp, bh);
606 brelse(bh);
607
608 if (sdp->sd_log_tail != tail)
Robert Peterson2332c442007-06-18 14:50:20 -0500609 log_pull_tail(sdp, tail);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 else
611 gfs2_assert_withdraw(sdp, !pull);
612
613 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100614 gfs2_log_incr_head(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615}
616
617static void log_flush_commit(struct gfs2_sbd *sdp)
618{
Steven Whitehouse16615be2007-09-17 10:59:52 +0100619 DEFINE_WAIT(wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620
Steven Whitehouse16615be2007-09-17 10:59:52 +0100621 if (atomic_read(&sdp->sd_log_in_flight)) {
622 do {
623 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
624 TASK_UNINTERRUPTIBLE);
625 if (atomic_read(&sdp->sd_log_in_flight))
626 io_schedule();
627 } while(atomic_read(&sdp->sd_log_in_flight));
628 finish_wait(&sdp->sd_log_flush_wait, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629 }
630
Steven Whitehouse16615be2007-09-17 10:59:52 +0100631 log_write_header(sdp, 0, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632}
633
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100634static void gfs2_ordered_write(struct gfs2_sbd *sdp)
635{
636 struct gfs2_bufdata *bd;
637 struct buffer_head *bh;
638 LIST_HEAD(written);
639
640 gfs2_log_lock(sdp);
641 while (!list_empty(&sdp->sd_log_le_ordered)) {
642 bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
643 list_move(&bd->bd_le.le_list, &written);
644 bh = bd->bd_bh;
645 if (!buffer_dirty(bh))
646 continue;
647 get_bh(bh);
648 gfs2_log_unlock(sdp);
649 lock_buffer(bh);
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100650 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100651 bh->b_end_io = end_buffer_write_sync;
Jens Axboe721a9602011-03-09 11:56:30 +0100652 submit_bh(WRITE_SYNC, bh);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100653 } else {
654 unlock_buffer(bh);
655 brelse(bh);
656 }
657 gfs2_log_lock(sdp);
658 }
659 list_splice(&written, &sdp->sd_log_le_ordered);
660 gfs2_log_unlock(sdp);
661}
662
663static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
664{
665 struct gfs2_bufdata *bd;
666 struct buffer_head *bh;
667
668 gfs2_log_lock(sdp);
669 while (!list_empty(&sdp->sd_log_le_ordered)) {
670 bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
671 bh = bd->bd_bh;
672 if (buffer_locked(bh)) {
673 get_bh(bh);
674 gfs2_log_unlock(sdp);
675 wait_on_buffer(bh);
676 brelse(bh);
677 gfs2_log_lock(sdp);
678 continue;
679 }
680 list_del_init(&bd->bd_le.le_list);
681 }
682 gfs2_log_unlock(sdp);
683}
684
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400686 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 * @sdp: the filesystem
688 * @gl: The glock structure to flush. If NULL, flush the whole incore log
689 *
690 */
691
Bob Petersoned4878e2010-05-20 23:30:11 -0400692void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693{
694 struct gfs2_ail *ai;
695
Steven Whitehouse484adff2006-03-29 09:12:12 -0500696 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000697
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000698 /* Log might have been flushed while we waited for the flush lock */
699 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
700 up_write(&sdp->sd_log_flush_lock);
701 return;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000702 }
Steven Whitehouse63997772009-06-12 08:49:20 +0100703 trace_gfs2_log_flush(sdp, 1);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000704
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400705 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
706 INIT_LIST_HEAD(&ai->ai_ail1_list);
707 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708
Steven Whitehouse16615be2007-09-17 10:59:52 +0100709 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
710 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
711 sdp->sd_log_commited_buf);
712 gfs2_assert_withdraw(sdp, 0);
713 }
714 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
715 printk(KERN_INFO "GFS2: log databuf %u %u\n",
716 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
717 gfs2_assert_withdraw(sdp, 0);
718 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719 gfs2_assert_withdraw(sdp,
720 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
721
David Teiglandb3b94fa2006-01-16 16:50:04 +0000722 sdp->sd_log_flush_head = sdp->sd_log_head;
723 sdp->sd_log_flush_wrapped = 0;
724 ai->ai_first = sdp->sd_log_flush_head;
725
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100726 gfs2_ordered_write(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727 lops_before_commit(sdp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100728 gfs2_ordered_wait(sdp);
729
Steven Whitehouse16615be2007-09-17 10:59:52 +0100730 if (sdp->sd_log_head != sdp->sd_log_flush_head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000731 log_flush_commit(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500732 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
733 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000734 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
Steven Whitehouse63997772009-06-12 08:49:20 +0100735 trace_gfs2_log_blocks(sdp, -1);
Robert Peterson2332c442007-06-18 14:50:20 -0500736 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 log_write_header(sdp, 0, PULL);
Robert Peterson2332c442007-06-18 14:50:20 -0500738 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400740
741 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400743 sdp->sd_log_blks_reserved = 0;
744 sdp->sd_log_commited_buf = 0;
Robert Peterson2332c442007-06-18 14:50:20 -0500745 sdp->sd_log_commited_databuf = 0;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400746 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000748 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 if (!list_empty(&ai->ai_ail1_list)) {
750 list_add(&ai->ai_list, &sdp->sd_ail1_list);
751 ai = NULL;
752 }
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000753 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754 gfs2_log_unlock(sdp);
Steven Whitehouse63997772009-06-12 08:49:20 +0100755 trace_gfs2_log_flush(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500756 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757
758 kfree(ai);
759}
760
761static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
762{
Robert Peterson2332c442007-06-18 14:50:20 -0500763 unsigned int reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000764 unsigned int unused;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765
766 gfs2_log_lock(sdp);
767
768 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500769 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
770 tr->tr_num_databuf_rm;
771 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
772 (((int)sdp->sd_log_commited_databuf) >= 0));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500774 reserved = calc_reserved(sdp);
Roel Kluin62be1f72008-04-17 17:25:37 +0200775 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000776 unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000777 atomic_add(unused, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100778 trace_gfs2_log_blocks(sdp, unused);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000779 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
Robert Peterson2332c442007-06-18 14:50:20 -0500780 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 sdp->sd_log_blks_reserved = reserved;
782
783 gfs2_log_unlock(sdp);
784}
785
Bob Petersond0109bf2008-01-28 11:20:10 -0600786static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
787{
788 struct list_head *head = &tr->tr_list_buf;
789 struct gfs2_bufdata *bd;
790
791 gfs2_log_lock(sdp);
792 while (!list_empty(head)) {
793 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
794 list_del_init(&bd->bd_list_tr);
795 tr->tr_num_buf--;
796 }
797 gfs2_log_unlock(sdp);
798 gfs2_assert_warn(sdp, !tr->tr_num_buf);
799}
800
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801/**
802 * gfs2_log_commit - Commit a transaction to the log
803 * @sdp: the filesystem
804 * @tr: the transaction
805 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500806 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
807 * or the total number of used blocks (pinned blocks plus AIL blocks)
808 * is greater than thresh2.
809 *
810 * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
811 * journal size.
812 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813 * Returns: errno
814 */
815
816void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
817{
818 log_refund(sdp, tr);
Bob Petersond0109bf2008-01-28 11:20:10 -0600819 buf_lo_incore_commit(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820
Steven Whitehouse484adff2006-03-29 09:12:12 -0500821 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500823 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
824 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
825 atomic_read(&sdp->sd_log_thresh2)))
826 wake_up(&sdp->sd_logd_waitq);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827}
828
829/**
830 * gfs2_log_shutdown - write a shutdown header into a journal
831 * @sdp: the filesystem
832 *
833 */
834
835void gfs2_log_shutdown(struct gfs2_sbd *sdp)
836{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500837 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
842 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
843 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
844 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
845
846 sdp->sd_log_flush_head = sdp->sd_log_head;
847 sdp->sd_log_flush_wrapped = 0;
848
Robert Peterson2332c442007-06-18 14:50:20 -0500849 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
850 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851
Steven Whitehousefd041f02007-11-08 14:55:03 +0000852 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400853 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
854 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855
856 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857 sdp->sd_log_tail = sdp->sd_log_head;
858
Steven Whitehouse484adff2006-03-29 09:12:12 -0500859 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000860}
861
Steven Whitehousea25311c2006-11-23 11:06:35 -0500862
863/**
864 * gfs2_meta_syncfs - sync all the buffers in a filesystem
865 * @sdp: the filesystem
866 *
867 */
868
869void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
870{
871 gfs2_log_flush(sdp, NULL);
872 for (;;) {
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500873 gfs2_ail1_start(sdp);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100874 if (gfs2_ail1_empty(sdp))
Steven Whitehousea25311c2006-11-23 11:06:35 -0500875 break;
876 msleep(10);
877 }
878}
879
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500880static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
881{
882 return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
883}
884
885static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
886{
887 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
888 return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
889}
Steven Whitehouseec69b182007-11-09 10:01:41 +0000890
891/**
892 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
893 * @sdp: Pointer to GFS2 superblock
894 *
895 * Also, periodically check to make sure that we're using the most recent
896 * journal index.
897 */
898
899int gfs2_logd(void *data)
900{
901 struct gfs2_sbd *sdp = data;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500902 unsigned long t = 1;
903 DEFINE_WAIT(wait);
904 unsigned preflush;
Steven Whitehouseec69b182007-11-09 10:01:41 +0000905
906 while (!kthread_should_stop()) {
Steven Whitehouseec69b182007-11-09 10:01:41 +0000907
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500908 preflush = atomic_read(&sdp->sd_log_pinned);
909 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100910 gfs2_ail1_empty(sdp);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000911 gfs2_log_flush(sdp, NULL);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000912 }
913
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500914 if (gfs2_ail_flush_reqd(sdp)) {
915 gfs2_ail1_start(sdp);
916 io_schedule();
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100917 gfs2_ail1_empty(sdp);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500918 gfs2_log_flush(sdp, NULL);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500919 }
920
921 wake_up(&sdp->sd_log_waitq);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000922 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
923 if (freezing(current))
924 refrigerator();
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500925
926 do {
927 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
Steven Whitehouse5f487492010-09-09 14:45:00 +0100928 TASK_INTERRUPTIBLE);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500929 if (!gfs2_ail_flush_reqd(sdp) &&
930 !gfs2_jrnl_flush_reqd(sdp) &&
931 !kthread_should_stop())
932 t = schedule_timeout(t);
933 } while(t && !gfs2_ail_flush_reqd(sdp) &&
934 !gfs2_jrnl_flush_reqd(sdp) &&
935 !kthread_should_stop());
936 finish_wait(&sdp->sd_logd_waitq, &wait);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000937 }
938
939 return 0;
940}
941