blob: 3ebafa1efad0fe5f44346d26aebbf70ff6dbff61 [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000021
22#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050023#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "bmap.h"
25#include "glock.h"
26#include "log.h"
27#include "lops.h"
28#include "meta_io.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050029#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050030#include "dir.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010031#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032
33#define PULL 1
34
David Teiglandb3b94fa2006-01-16 16:50:04 +000035/**
36 * gfs2_struct2blk - compute stuff
37 * @sdp: the filesystem
38 * @nstruct: the number of structures
39 * @ssize: the size of the structures
40 *
41 * Compute the number of log descriptor blocks needed to hold a certain number
42 * of structures of a certain size.
43 *
44 * Returns: the number of blocks needed (minimum is always 1)
45 */
46
47unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
48 unsigned int ssize)
49{
50 unsigned int blks;
51 unsigned int first, second;
52
53 blks = 1;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -040054 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
David Teiglandb3b94fa2006-01-16 16:50:04 +000055
56 if (nstruct > first) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -050057 second = (sdp->sd_sb.sb_bsize -
58 sizeof(struct gfs2_meta_header)) / ssize;
Steven Whitehouse5c676f62006-02-27 17:23:27 -050059 blks += DIV_ROUND_UP(nstruct - first, second);
David Teiglandb3b94fa2006-01-16 16:50:04 +000060 }
61
62 return blks;
63}
64
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040065/**
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010066 * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
67 * @mapping: The associated mapping (maybe NULL)
68 * @bd: The gfs2_bufdata to remove
69 *
Steven Whitehousec618e872011-03-14 12:40:29 +000070 * The ail lock _must_ be held when calling this function
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010071 *
72 */
73
Steven Whitehousef91a0d32007-10-15 16:29:05 +010074void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010075{
76 bd->bd_ail = NULL;
Steven Whitehouse1ad38c42007-09-03 11:01:33 +010077 list_del_init(&bd->bd_ail_st_list);
78 list_del_init(&bd->bd_ail_gl_list);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010079 atomic_dec(&bd->bd_gl->gl_ail_count);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010080 brelse(bd->bd_bh);
81}
82
83/**
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040084 * gfs2_ail1_start_one - Start I/O on a part of the AIL
85 * @sdp: the filesystem
86 * @tr: the part of the AIL
87 *
88 */
89
90static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
Dave Chinnerd6a079e2011-03-11 11:52:25 +000091__releases(&sdp->sd_ail_lock)
92__acquires(&sdp->sd_ail_lock)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040093{
Steven Whitehouse5ac048b2011-03-30 16:25:51 +010094 struct gfs2_glock *gl = NULL;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040095 struct gfs2_bufdata *bd, *s;
96 struct buffer_head *bh;
97 int retry;
98
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040099 do {
100 retry = 0;
101
102 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
103 bd_ail_st_list) {
104 bh = bd->bd_bh;
105
106 gfs2_assert(sdp, bd->bd_ail == ai);
107
108 if (!buffer_busy(bh)) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100109 if (!buffer_uptodate(bh))
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400110 gfs2_io_error_bh(sdp, bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400111 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
112 continue;
113 }
114
115 if (!buffer_dirty(bh))
116 continue;
Steven Whitehouse5ac048b2011-03-30 16:25:51 +0100117 if (gl == bd->bd_gl)
118 continue;
119 gl = bd->bd_gl;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400120 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
121
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000122 spin_unlock(&sdp->sd_ail_lock);
Steven Whitehouse5ac048b2011-03-30 16:25:51 +0100123 filemap_fdatawrite(gfs2_glock2aspace(gl));
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000124 spin_lock(&sdp->sd_ail_lock);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400125
126 retry = 1;
127 break;
128 }
129 } while (retry);
130}
131
132/**
133 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
134 * @sdp: the filesystem
135 * @ai: the AIL entry
136 *
137 */
138
139static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
140{
141 struct gfs2_bufdata *bd, *s;
142 struct buffer_head *bh;
143
144 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
145 bd_ail_st_list) {
146 bh = bd->bd_bh;
147
148 gfs2_assert(sdp, bd->bd_ail == ai);
149
150 if (buffer_busy(bh)) {
151 if (flags & DIO_ALL)
152 continue;
153 else
154 break;
155 }
156
157 if (!buffer_uptodate(bh))
158 gfs2_io_error_bh(sdp, bh);
159
160 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
161 }
162
163 return list_empty(&ai->ai_ail1_list);
164}
165
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500166static void gfs2_ail1_start(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000167{
Bob Peterson693ddea2007-07-24 14:07:33 -0500168 struct list_head *head;
Steven Whitehousecd915492006-09-04 12:49:07 -0400169 u64 sync_gen;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500170 struct gfs2_ail *ai;
Steven Whitehouse74669412006-09-19 11:17:38 -0400171 int done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000173 spin_lock(&sdp->sd_ail_lock);
Bob Peterson693ddea2007-07-24 14:07:33 -0500174 head = &sdp->sd_ail1_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 if (list_empty(head)) {
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000176 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 return;
178 }
179 sync_gen = sdp->sd_ail_sync_gen++;
180
Steven Whitehouse74669412006-09-19 11:17:38 -0400181 while(!done) {
Steven Whitehouse74669412006-09-19 11:17:38 -0400182 done = 1;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500183 list_for_each_entry_reverse(ai, head, ai_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184 if (ai->ai_sync_gen >= sync_gen)
185 continue;
186 ai->ai_sync_gen = sync_gen;
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000187 gfs2_ail1_start_one(sdp, ai); /* This may drop ail lock */
Steven Whitehouse74669412006-09-19 11:17:38 -0400188 done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189 break;
190 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191 }
192
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000193 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194}
195
Steven Whitehouseec69b182007-11-09 10:01:41 +0000196static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197{
198 struct gfs2_ail *ai, *s;
199 int ret;
200
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000201 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202
203 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
204 if (gfs2_ail1_empty_one(sdp, ai, flags))
205 list_move(&ai->ai_list, &sdp->sd_ail2_list);
206 else if (!(flags & DIO_ALL))
207 break;
208 }
209
210 ret = list_empty(&sdp->sd_ail1_list);
211
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000212 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213
214 return ret;
215}
216
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400217
218/**
219 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
220 * @sdp: the filesystem
221 * @ai: the AIL entry
222 *
223 */
224
225static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
226{
227 struct list_head *head = &ai->ai_ail2_list;
228 struct gfs2_bufdata *bd;
229
230 while (!list_empty(head)) {
231 bd = list_entry(head->prev, struct gfs2_bufdata,
232 bd_ail_st_list);
233 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100234 gfs2_remove_from_ail(bd);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400235 }
236}
237
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
239{
240 struct gfs2_ail *ai, *safe;
241 unsigned int old_tail = sdp->sd_log_tail;
242 int wrap = (new_tail < old_tail);
243 int a, b, rm;
244
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000245 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246
247 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
248 a = (old_tail <= ai->ai_first);
249 b = (ai->ai_first < new_tail);
250 rm = (wrap) ? (a || b) : (a && b);
251 if (!rm)
252 continue;
253
254 gfs2_ail2_empty_one(sdp, ai);
255 list_del(&ai->ai_list);
256 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
257 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
258 kfree(ai);
259 }
260
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000261 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262}
263
264/**
265 * gfs2_log_reserve - Make a log reservation
266 * @sdp: The GFS2 superblock
267 * @blks: The number of blocks to reserve
268 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100269 * Note that we never give out the last few blocks of the journal. Thats
Robert Peterson2332c442007-06-18 14:50:20 -0500270 * due to the fact that there is a small number of header blocks
Steven Whitehouseb0041572006-11-23 10:51:34 -0500271 * associated with each log flush. The exact number can't be known until
272 * flush time, so we ensure that we have just enough free blocks at all
273 * times to avoid running out during a log flush.
274 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500275 * We no longer flush the log here, instead we wake up logd to do that
276 * for us. To avoid the thundering herd and to ensure that we deal fairly
277 * with queued waiters, we use an exclusive wait. This means that when we
278 * get woken with enough journal space to get our reservation, we need to
279 * wake the next waiter on the list.
280 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281 * Returns: errno
282 */
283
284int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
285{
Steven Whitehouse89918642007-06-01 15:19:33 +0100286 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500287 unsigned wanted = blks + reserved_blks;
288 DEFINE_WAIT(wait);
289 int did_wait = 0;
290 unsigned int free_blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000291
292 if (gfs2_assert_warn(sdp, blks) ||
293 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
294 return -EINVAL;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500295retry:
296 free_blocks = atomic_read(&sdp->sd_log_blks_free);
297 if (unlikely(free_blocks <= wanted)) {
298 do {
299 prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
300 TASK_UNINTERRUPTIBLE);
301 wake_up(&sdp->sd_logd_waitq);
302 did_wait = 1;
303 if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
304 io_schedule();
305 free_blocks = atomic_read(&sdp->sd_log_blks_free);
306 } while(free_blocks <= wanted);
307 finish_wait(&sdp->sd_log_waitq, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308 }
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500309 if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
310 free_blocks - blks) != free_blocks)
311 goto retry;
Steven Whitehouse63997772009-06-12 08:49:20 +0100312 trace_gfs2_log_blocks(sdp, -blks);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500313
314 /*
315 * If we waited, then so might others, wake them up _after_ we get
316 * our share of the log.
317 */
318 if (unlikely(did_wait))
319 wake_up(&sdp->sd_log_waitq);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500320
321 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322
323 return 0;
324}
325
Steven Whitehousecd915492006-09-04 12:49:07 -0400326static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327{
Bob Petersonda6dd402007-12-11 18:49:21 -0600328 struct gfs2_journal_extent *je;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329
Bob Petersonda6dd402007-12-11 18:49:21 -0600330 list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
331 if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
Steven Whitehouseff91cc92007-12-14 14:04:34 +0000332 return je->dblock + lbn - je->lblock;
Bob Petersonda6dd402007-12-11 18:49:21 -0600333 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334
Bob Petersonda6dd402007-12-11 18:49:21 -0600335 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336}
337
338/**
339 * log_distance - Compute distance between two journal blocks
340 * @sdp: The GFS2 superblock
341 * @newer: The most recent journal block of the pair
342 * @older: The older journal block of the pair
343 *
344 * Compute the distance (in the journal direction) between two
345 * blocks in the journal
346 *
347 * Returns: the distance in blocks
348 */
349
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400350static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351 unsigned int older)
352{
353 int dist;
354
355 dist = newer - older;
356 if (dist < 0)
357 dist += sdp->sd_jdesc->jd_blocks;
358
359 return dist;
360}
361
Robert Peterson2332c442007-06-18 14:50:20 -0500362/**
363 * calc_reserved - Calculate the number of blocks to reserve when
364 * refunding a transaction's unused buffers.
365 * @sdp: The GFS2 superblock
366 *
367 * This is complex. We need to reserve room for all our currently used
368 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
369 * all our journaled data buffers for journaled files (e.g. files in the
370 * meta_fs like rindex, or files for which chattr +j was done.)
371 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
372 * will count it as free space (sd_log_blks_free) and corruption will follow.
373 *
374 * We can have metadata bufs and jdata bufs in the same journal. So each
375 * type gets its own log header, for which we need to reserve a block.
376 * In fact, each type has the potential for needing more than one header
377 * in cases where we have more buffers than will fit on a journal page.
378 * Metadata journal entries take up half the space of journaled buffer entries.
379 * Thus, metadata entries have buf_limit (502) and journaled buffers have
380 * databuf_limit (251) before they cause a wrap around.
381 *
382 * Also, we need to reserve blocks for revoke journal entries and one for an
383 * overall header for the lot.
384 *
385 * Returns: the number of blocks reserved
386 */
387static unsigned int calc_reserved(struct gfs2_sbd *sdp)
388{
389 unsigned int reserved = 0;
390 unsigned int mbuf_limit, metabufhdrs_needed;
391 unsigned int dbuf_limit, databufhdrs_needed;
392 unsigned int revokes = 0;
393
394 mbuf_limit = buf_limit(sdp);
395 metabufhdrs_needed = (sdp->sd_log_commited_buf +
396 (mbuf_limit - 1)) / mbuf_limit;
397 dbuf_limit = databuf_limit(sdp);
398 databufhdrs_needed = (sdp->sd_log_commited_databuf +
399 (dbuf_limit - 1)) / dbuf_limit;
400
Benjamin Marzinski2e95e3f2010-03-10 18:10:19 -0600401 if (sdp->sd_log_commited_revoke > 0)
Robert Peterson2332c442007-06-18 14:50:20 -0500402 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
403 sizeof(u64));
404
405 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
406 sdp->sd_log_commited_databuf + databufhdrs_needed +
407 revokes;
408 /* One for the overall header */
409 if (reserved)
410 reserved++;
411 return reserved;
412}
413
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414static unsigned int current_tail(struct gfs2_sbd *sdp)
415{
416 struct gfs2_ail *ai;
417 unsigned int tail;
418
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000419 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400421 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400423 } else {
424 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 tail = ai->ai_first;
426 }
427
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000428 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000429
430 return tail;
431}
432
Steven Whitehouse16615be2007-09-17 10:59:52 +0100433void gfs2_log_incr_head(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000434{
435 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehouse16615be2007-09-17 10:59:52 +0100436 BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437
438 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
439 sdp->sd_log_flush_head = 0;
440 sdp->sd_log_flush_wrapped = 1;
441 }
442}
443
444/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100445 * gfs2_log_write_endio - End of I/O for a log buffer
446 * @bh: The buffer head
447 * @uptodate: I/O Status
448 *
449 */
450
451static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
452{
453 struct gfs2_sbd *sdp = bh->b_private;
454 bh->b_private = NULL;
455
456 end_buffer_write_sync(bh, uptodate);
457 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
458 wake_up(&sdp->sd_log_flush_wait);
459}
460
461/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
463 * @sdp: The GFS2 superblock
464 *
465 * Returns: the buffer_head
466 */
467
468struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
469{
Steven Whitehousecd915492006-09-04 12:49:07 -0400470 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471 struct buffer_head *bh;
472
Steven Whitehouse16615be2007-09-17 10:59:52 +0100473 bh = sb_getblk(sdp->sd_vfs, blkno);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474 lock_buffer(bh);
475 memset(bh->b_data, 0, bh->b_size);
476 set_buffer_uptodate(bh);
477 clear_buffer_dirty(bh);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100478 gfs2_log_incr_head(sdp);
479 atomic_inc(&sdp->sd_log_in_flight);
480 bh->b_private = sdp;
481 bh->b_end_io = gfs2_log_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482
483 return bh;
484}
485
486/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100487 * gfs2_fake_write_endio -
488 * @bh: The buffer head
489 * @uptodate: The I/O Status
490 *
491 */
492
493static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
494{
495 struct buffer_head *real_bh = bh->b_private;
Steven Whitehouse5a60c532007-09-26 09:39:31 +0100496 struct gfs2_bufdata *bd = real_bh->b_private;
497 struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100498
499 end_buffer_write_sync(bh, uptodate);
500 free_buffer_head(bh);
501 unlock_buffer(real_bh);
502 brelse(real_bh);
503 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
504 wake_up(&sdp->sd_log_flush_wait);
505}
506
507/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
509 * @sdp: the filesystem
510 * @data: the data the buffer_head should point to
511 *
512 * Returns: the log buffer descriptor
513 */
514
515struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
516 struct buffer_head *real)
517{
Steven Whitehousecd915492006-09-04 12:49:07 -0400518 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000519 struct buffer_head *bh;
520
Steven Whitehouse16615be2007-09-17 10:59:52 +0100521 bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522 atomic_set(&bh->b_count, 1);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100523 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000524 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525 bh->b_blocknr = blkno;
526 bh->b_size = sdp->sd_sb.sb_bsize;
527 bh->b_bdev = sdp->sd_vfs->s_bdev;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100528 bh->b_private = real;
529 bh->b_end_io = gfs2_fake_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530
Steven Whitehouse16615be2007-09-17 10:59:52 +0100531 gfs2_log_incr_head(sdp);
532 atomic_inc(&sdp->sd_log_in_flight);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533
534 return bh;
535}
536
Robert Peterson2332c442007-06-18 14:50:20 -0500537static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538{
539 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
540
541 ail2_empty(sdp, new_tail);
542
Steven Whitehousefd041f02007-11-08 14:55:03 +0000543 atomic_add(dist, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100544 trace_gfs2_log_blocks(sdp, dist);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500545 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
546 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547
548 sdp->sd_log_tail = new_tail;
549}
550
551/**
552 * log_write_header - Get and initialize a journal header buffer
553 * @sdp: The GFS2 superblock
554 *
555 * Returns: the initialized log buffer descriptor
556 */
557
Steven Whitehousecd915492006-09-04 12:49:07 -0400558static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559{
Steven Whitehousecd915492006-09-04 12:49:07 -0400560 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000561 struct buffer_head *bh;
562 struct gfs2_log_header *lh;
563 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400564 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 bh = sb_getblk(sdp->sd_vfs, blkno);
567 lock_buffer(bh);
568 memset(bh->b_data, 0, bh->b_size);
569 set_buffer_uptodate(bh);
570 clear_buffer_dirty(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571
572 gfs2_ail1_empty(sdp, 0);
573 tail = current_tail(sdp);
574
575 lh = (struct gfs2_log_header *)bh->b_data;
576 memset(lh, 0, sizeof(struct gfs2_log_header));
577 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500578 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000579 lh->lh_header.__pad0 = cpu_to_be64(0);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500580 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000581 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400582 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
583 lh->lh_flags = cpu_to_be32(flags);
584 lh->lh_tail = cpu_to_be32(tail);
585 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
587 lh->lh_hash = cpu_to_be32(hash);
588
Steven Whitehouse254db572008-09-26 10:23:22 +0100589 bh->b_end_io = end_buffer_write_sync;
Steven Whitehouse254db572008-09-26 10:23:22 +0100590 get_bh(bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400591 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200592 submit_bh(WRITE_SYNC | REQ_META, bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400593 else
594 submit_bh(WRITE_FLUSH_FUA | REQ_META, bh);
595 wait_on_buffer(bh);
596
Steven Whitehouse254db572008-09-26 10:23:22 +0100597 if (!buffer_uptodate(bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598 gfs2_io_error_bh(sdp, bh);
599 brelse(bh);
600
601 if (sdp->sd_log_tail != tail)
Robert Peterson2332c442007-06-18 14:50:20 -0500602 log_pull_tail(sdp, tail);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603 else
604 gfs2_assert_withdraw(sdp, !pull);
605
606 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100607 gfs2_log_incr_head(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608}
609
610static void log_flush_commit(struct gfs2_sbd *sdp)
611{
Steven Whitehouse16615be2007-09-17 10:59:52 +0100612 DEFINE_WAIT(wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613
Steven Whitehouse16615be2007-09-17 10:59:52 +0100614 if (atomic_read(&sdp->sd_log_in_flight)) {
615 do {
616 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
617 TASK_UNINTERRUPTIBLE);
618 if (atomic_read(&sdp->sd_log_in_flight))
619 io_schedule();
620 } while(atomic_read(&sdp->sd_log_in_flight));
621 finish_wait(&sdp->sd_log_flush_wait, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622 }
623
Steven Whitehouse16615be2007-09-17 10:59:52 +0100624 log_write_header(sdp, 0, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625}
626
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100627static void gfs2_ordered_write(struct gfs2_sbd *sdp)
628{
629 struct gfs2_bufdata *bd;
630 struct buffer_head *bh;
631 LIST_HEAD(written);
632
633 gfs2_log_lock(sdp);
634 while (!list_empty(&sdp->sd_log_le_ordered)) {
635 bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
636 list_move(&bd->bd_le.le_list, &written);
637 bh = bd->bd_bh;
638 if (!buffer_dirty(bh))
639 continue;
640 get_bh(bh);
641 gfs2_log_unlock(sdp);
642 lock_buffer(bh);
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100643 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100644 bh->b_end_io = end_buffer_write_sync;
Jens Axboe721a9602011-03-09 11:56:30 +0100645 submit_bh(WRITE_SYNC, bh);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100646 } else {
647 unlock_buffer(bh);
648 brelse(bh);
649 }
650 gfs2_log_lock(sdp);
651 }
652 list_splice(&written, &sdp->sd_log_le_ordered);
653 gfs2_log_unlock(sdp);
654}
655
656static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
657{
658 struct gfs2_bufdata *bd;
659 struct buffer_head *bh;
660
661 gfs2_log_lock(sdp);
662 while (!list_empty(&sdp->sd_log_le_ordered)) {
663 bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
664 bh = bd->bd_bh;
665 if (buffer_locked(bh)) {
666 get_bh(bh);
667 gfs2_log_unlock(sdp);
668 wait_on_buffer(bh);
669 brelse(bh);
670 gfs2_log_lock(sdp);
671 continue;
672 }
673 list_del_init(&bd->bd_le.le_list);
674 }
675 gfs2_log_unlock(sdp);
676}
677
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400679 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680 * @sdp: the filesystem
681 * @gl: The glock structure to flush. If NULL, flush the whole incore log
682 *
683 */
684
Bob Petersoned4878e2010-05-20 23:30:11 -0400685void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686{
687 struct gfs2_ail *ai;
688
Steven Whitehouse484adff2006-03-29 09:12:12 -0500689 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000690
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000691 /* Log might have been flushed while we waited for the flush lock */
692 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
693 up_write(&sdp->sd_log_flush_lock);
694 return;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000695 }
Steven Whitehouse63997772009-06-12 08:49:20 +0100696 trace_gfs2_log_flush(sdp, 1);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000697
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400698 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
699 INIT_LIST_HEAD(&ai->ai_ail1_list);
700 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000701
Steven Whitehouse16615be2007-09-17 10:59:52 +0100702 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
703 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
704 sdp->sd_log_commited_buf);
705 gfs2_assert_withdraw(sdp, 0);
706 }
707 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
708 printk(KERN_INFO "GFS2: log databuf %u %u\n",
709 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
710 gfs2_assert_withdraw(sdp, 0);
711 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 gfs2_assert_withdraw(sdp,
713 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
714
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 sdp->sd_log_flush_head = sdp->sd_log_head;
716 sdp->sd_log_flush_wrapped = 0;
717 ai->ai_first = sdp->sd_log_flush_head;
718
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100719 gfs2_ordered_write(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720 lops_before_commit(sdp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100721 gfs2_ordered_wait(sdp);
722
Steven Whitehouse16615be2007-09-17 10:59:52 +0100723 if (sdp->sd_log_head != sdp->sd_log_flush_head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 log_flush_commit(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500725 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
726 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000727 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
Steven Whitehouse63997772009-06-12 08:49:20 +0100728 trace_gfs2_log_blocks(sdp, -1);
Robert Peterson2332c442007-06-18 14:50:20 -0500729 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 log_write_header(sdp, 0, PULL);
Robert Peterson2332c442007-06-18 14:50:20 -0500731 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400733
734 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400736 sdp->sd_log_blks_reserved = 0;
737 sdp->sd_log_commited_buf = 0;
Robert Peterson2332c442007-06-18 14:50:20 -0500738 sdp->sd_log_commited_databuf = 0;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400739 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000741 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 if (!list_empty(&ai->ai_ail1_list)) {
743 list_add(&ai->ai_list, &sdp->sd_ail1_list);
744 ai = NULL;
745 }
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000746 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747 gfs2_log_unlock(sdp);
Steven Whitehouse63997772009-06-12 08:49:20 +0100748 trace_gfs2_log_flush(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500749 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750
751 kfree(ai);
752}
753
754static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
755{
Robert Peterson2332c442007-06-18 14:50:20 -0500756 unsigned int reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000757 unsigned int unused;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000758
759 gfs2_log_lock(sdp);
760
761 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500762 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
763 tr->tr_num_databuf_rm;
764 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
765 (((int)sdp->sd_log_commited_databuf) >= 0));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500767 reserved = calc_reserved(sdp);
Roel Kluin62be1f72008-04-17 17:25:37 +0200768 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000769 unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000770 atomic_add(unused, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100771 trace_gfs2_log_blocks(sdp, unused);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000772 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
Robert Peterson2332c442007-06-18 14:50:20 -0500773 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 sdp->sd_log_blks_reserved = reserved;
775
776 gfs2_log_unlock(sdp);
777}
778
Bob Petersond0109bf2008-01-28 11:20:10 -0600779static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
780{
781 struct list_head *head = &tr->tr_list_buf;
782 struct gfs2_bufdata *bd;
783
784 gfs2_log_lock(sdp);
785 while (!list_empty(head)) {
786 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
787 list_del_init(&bd->bd_list_tr);
788 tr->tr_num_buf--;
789 }
790 gfs2_log_unlock(sdp);
791 gfs2_assert_warn(sdp, !tr->tr_num_buf);
792}
793
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794/**
795 * gfs2_log_commit - Commit a transaction to the log
796 * @sdp: the filesystem
797 * @tr: the transaction
798 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500799 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
800 * or the total number of used blocks (pinned blocks plus AIL blocks)
801 * is greater than thresh2.
802 *
803 * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
804 * journal size.
805 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806 * Returns: errno
807 */
808
809void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
810{
811 log_refund(sdp, tr);
Bob Petersond0109bf2008-01-28 11:20:10 -0600812 buf_lo_incore_commit(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813
Steven Whitehouse484adff2006-03-29 09:12:12 -0500814 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500816 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
817 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
818 atomic_read(&sdp->sd_log_thresh2)))
819 wake_up(&sdp->sd_logd_waitq);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820}
821
822/**
823 * gfs2_log_shutdown - write a shutdown header into a journal
824 * @sdp: the filesystem
825 *
826 */
827
828void gfs2_log_shutdown(struct gfs2_sbd *sdp)
829{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500830 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
835 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
836 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
837 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
838
839 sdp->sd_log_flush_head = sdp->sd_log_head;
840 sdp->sd_log_flush_wrapped = 0;
841
Robert Peterson2332c442007-06-18 14:50:20 -0500842 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
843 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844
Steven Whitehousefd041f02007-11-08 14:55:03 +0000845 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400846 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
847 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848
849 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 sdp->sd_log_tail = sdp->sd_log_head;
851
Steven Whitehouse484adff2006-03-29 09:12:12 -0500852 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853}
854
Steven Whitehousea25311c2006-11-23 11:06:35 -0500855
856/**
857 * gfs2_meta_syncfs - sync all the buffers in a filesystem
858 * @sdp: the filesystem
859 *
860 */
861
862void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
863{
864 gfs2_log_flush(sdp, NULL);
865 for (;;) {
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500866 gfs2_ail1_start(sdp);
Steven Whitehousea25311c2006-11-23 11:06:35 -0500867 if (gfs2_ail1_empty(sdp, DIO_ALL))
868 break;
869 msleep(10);
870 }
871}
872
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500873static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
874{
875 return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
876}
877
878static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
879{
880 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
881 return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
882}
Steven Whitehouseec69b182007-11-09 10:01:41 +0000883
884/**
885 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
886 * @sdp: Pointer to GFS2 superblock
887 *
888 * Also, periodically check to make sure that we're using the most recent
889 * journal index.
890 */
891
892int gfs2_logd(void *data)
893{
894 struct gfs2_sbd *sdp = data;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500895 unsigned long t = 1;
896 DEFINE_WAIT(wait);
897 unsigned preflush;
Steven Whitehouseec69b182007-11-09 10:01:41 +0000898
899 while (!kthread_should_stop()) {
Steven Whitehouseec69b182007-11-09 10:01:41 +0000900
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500901 preflush = atomic_read(&sdp->sd_log_pinned);
902 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
903 gfs2_ail1_empty(sdp, DIO_ALL);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000904 gfs2_log_flush(sdp, NULL);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500905 gfs2_ail1_empty(sdp, DIO_ALL);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000906 }
907
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500908 if (gfs2_ail_flush_reqd(sdp)) {
909 gfs2_ail1_start(sdp);
910 io_schedule();
911 gfs2_ail1_empty(sdp, 0);
912 gfs2_log_flush(sdp, NULL);
913 gfs2_ail1_empty(sdp, DIO_ALL);
914 }
915
916 wake_up(&sdp->sd_log_waitq);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000917 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
918 if (freezing(current))
919 refrigerator();
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500920
921 do {
922 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
Steven Whitehouse5f487492010-09-09 14:45:00 +0100923 TASK_INTERRUPTIBLE);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500924 if (!gfs2_ail_flush_reqd(sdp) &&
925 !gfs2_jrnl_flush_reqd(sdp) &&
926 !kthread_should_stop())
927 t = schedule_timeout(t);
928 } while(t && !gfs2_ail_flush_reqd(sdp) &&
929 !gfs2_jrnl_flush_reqd(sdp) &&
930 !kthread_should_stop());
931 finish_wait(&sdp->sd_logd_waitq, &wait);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000932 }
933
934 return 0;
935}
936