blob: ad1f1887633f4d540b0457b3296943351fb01dc6 [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
Steven Whitehousec83ae9c2011-04-18 14:18:38 +0100147 trace_gfs2_ail_flush(sdp, wbc, 1);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100148 spin_lock(&sdp->sd_ail_lock);
149 list_for_each_entry_reverse(ai, head, ai_list) {
150 if (wbc->nr_to_write <= 0)
151 break;
152 gfs2_ail1_start_one(sdp, wbc, ai); /* This may drop ail lock */
153 }
154 spin_unlock(&sdp->sd_ail_lock);
Steven Whitehousec83ae9c2011-04-18 14:18:38 +0100155 trace_gfs2_ail_flush(sdp, wbc, 0);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100156}
157
158/**
159 * gfs2_ail1_start - start writeback of all ail1 entries
160 * @sdp: The superblock
161 */
162
163static void gfs2_ail1_start(struct gfs2_sbd *sdp)
164{
165 struct writeback_control wbc = {
166 .sync_mode = WB_SYNC_NONE,
167 .nr_to_write = LONG_MAX,
168 .range_start = 0,
169 .range_end = LLONG_MAX,
170 };
171
172 return gfs2_ail1_flush(sdp, &wbc);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400173}
174
175/**
176 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
177 * @sdp: the filesystem
178 * @ai: the AIL entry
179 *
180 */
181
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100182static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400183{
184 struct gfs2_bufdata *bd, *s;
185 struct buffer_head *bh;
186
187 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
188 bd_ail_st_list) {
189 bh = bd->bd_bh;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400190 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100191 if (buffer_busy(bh))
192 continue;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400193 if (!buffer_uptodate(bh))
194 gfs2_io_error_bh(sdp, bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400195 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
196 }
197
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400198}
199
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100200/**
201 * gfs2_ail1_empty - Try to empty the ail1 lists
202 * @sdp: The superblock
203 *
204 * Tries to empty the ail1 lists, starting with the oldest first
205 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100207static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208{
209 struct gfs2_ail *ai, *s;
210 int ret;
211
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000212 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100214 gfs2_ail1_empty_one(sdp, ai);
215 if (list_empty(&ai->ai_ail1_list))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 list_move(&ai->ai_list, &sdp->sd_ail2_list);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100217 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000218 break;
219 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000220 ret = list_empty(&sdp->sd_ail1_list);
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000221 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222
223 return ret;
224}
225
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400226
227/**
228 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
229 * @sdp: the filesystem
230 * @ai: the AIL entry
231 *
232 */
233
234static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
235{
236 struct list_head *head = &ai->ai_ail2_list;
237 struct gfs2_bufdata *bd;
238
239 while (!list_empty(head)) {
240 bd = list_entry(head->prev, struct gfs2_bufdata,
241 bd_ail_st_list);
242 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100243 gfs2_remove_from_ail(bd);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400244 }
245}
246
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
248{
249 struct gfs2_ail *ai, *safe;
250 unsigned int old_tail = sdp->sd_log_tail;
251 int wrap = (new_tail < old_tail);
252 int a, b, rm;
253
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000254 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255
256 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
257 a = (old_tail <= ai->ai_first);
258 b = (ai->ai_first < new_tail);
259 rm = (wrap) ? (a || b) : (a && b);
260 if (!rm)
261 continue;
262
263 gfs2_ail2_empty_one(sdp, ai);
264 list_del(&ai->ai_list);
265 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
266 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
267 kfree(ai);
268 }
269
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000270 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271}
272
273/**
274 * gfs2_log_reserve - Make a log reservation
275 * @sdp: The GFS2 superblock
276 * @blks: The number of blocks to reserve
277 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100278 * Note that we never give out the last few blocks of the journal. Thats
Robert Peterson2332c442007-06-18 14:50:20 -0500279 * due to the fact that there is a small number of header blocks
Steven Whitehouseb0041572006-11-23 10:51:34 -0500280 * associated with each log flush. The exact number can't be known until
281 * flush time, so we ensure that we have just enough free blocks at all
282 * times to avoid running out during a log flush.
283 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500284 * We no longer flush the log here, instead we wake up logd to do that
285 * for us. To avoid the thundering herd and to ensure that we deal fairly
286 * with queued waiters, we use an exclusive wait. This means that when we
287 * get woken with enough journal space to get our reservation, we need to
288 * wake the next waiter on the list.
289 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290 * Returns: errno
291 */
292
293int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
294{
Steven Whitehouse89918642007-06-01 15:19:33 +0100295 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500296 unsigned wanted = blks + reserved_blks;
297 DEFINE_WAIT(wait);
298 int did_wait = 0;
299 unsigned int free_blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300
301 if (gfs2_assert_warn(sdp, blks) ||
302 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
303 return -EINVAL;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500304retry:
305 free_blocks = atomic_read(&sdp->sd_log_blks_free);
306 if (unlikely(free_blocks <= wanted)) {
307 do {
308 prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
309 TASK_UNINTERRUPTIBLE);
310 wake_up(&sdp->sd_logd_waitq);
311 did_wait = 1;
312 if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
313 io_schedule();
314 free_blocks = atomic_read(&sdp->sd_log_blks_free);
315 } while(free_blocks <= wanted);
316 finish_wait(&sdp->sd_log_waitq, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317 }
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500318 if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
319 free_blocks - blks) != free_blocks)
320 goto retry;
Steven Whitehouse63997772009-06-12 08:49:20 +0100321 trace_gfs2_log_blocks(sdp, -blks);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500322
323 /*
324 * If we waited, then so might others, wake them up _after_ we get
325 * our share of the log.
326 */
327 if (unlikely(did_wait))
328 wake_up(&sdp->sd_log_waitq);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500329
330 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331
332 return 0;
333}
334
Steven Whitehousecd915492006-09-04 12:49:07 -0400335static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336{
Bob Petersonda6dd402007-12-11 18:49:21 -0600337 struct gfs2_journal_extent *je;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338
Bob Petersonda6dd402007-12-11 18:49:21 -0600339 list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
340 if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
Steven Whitehouseff91cc92007-12-14 14:04:34 +0000341 return je->dblock + lbn - je->lblock;
Bob Petersonda6dd402007-12-11 18:49:21 -0600342 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343
Bob Petersonda6dd402007-12-11 18:49:21 -0600344 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345}
346
347/**
348 * log_distance - Compute distance between two journal blocks
349 * @sdp: The GFS2 superblock
350 * @newer: The most recent journal block of the pair
351 * @older: The older journal block of the pair
352 *
353 * Compute the distance (in the journal direction) between two
354 * blocks in the journal
355 *
356 * Returns: the distance in blocks
357 */
358
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400359static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360 unsigned int older)
361{
362 int dist;
363
364 dist = newer - older;
365 if (dist < 0)
366 dist += sdp->sd_jdesc->jd_blocks;
367
368 return dist;
369}
370
Robert Peterson2332c442007-06-18 14:50:20 -0500371/**
372 * calc_reserved - Calculate the number of blocks to reserve when
373 * refunding a transaction's unused buffers.
374 * @sdp: The GFS2 superblock
375 *
376 * This is complex. We need to reserve room for all our currently used
377 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
378 * all our journaled data buffers for journaled files (e.g. files in the
379 * meta_fs like rindex, or files for which chattr +j was done.)
380 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
381 * will count it as free space (sd_log_blks_free) and corruption will follow.
382 *
383 * We can have metadata bufs and jdata bufs in the same journal. So each
384 * type gets its own log header, for which we need to reserve a block.
385 * In fact, each type has the potential for needing more than one header
386 * in cases where we have more buffers than will fit on a journal page.
387 * Metadata journal entries take up half the space of journaled buffer entries.
388 * Thus, metadata entries have buf_limit (502) and journaled buffers have
389 * databuf_limit (251) before they cause a wrap around.
390 *
391 * Also, we need to reserve blocks for revoke journal entries and one for an
392 * overall header for the lot.
393 *
394 * Returns: the number of blocks reserved
395 */
396static unsigned int calc_reserved(struct gfs2_sbd *sdp)
397{
398 unsigned int reserved = 0;
399 unsigned int mbuf_limit, metabufhdrs_needed;
400 unsigned int dbuf_limit, databufhdrs_needed;
401 unsigned int revokes = 0;
402
403 mbuf_limit = buf_limit(sdp);
404 metabufhdrs_needed = (sdp->sd_log_commited_buf +
405 (mbuf_limit - 1)) / mbuf_limit;
406 dbuf_limit = databuf_limit(sdp);
407 databufhdrs_needed = (sdp->sd_log_commited_databuf +
408 (dbuf_limit - 1)) / dbuf_limit;
409
Benjamin Marzinski2e95e3f2010-03-10 18:10:19 -0600410 if (sdp->sd_log_commited_revoke > 0)
Robert Peterson2332c442007-06-18 14:50:20 -0500411 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
412 sizeof(u64));
413
414 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
415 sdp->sd_log_commited_databuf + databufhdrs_needed +
416 revokes;
417 /* One for the overall header */
418 if (reserved)
419 reserved++;
420 return reserved;
421}
422
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423static unsigned int current_tail(struct gfs2_sbd *sdp)
424{
425 struct gfs2_ail *ai;
426 unsigned int tail;
427
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000428 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000429
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400430 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400432 } else {
433 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000434 tail = ai->ai_first;
435 }
436
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000437 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438
439 return tail;
440}
441
Steven Whitehouse16615be2007-09-17 10:59:52 +0100442void gfs2_log_incr_head(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443{
444 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehouse16615be2007-09-17 10:59:52 +0100445 BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446
447 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
448 sdp->sd_log_flush_head = 0;
449 sdp->sd_log_flush_wrapped = 1;
450 }
451}
452
453/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100454 * gfs2_log_write_endio - End of I/O for a log buffer
455 * @bh: The buffer head
456 * @uptodate: I/O Status
457 *
458 */
459
460static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
461{
462 struct gfs2_sbd *sdp = bh->b_private;
463 bh->b_private = NULL;
464
465 end_buffer_write_sync(bh, uptodate);
466 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
467 wake_up(&sdp->sd_log_flush_wait);
468}
469
470/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
472 * @sdp: The GFS2 superblock
473 *
474 * Returns: the buffer_head
475 */
476
477struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
478{
Steven Whitehousecd915492006-09-04 12:49:07 -0400479 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 struct buffer_head *bh;
481
Steven Whitehouse16615be2007-09-17 10:59:52 +0100482 bh = sb_getblk(sdp->sd_vfs, blkno);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483 lock_buffer(bh);
484 memset(bh->b_data, 0, bh->b_size);
485 set_buffer_uptodate(bh);
486 clear_buffer_dirty(bh);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100487 gfs2_log_incr_head(sdp);
488 atomic_inc(&sdp->sd_log_in_flight);
489 bh->b_private = sdp;
490 bh->b_end_io = gfs2_log_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491
492 return bh;
493}
494
495/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100496 * gfs2_fake_write_endio -
497 * @bh: The buffer head
498 * @uptodate: The I/O Status
499 *
500 */
501
502static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
503{
504 struct buffer_head *real_bh = bh->b_private;
Steven Whitehouse5a60c532007-09-26 09:39:31 +0100505 struct gfs2_bufdata *bd = real_bh->b_private;
506 struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100507
508 end_buffer_write_sync(bh, uptodate);
509 free_buffer_head(bh);
510 unlock_buffer(real_bh);
511 brelse(real_bh);
512 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
513 wake_up(&sdp->sd_log_flush_wait);
514}
515
516/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000517 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
518 * @sdp: the filesystem
519 * @data: the data the buffer_head should point to
520 *
521 * Returns: the log buffer descriptor
522 */
523
524struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
525 struct buffer_head *real)
526{
Steven Whitehousecd915492006-09-04 12:49:07 -0400527 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528 struct buffer_head *bh;
529
Steven Whitehouse16615be2007-09-17 10:59:52 +0100530 bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531 atomic_set(&bh->b_count, 1);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100532 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000533 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534 bh->b_blocknr = blkno;
535 bh->b_size = sdp->sd_sb.sb_bsize;
536 bh->b_bdev = sdp->sd_vfs->s_bdev;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100537 bh->b_private = real;
538 bh->b_end_io = gfs2_fake_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539
Steven Whitehouse16615be2007-09-17 10:59:52 +0100540 gfs2_log_incr_head(sdp);
541 atomic_inc(&sdp->sd_log_in_flight);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000542
543 return bh;
544}
545
Robert Peterson2332c442007-06-18 14:50:20 -0500546static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547{
548 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
549
550 ail2_empty(sdp, new_tail);
551
Steven Whitehousefd041f02007-11-08 14:55:03 +0000552 atomic_add(dist, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100553 trace_gfs2_log_blocks(sdp, dist);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500554 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
555 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556
557 sdp->sd_log_tail = new_tail;
558}
559
560/**
561 * log_write_header - Get and initialize a journal header buffer
562 * @sdp: The GFS2 superblock
563 *
564 * Returns: the initialized log buffer descriptor
565 */
566
Steven Whitehousecd915492006-09-04 12:49:07 -0400567static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568{
Steven Whitehousecd915492006-09-04 12:49:07 -0400569 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570 struct buffer_head *bh;
571 struct gfs2_log_header *lh;
572 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400573 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 bh = sb_getblk(sdp->sd_vfs, blkno);
576 lock_buffer(bh);
577 memset(bh->b_data, 0, bh->b_size);
578 set_buffer_uptodate(bh);
579 clear_buffer_dirty(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100581 gfs2_ail1_empty(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 tail = current_tail(sdp);
583
584 lh = (struct gfs2_log_header *)bh->b_data;
585 memset(lh, 0, sizeof(struct gfs2_log_header));
586 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500587 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000588 lh->lh_header.__pad0 = cpu_to_be64(0);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500589 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000590 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400591 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
592 lh->lh_flags = cpu_to_be32(flags);
593 lh->lh_tail = cpu_to_be32(tail);
594 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
596 lh->lh_hash = cpu_to_be32(hash);
597
Steven Whitehouse254db572008-09-26 10:23:22 +0100598 bh->b_end_io = end_buffer_write_sync;
Steven Whitehouse254db572008-09-26 10:23:22 +0100599 get_bh(bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400600 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200601 submit_bh(WRITE_SYNC | REQ_META, bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400602 else
603 submit_bh(WRITE_FLUSH_FUA | REQ_META, bh);
604 wait_on_buffer(bh);
605
Steven Whitehouse254db572008-09-26 10:23:22 +0100606 if (!buffer_uptodate(bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 gfs2_io_error_bh(sdp, bh);
608 brelse(bh);
609
610 if (sdp->sd_log_tail != tail)
Robert Peterson2332c442007-06-18 14:50:20 -0500611 log_pull_tail(sdp, tail);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 else
613 gfs2_assert_withdraw(sdp, !pull);
614
615 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100616 gfs2_log_incr_head(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617}
618
619static void log_flush_commit(struct gfs2_sbd *sdp)
620{
Steven Whitehouse16615be2007-09-17 10:59:52 +0100621 DEFINE_WAIT(wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622
Steven Whitehouse16615be2007-09-17 10:59:52 +0100623 if (atomic_read(&sdp->sd_log_in_flight)) {
624 do {
625 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
626 TASK_UNINTERRUPTIBLE);
627 if (atomic_read(&sdp->sd_log_in_flight))
628 io_schedule();
629 } while(atomic_read(&sdp->sd_log_in_flight));
630 finish_wait(&sdp->sd_log_flush_wait, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000631 }
632
Steven Whitehouse16615be2007-09-17 10:59:52 +0100633 log_write_header(sdp, 0, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634}
635
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100636static void gfs2_ordered_write(struct gfs2_sbd *sdp)
637{
638 struct gfs2_bufdata *bd;
639 struct buffer_head *bh;
640 LIST_HEAD(written);
641
642 gfs2_log_lock(sdp);
643 while (!list_empty(&sdp->sd_log_le_ordered)) {
644 bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
645 list_move(&bd->bd_le.le_list, &written);
646 bh = bd->bd_bh;
647 if (!buffer_dirty(bh))
648 continue;
649 get_bh(bh);
650 gfs2_log_unlock(sdp);
651 lock_buffer(bh);
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100652 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100653 bh->b_end_io = end_buffer_write_sync;
Jens Axboe721a9602011-03-09 11:56:30 +0100654 submit_bh(WRITE_SYNC, bh);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100655 } else {
656 unlock_buffer(bh);
657 brelse(bh);
658 }
659 gfs2_log_lock(sdp);
660 }
661 list_splice(&written, &sdp->sd_log_le_ordered);
662 gfs2_log_unlock(sdp);
663}
664
665static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
666{
667 struct gfs2_bufdata *bd;
668 struct buffer_head *bh;
669
670 gfs2_log_lock(sdp);
671 while (!list_empty(&sdp->sd_log_le_ordered)) {
672 bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
673 bh = bd->bd_bh;
674 if (buffer_locked(bh)) {
675 get_bh(bh);
676 gfs2_log_unlock(sdp);
677 wait_on_buffer(bh);
678 brelse(bh);
679 gfs2_log_lock(sdp);
680 continue;
681 }
682 list_del_init(&bd->bd_le.le_list);
683 }
684 gfs2_log_unlock(sdp);
685}
686
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400688 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 * @sdp: the filesystem
690 * @gl: The glock structure to flush. If NULL, flush the whole incore log
691 *
692 */
693
Bob Petersoned4878e2010-05-20 23:30:11 -0400694void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695{
696 struct gfs2_ail *ai;
697
Steven Whitehouse484adff2006-03-29 09:12:12 -0500698 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000699
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000700 /* Log might have been flushed while we waited for the flush lock */
701 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
702 up_write(&sdp->sd_log_flush_lock);
703 return;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000704 }
Steven Whitehouse63997772009-06-12 08:49:20 +0100705 trace_gfs2_log_flush(sdp, 1);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000706
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400707 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
708 INIT_LIST_HEAD(&ai->ai_ail1_list);
709 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710
Steven Whitehouse16615be2007-09-17 10:59:52 +0100711 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
712 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
713 sdp->sd_log_commited_buf);
714 gfs2_assert_withdraw(sdp, 0);
715 }
716 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
717 printk(KERN_INFO "GFS2: log databuf %u %u\n",
718 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
719 gfs2_assert_withdraw(sdp, 0);
720 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721 gfs2_assert_withdraw(sdp,
722 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
723
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 sdp->sd_log_flush_head = sdp->sd_log_head;
725 sdp->sd_log_flush_wrapped = 0;
726 ai->ai_first = sdp->sd_log_flush_head;
727
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100728 gfs2_ordered_write(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 lops_before_commit(sdp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100730 gfs2_ordered_wait(sdp);
731
Steven Whitehouse16615be2007-09-17 10:59:52 +0100732 if (sdp->sd_log_head != sdp->sd_log_flush_head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 log_flush_commit(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500734 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
735 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000736 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
Steven Whitehouse63997772009-06-12 08:49:20 +0100737 trace_gfs2_log_blocks(sdp, -1);
Robert Peterson2332c442007-06-18 14:50:20 -0500738 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739 log_write_header(sdp, 0, PULL);
Robert Peterson2332c442007-06-18 14:50:20 -0500740 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400742
743 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400745 sdp->sd_log_blks_reserved = 0;
746 sdp->sd_log_commited_buf = 0;
Robert Peterson2332c442007-06-18 14:50:20 -0500747 sdp->sd_log_commited_databuf = 0;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400748 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000750 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 if (!list_empty(&ai->ai_ail1_list)) {
752 list_add(&ai->ai_list, &sdp->sd_ail1_list);
753 ai = NULL;
754 }
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000755 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 gfs2_log_unlock(sdp);
Steven Whitehouse63997772009-06-12 08:49:20 +0100757 trace_gfs2_log_flush(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500758 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759
760 kfree(ai);
761}
762
763static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
764{
Robert Peterson2332c442007-06-18 14:50:20 -0500765 unsigned int reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000766 unsigned int unused;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767
768 gfs2_log_lock(sdp);
769
770 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500771 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
772 tr->tr_num_databuf_rm;
773 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
774 (((int)sdp->sd_log_commited_databuf) >= 0));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000775 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500776 reserved = calc_reserved(sdp);
Roel Kluin62be1f72008-04-17 17:25:37 +0200777 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000778 unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000779 atomic_add(unused, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100780 trace_gfs2_log_blocks(sdp, unused);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000781 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
Robert Peterson2332c442007-06-18 14:50:20 -0500782 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 sdp->sd_log_blks_reserved = reserved;
784
785 gfs2_log_unlock(sdp);
786}
787
Bob Petersond0109bf2008-01-28 11:20:10 -0600788static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
789{
790 struct list_head *head = &tr->tr_list_buf;
791 struct gfs2_bufdata *bd;
792
793 gfs2_log_lock(sdp);
794 while (!list_empty(head)) {
795 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
796 list_del_init(&bd->bd_list_tr);
797 tr->tr_num_buf--;
798 }
799 gfs2_log_unlock(sdp);
800 gfs2_assert_warn(sdp, !tr->tr_num_buf);
801}
802
David Teiglandb3b94fa2006-01-16 16:50:04 +0000803/**
804 * gfs2_log_commit - Commit a transaction to the log
805 * @sdp: the filesystem
806 * @tr: the transaction
807 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500808 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
809 * or the total number of used blocks (pinned blocks plus AIL blocks)
810 * is greater than thresh2.
811 *
812 * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
813 * journal size.
814 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 * Returns: errno
816 */
817
818void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
819{
820 log_refund(sdp, tr);
Bob Petersond0109bf2008-01-28 11:20:10 -0600821 buf_lo_incore_commit(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822
Steven Whitehouse484adff2006-03-29 09:12:12 -0500823 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500825 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
826 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
827 atomic_read(&sdp->sd_log_thresh2)))
828 wake_up(&sdp->sd_logd_waitq);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829}
830
831/**
832 * gfs2_log_shutdown - write a shutdown header into a journal
833 * @sdp: the filesystem
834 *
835 */
836
837void gfs2_log_shutdown(struct gfs2_sbd *sdp)
838{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500839 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
844 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
845 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
846 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
847
848 sdp->sd_log_flush_head = sdp->sd_log_head;
849 sdp->sd_log_flush_wrapped = 0;
850
Robert Peterson2332c442007-06-18 14:50:20 -0500851 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
852 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853
Steven Whitehousefd041f02007-11-08 14:55:03 +0000854 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400855 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
856 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857
858 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 sdp->sd_log_tail = sdp->sd_log_head;
860
Steven Whitehouse484adff2006-03-29 09:12:12 -0500861 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862}
863
Steven Whitehousea25311c2006-11-23 11:06:35 -0500864
865/**
866 * gfs2_meta_syncfs - sync all the buffers in a filesystem
867 * @sdp: the filesystem
868 *
869 */
870
871void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
872{
873 gfs2_log_flush(sdp, NULL);
874 for (;;) {
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500875 gfs2_ail1_start(sdp);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100876 if (gfs2_ail1_empty(sdp))
Steven Whitehousea25311c2006-11-23 11:06:35 -0500877 break;
878 msleep(10);
879 }
880}
881
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500882static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
883{
884 return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
885}
886
887static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
888{
889 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
890 return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
891}
Steven Whitehouseec69b182007-11-09 10:01:41 +0000892
893/**
894 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
895 * @sdp: Pointer to GFS2 superblock
896 *
897 * Also, periodically check to make sure that we're using the most recent
898 * journal index.
899 */
900
901int gfs2_logd(void *data)
902{
903 struct gfs2_sbd *sdp = data;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500904 unsigned long t = 1;
905 DEFINE_WAIT(wait);
906 unsigned preflush;
Steven Whitehouseec69b182007-11-09 10:01:41 +0000907
908 while (!kthread_should_stop()) {
Steven Whitehouseec69b182007-11-09 10:01:41 +0000909
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500910 preflush = atomic_read(&sdp->sd_log_pinned);
911 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100912 gfs2_ail1_empty(sdp);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000913 gfs2_log_flush(sdp, NULL);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000914 }
915
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500916 if (gfs2_ail_flush_reqd(sdp)) {
917 gfs2_ail1_start(sdp);
918 io_schedule();
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100919 gfs2_ail1_empty(sdp);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500920 gfs2_log_flush(sdp, NULL);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500921 }
922
923 wake_up(&sdp->sd_log_waitq);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000924 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
925 if (freezing(current))
926 refrigerator();
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500927
928 do {
929 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
Steven Whitehouse5f487492010-09-09 14:45:00 +0100930 TASK_INTERRUPTIBLE);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500931 if (!gfs2_ail_flush_reqd(sdp) &&
932 !gfs2_jrnl_flush_reqd(sdp) &&
933 !kthread_should_stop())
934 t = schedule_timeout(t);
935 } while(t && !gfs2_ail_flush_reqd(sdp) &&
936 !gfs2_jrnl_flush_reqd(sdp) &&
937 !kthread_should_stop());
938 finish_wait(&sdp->sd_logd_waitq, &wait);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000939 }
940
941 return 0;
942}
943