blob: d5959df6deb25e611de5be7167f50d9b70f1b5e6 [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 *
70 * The log lock _must_ be held when calling this function
71 *
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)
Harvey Harrison2d81afb2008-05-29 18:27:51 -070091__releases(&sdp->sd_log_lock)
92__acquires(&sdp->sd_log_lock)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040093{
94 struct gfs2_bufdata *bd, *s;
95 struct buffer_head *bh;
96 int retry;
97
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040098 do {
99 retry = 0;
100
101 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
102 bd_ail_st_list) {
103 bh = bd->bd_bh;
104
105 gfs2_assert(sdp, bd->bd_ail == ai);
106
107 if (!buffer_busy(bh)) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100108 if (!buffer_uptodate(bh))
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400109 gfs2_io_error_bh(sdp, bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400110 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
111 continue;
112 }
113
114 if (!buffer_dirty(bh))
115 continue;
116
117 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
118
Steven Whitehouse16615be2007-09-17 10:59:52 +0100119 get_bh(bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400120 gfs2_log_unlock(sdp);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100121 lock_buffer(bh);
122 if (test_clear_buffer_dirty(bh)) {
123 bh->b_end_io = end_buffer_write_sync;
Steven Whitehousec969f582009-04-07 14:13:01 +0100124 submit_bh(WRITE_SYNC_PLUG, bh);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100125 } else {
126 unlock_buffer(bh);
127 brelse(bh);
128 }
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400129 gfs2_log_lock(sdp);
130
131 retry = 1;
132 break;
133 }
134 } while (retry);
135}
136
137/**
138 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
139 * @sdp: the filesystem
140 * @ai: the AIL entry
141 *
142 */
143
144static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
145{
146 struct gfs2_bufdata *bd, *s;
147 struct buffer_head *bh;
148
149 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
150 bd_ail_st_list) {
151 bh = bd->bd_bh;
152
153 gfs2_assert(sdp, bd->bd_ail == ai);
154
155 if (buffer_busy(bh)) {
156 if (flags & DIO_ALL)
157 continue;
158 else
159 break;
160 }
161
162 if (!buffer_uptodate(bh))
163 gfs2_io_error_bh(sdp, bh);
164
165 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
166 }
167
168 return list_empty(&ai->ai_ail1_list);
169}
170
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500171static void gfs2_ail1_start(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172{
Bob Peterson693ddea2007-07-24 14:07:33 -0500173 struct list_head *head;
Steven Whitehousecd915492006-09-04 12:49:07 -0400174 u64 sync_gen;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500175 struct gfs2_ail *ai;
Steven Whitehouse74669412006-09-19 11:17:38 -0400176 int done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177
178 gfs2_log_lock(sdp);
Bob Peterson693ddea2007-07-24 14:07:33 -0500179 head = &sdp->sd_ail1_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180 if (list_empty(head)) {
181 gfs2_log_unlock(sdp);
182 return;
183 }
184 sync_gen = sdp->sd_ail_sync_gen++;
185
Steven Whitehouse74669412006-09-19 11:17:38 -0400186 while(!done) {
Steven Whitehouse74669412006-09-19 11:17:38 -0400187 done = 1;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500188 list_for_each_entry_reverse(ai, head, ai_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189 if (ai->ai_sync_gen >= sync_gen)
190 continue;
191 ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400192 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
193 done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194 break;
195 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196 }
197
198 gfs2_log_unlock(sdp);
199}
200
Steven Whitehouseec69b182007-11-09 10:01:41 +0000201static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202{
203 struct gfs2_ail *ai, *s;
204 int ret;
205
206 gfs2_log_lock(sdp);
207
208 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
209 if (gfs2_ail1_empty_one(sdp, ai, flags))
210 list_move(&ai->ai_list, &sdp->sd_ail2_list);
211 else if (!(flags & DIO_ALL))
212 break;
213 }
214
215 ret = list_empty(&sdp->sd_ail1_list);
216
217 gfs2_log_unlock(sdp);
218
219 return ret;
220}
221
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400222
223/**
224 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
225 * @sdp: the filesystem
226 * @ai: the AIL entry
227 *
228 */
229
230static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
231{
232 struct list_head *head = &ai->ai_ail2_list;
233 struct gfs2_bufdata *bd;
234
235 while (!list_empty(head)) {
236 bd = list_entry(head->prev, struct gfs2_bufdata,
237 bd_ail_st_list);
238 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100239 gfs2_remove_from_ail(bd);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400240 }
241}
242
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
244{
245 struct gfs2_ail *ai, *safe;
246 unsigned int old_tail = sdp->sd_log_tail;
247 int wrap = (new_tail < old_tail);
248 int a, b, rm;
249
250 gfs2_log_lock(sdp);
251
252 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
253 a = (old_tail <= ai->ai_first);
254 b = (ai->ai_first < new_tail);
255 rm = (wrap) ? (a || b) : (a && b);
256 if (!rm)
257 continue;
258
259 gfs2_ail2_empty_one(sdp, ai);
260 list_del(&ai->ai_list);
261 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
262 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
263 kfree(ai);
264 }
265
266 gfs2_log_unlock(sdp);
267}
268
269/**
270 * gfs2_log_reserve - Make a log reservation
271 * @sdp: The GFS2 superblock
272 * @blks: The number of blocks to reserve
273 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100274 * Note that we never give out the last few blocks of the journal. Thats
Robert Peterson2332c442007-06-18 14:50:20 -0500275 * due to the fact that there is a small number of header blocks
Steven Whitehouseb0041572006-11-23 10:51:34 -0500276 * associated with each log flush. The exact number can't be known until
277 * flush time, so we ensure that we have just enough free blocks at all
278 * times to avoid running out during a log flush.
279 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500280 * We no longer flush the log here, instead we wake up logd to do that
281 * for us. To avoid the thundering herd and to ensure that we deal fairly
282 * with queued waiters, we use an exclusive wait. This means that when we
283 * get woken with enough journal space to get our reservation, we need to
284 * wake the next waiter on the list.
285 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000286 * Returns: errno
287 */
288
289int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
290{
Steven Whitehouse89918642007-06-01 15:19:33 +0100291 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500292 unsigned wanted = blks + reserved_blks;
293 DEFINE_WAIT(wait);
294 int did_wait = 0;
295 unsigned int free_blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000296
297 if (gfs2_assert_warn(sdp, blks) ||
298 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
299 return -EINVAL;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500300retry:
301 free_blocks = atomic_read(&sdp->sd_log_blks_free);
302 if (unlikely(free_blocks <= wanted)) {
303 do {
304 prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
305 TASK_UNINTERRUPTIBLE);
306 wake_up(&sdp->sd_logd_waitq);
307 did_wait = 1;
308 if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
309 io_schedule();
310 free_blocks = atomic_read(&sdp->sd_log_blks_free);
311 } while(free_blocks <= wanted);
312 finish_wait(&sdp->sd_log_waitq, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000313 }
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500314 if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
315 free_blocks - blks) != free_blocks)
316 goto retry;
Steven Whitehouse63997772009-06-12 08:49:20 +0100317 trace_gfs2_log_blocks(sdp, -blks);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500318
319 /*
320 * If we waited, then so might others, wake them up _after_ we get
321 * our share of the log.
322 */
323 if (unlikely(did_wait))
324 wake_up(&sdp->sd_log_waitq);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500325
326 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327
328 return 0;
329}
330
Steven Whitehousecd915492006-09-04 12:49:07 -0400331static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000332{
Bob Petersonda6dd402007-12-11 18:49:21 -0600333 struct gfs2_journal_extent *je;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334
Bob Petersonda6dd402007-12-11 18:49:21 -0600335 list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
336 if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
Steven Whitehouseff91cc92007-12-14 14:04:34 +0000337 return je->dblock + lbn - je->lblock;
Bob Petersonda6dd402007-12-11 18:49:21 -0600338 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339
Bob Petersonda6dd402007-12-11 18:49:21 -0600340 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341}
342
343/**
344 * log_distance - Compute distance between two journal blocks
345 * @sdp: The GFS2 superblock
346 * @newer: The most recent journal block of the pair
347 * @older: The older journal block of the pair
348 *
349 * Compute the distance (in the journal direction) between two
350 * blocks in the journal
351 *
352 * Returns: the distance in blocks
353 */
354
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400355static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 unsigned int older)
357{
358 int dist;
359
360 dist = newer - older;
361 if (dist < 0)
362 dist += sdp->sd_jdesc->jd_blocks;
363
364 return dist;
365}
366
Robert Peterson2332c442007-06-18 14:50:20 -0500367/**
368 * calc_reserved - Calculate the number of blocks to reserve when
369 * refunding a transaction's unused buffers.
370 * @sdp: The GFS2 superblock
371 *
372 * This is complex. We need to reserve room for all our currently used
373 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
374 * all our journaled data buffers for journaled files (e.g. files in the
375 * meta_fs like rindex, or files for which chattr +j was done.)
376 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
377 * will count it as free space (sd_log_blks_free) and corruption will follow.
378 *
379 * We can have metadata bufs and jdata bufs in the same journal. So each
380 * type gets its own log header, for which we need to reserve a block.
381 * In fact, each type has the potential for needing more than one header
382 * in cases where we have more buffers than will fit on a journal page.
383 * Metadata journal entries take up half the space of journaled buffer entries.
384 * Thus, metadata entries have buf_limit (502) and journaled buffers have
385 * databuf_limit (251) before they cause a wrap around.
386 *
387 * Also, we need to reserve blocks for revoke journal entries and one for an
388 * overall header for the lot.
389 *
390 * Returns: the number of blocks reserved
391 */
392static unsigned int calc_reserved(struct gfs2_sbd *sdp)
393{
394 unsigned int reserved = 0;
395 unsigned int mbuf_limit, metabufhdrs_needed;
396 unsigned int dbuf_limit, databufhdrs_needed;
397 unsigned int revokes = 0;
398
399 mbuf_limit = buf_limit(sdp);
400 metabufhdrs_needed = (sdp->sd_log_commited_buf +
401 (mbuf_limit - 1)) / mbuf_limit;
402 dbuf_limit = databuf_limit(sdp);
403 databufhdrs_needed = (sdp->sd_log_commited_databuf +
404 (dbuf_limit - 1)) / dbuf_limit;
405
Benjamin Marzinski2e95e3f2010-03-10 18:10:19 -0600406 if (sdp->sd_log_commited_revoke > 0)
Robert Peterson2332c442007-06-18 14:50:20 -0500407 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
408 sizeof(u64));
409
410 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
411 sdp->sd_log_commited_databuf + databufhdrs_needed +
412 revokes;
413 /* One for the overall header */
414 if (reserved)
415 reserved++;
416 return reserved;
417}
418
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419static unsigned int current_tail(struct gfs2_sbd *sdp)
420{
421 struct gfs2_ail *ai;
422 unsigned int tail;
423
424 gfs2_log_lock(sdp);
425
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400426 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400428 } else {
429 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430 tail = ai->ai_first;
431 }
432
433 gfs2_log_unlock(sdp);
434
435 return tail;
436}
437
Steven Whitehouse16615be2007-09-17 10:59:52 +0100438void gfs2_log_incr_head(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439{
440 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehouse16615be2007-09-17 10:59:52 +0100441 BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442
443 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
444 sdp->sd_log_flush_head = 0;
445 sdp->sd_log_flush_wrapped = 1;
446 }
447}
448
449/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100450 * gfs2_log_write_endio - End of I/O for a log buffer
451 * @bh: The buffer head
452 * @uptodate: I/O Status
453 *
454 */
455
456static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
457{
458 struct gfs2_sbd *sdp = bh->b_private;
459 bh->b_private = NULL;
460
461 end_buffer_write_sync(bh, uptodate);
462 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
463 wake_up(&sdp->sd_log_flush_wait);
464}
465
466/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
468 * @sdp: The GFS2 superblock
469 *
470 * Returns: the buffer_head
471 */
472
473struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
474{
Steven Whitehousecd915492006-09-04 12:49:07 -0400475 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476 struct buffer_head *bh;
477
Steven Whitehouse16615be2007-09-17 10:59:52 +0100478 bh = sb_getblk(sdp->sd_vfs, blkno);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479 lock_buffer(bh);
480 memset(bh->b_data, 0, bh->b_size);
481 set_buffer_uptodate(bh);
482 clear_buffer_dirty(bh);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100483 gfs2_log_incr_head(sdp);
484 atomic_inc(&sdp->sd_log_in_flight);
485 bh->b_private = sdp;
486 bh->b_end_io = gfs2_log_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000487
488 return bh;
489}
490
491/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100492 * gfs2_fake_write_endio -
493 * @bh: The buffer head
494 * @uptodate: The I/O Status
495 *
496 */
497
498static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
499{
500 struct buffer_head *real_bh = bh->b_private;
Steven Whitehouse5a60c532007-09-26 09:39:31 +0100501 struct gfs2_bufdata *bd = real_bh->b_private;
502 struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100503
504 end_buffer_write_sync(bh, uptodate);
505 free_buffer_head(bh);
506 unlock_buffer(real_bh);
507 brelse(real_bh);
508 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
509 wake_up(&sdp->sd_log_flush_wait);
510}
511
512/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
514 * @sdp: the filesystem
515 * @data: the data the buffer_head should point to
516 *
517 * Returns: the log buffer descriptor
518 */
519
520struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
521 struct buffer_head *real)
522{
Steven Whitehousecd915492006-09-04 12:49:07 -0400523 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000524 struct buffer_head *bh;
525
Steven Whitehouse16615be2007-09-17 10:59:52 +0100526 bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 atomic_set(&bh->b_count, 1);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100528 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000529 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530 bh->b_blocknr = blkno;
531 bh->b_size = sdp->sd_sb.sb_bsize;
532 bh->b_bdev = sdp->sd_vfs->s_bdev;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100533 bh->b_private = real;
534 bh->b_end_io = gfs2_fake_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535
Steven Whitehouse16615be2007-09-17 10:59:52 +0100536 gfs2_log_incr_head(sdp);
537 atomic_inc(&sdp->sd_log_in_flight);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538
539 return bh;
540}
541
Robert Peterson2332c442007-06-18 14:50:20 -0500542static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543{
544 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
545
546 ail2_empty(sdp, new_tail);
547
Steven Whitehousefd041f02007-11-08 14:55:03 +0000548 atomic_add(dist, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100549 trace_gfs2_log_blocks(sdp, dist);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500550 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
551 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552
553 sdp->sd_log_tail = new_tail;
554}
555
556/**
557 * log_write_header - Get and initialize a journal header buffer
558 * @sdp: The GFS2 superblock
559 *
560 * Returns: the initialized log buffer descriptor
561 */
562
Steven Whitehousecd915492006-09-04 12:49:07 -0400563static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000564{
Steven Whitehousecd915492006-09-04 12:49:07 -0400565 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 struct buffer_head *bh;
567 struct gfs2_log_header *lh;
568 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400569 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571 bh = sb_getblk(sdp->sd_vfs, blkno);
572 lock_buffer(bh);
573 memset(bh->b_data, 0, bh->b_size);
574 set_buffer_uptodate(bh);
575 clear_buffer_dirty(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576
577 gfs2_ail1_empty(sdp, 0);
578 tail = current_tail(sdp);
579
580 lh = (struct gfs2_log_header *)bh->b_data;
581 memset(lh, 0, sizeof(struct gfs2_log_header));
582 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500583 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000584 lh->lh_header.__pad0 = cpu_to_be64(0);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500585 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000586 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400587 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
588 lh->lh_flags = cpu_to_be32(flags);
589 lh->lh_tail = cpu_to_be32(tail);
590 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
592 lh->lh_hash = cpu_to_be32(hash);
593
Steven Whitehouse254db572008-09-26 10:23:22 +0100594 bh->b_end_io = end_buffer_write_sync;
595 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
596 goto skip_barrier;
597 get_bh(bh);
Steven Whitehousec969f582009-04-07 14:13:01 +0100598 submit_bh(WRITE_SYNC | (1 << BIO_RW_BARRIER) | (1 << BIO_RW_META), bh);
Steven Whitehouse254db572008-09-26 10:23:22 +0100599 wait_on_buffer(bh);
600 if (buffer_eopnotsupp(bh)) {
601 clear_buffer_eopnotsupp(bh);
602 set_buffer_uptodate(bh);
603 set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
604 lock_buffer(bh);
605skip_barrier:
606 get_bh(bh);
607 submit_bh(WRITE_SYNC | (1 << BIO_RW_META), bh);
608 wait_on_buffer(bh);
609 }
610 if (!buffer_uptodate(bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000611 gfs2_io_error_bh(sdp, bh);
612 brelse(bh);
613
614 if (sdp->sd_log_tail != tail)
Robert Peterson2332c442007-06-18 14:50:20 -0500615 log_pull_tail(sdp, tail);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616 else
617 gfs2_assert_withdraw(sdp, !pull);
618
619 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100620 gfs2_log_incr_head(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621}
622
623static void log_flush_commit(struct gfs2_sbd *sdp)
624{
Steven Whitehouse16615be2007-09-17 10:59:52 +0100625 DEFINE_WAIT(wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000626
Steven Whitehouse16615be2007-09-17 10:59:52 +0100627 if (atomic_read(&sdp->sd_log_in_flight)) {
628 do {
629 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
630 TASK_UNINTERRUPTIBLE);
631 if (atomic_read(&sdp->sd_log_in_flight))
632 io_schedule();
633 } while(atomic_read(&sdp->sd_log_in_flight));
634 finish_wait(&sdp->sd_log_flush_wait, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635 }
636
Steven Whitehouse16615be2007-09-17 10:59:52 +0100637 log_write_header(sdp, 0, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638}
639
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100640static void gfs2_ordered_write(struct gfs2_sbd *sdp)
641{
642 struct gfs2_bufdata *bd;
643 struct buffer_head *bh;
644 LIST_HEAD(written);
645
646 gfs2_log_lock(sdp);
647 while (!list_empty(&sdp->sd_log_le_ordered)) {
648 bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
649 list_move(&bd->bd_le.le_list, &written);
650 bh = bd->bd_bh;
651 if (!buffer_dirty(bh))
652 continue;
653 get_bh(bh);
654 gfs2_log_unlock(sdp);
655 lock_buffer(bh);
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100656 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100657 bh->b_end_io = end_buffer_write_sync;
Steven Whitehousec969f582009-04-07 14:13:01 +0100658 submit_bh(WRITE_SYNC_PLUG, bh);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100659 } else {
660 unlock_buffer(bh);
661 brelse(bh);
662 }
663 gfs2_log_lock(sdp);
664 }
665 list_splice(&written, &sdp->sd_log_le_ordered);
666 gfs2_log_unlock(sdp);
667}
668
669static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
670{
671 struct gfs2_bufdata *bd;
672 struct buffer_head *bh;
673
674 gfs2_log_lock(sdp);
675 while (!list_empty(&sdp->sd_log_le_ordered)) {
676 bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
677 bh = bd->bd_bh;
678 if (buffer_locked(bh)) {
679 get_bh(bh);
680 gfs2_log_unlock(sdp);
681 wait_on_buffer(bh);
682 brelse(bh);
683 gfs2_log_lock(sdp);
684 continue;
685 }
686 list_del_init(&bd->bd_le.le_list);
687 }
688 gfs2_log_unlock(sdp);
689}
690
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400692 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 * @sdp: the filesystem
694 * @gl: The glock structure to flush. If NULL, flush the whole incore log
695 *
696 */
697
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000698void __gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699{
700 struct gfs2_ail *ai;
701
Steven Whitehouse484adff2006-03-29 09:12:12 -0500702 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000703
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000704 /* Log might have been flushed while we waited for the flush lock */
705 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
706 up_write(&sdp->sd_log_flush_lock);
707 return;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000708 }
Steven Whitehouse63997772009-06-12 08:49:20 +0100709 trace_gfs2_log_flush(sdp, 1);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000710
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400711 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
712 INIT_LIST_HEAD(&ai->ai_ail1_list);
713 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714
Steven Whitehouse16615be2007-09-17 10:59:52 +0100715 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
716 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
717 sdp->sd_log_commited_buf);
718 gfs2_assert_withdraw(sdp, 0);
719 }
720 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
721 printk(KERN_INFO "GFS2: log databuf %u %u\n",
722 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
723 gfs2_assert_withdraw(sdp, 0);
724 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725 gfs2_assert_withdraw(sdp,
726 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
727
David Teiglandb3b94fa2006-01-16 16:50:04 +0000728 sdp->sd_log_flush_head = sdp->sd_log_head;
729 sdp->sd_log_flush_wrapped = 0;
730 ai->ai_first = sdp->sd_log_flush_head;
731
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100732 gfs2_ordered_write(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 lops_before_commit(sdp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100734 gfs2_ordered_wait(sdp);
735
Steven Whitehouse16615be2007-09-17 10:59:52 +0100736 if (sdp->sd_log_head != sdp->sd_log_flush_head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 log_flush_commit(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500738 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
739 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000740 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
Steven Whitehouse63997772009-06-12 08:49:20 +0100741 trace_gfs2_log_blocks(sdp, -1);
Robert Peterson2332c442007-06-18 14:50:20 -0500742 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 log_write_header(sdp, 0, PULL);
Robert Peterson2332c442007-06-18 14:50:20 -0500744 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400746
747 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000748 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400749 sdp->sd_log_blks_reserved = 0;
750 sdp->sd_log_commited_buf = 0;
Robert Peterson2332c442007-06-18 14:50:20 -0500751 sdp->sd_log_commited_databuf = 0;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400752 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754 if (!list_empty(&ai->ai_ail1_list)) {
755 list_add(&ai->ai_list, &sdp->sd_ail1_list);
756 ai = NULL;
757 }
758 gfs2_log_unlock(sdp);
Steven Whitehouse63997772009-06-12 08:49:20 +0100759 trace_gfs2_log_flush(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500760 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761
762 kfree(ai);
763}
764
765static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
766{
Robert Peterson2332c442007-06-18 14:50:20 -0500767 unsigned int reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000768 unsigned int unused;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769
770 gfs2_log_lock(sdp);
771
772 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500773 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
774 tr->tr_num_databuf_rm;
775 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
776 (((int)sdp->sd_log_commited_databuf) >= 0));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500778 reserved = calc_reserved(sdp);
Roel Kluin62be1f72008-04-17 17:25:37 +0200779 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000780 unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000781 atomic_add(unused, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100782 trace_gfs2_log_blocks(sdp, unused);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000783 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
Robert Peterson2332c442007-06-18 14:50:20 -0500784 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 sdp->sd_log_blks_reserved = reserved;
786
787 gfs2_log_unlock(sdp);
788}
789
Bob Petersond0109bf2008-01-28 11:20:10 -0600790static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
791{
792 struct list_head *head = &tr->tr_list_buf;
793 struct gfs2_bufdata *bd;
794
795 gfs2_log_lock(sdp);
796 while (!list_empty(head)) {
797 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
798 list_del_init(&bd->bd_list_tr);
799 tr->tr_num_buf--;
800 }
801 gfs2_log_unlock(sdp);
802 gfs2_assert_warn(sdp, !tr->tr_num_buf);
803}
804
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805/**
806 * gfs2_log_commit - Commit a transaction to the log
807 * @sdp: the filesystem
808 * @tr: the transaction
809 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500810 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
811 * or the total number of used blocks (pinned blocks plus AIL blocks)
812 * is greater than thresh2.
813 *
814 * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
815 * journal size.
816 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817 * Returns: errno
818 */
819
820void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
821{
822 log_refund(sdp, tr);
Bob Petersond0109bf2008-01-28 11:20:10 -0600823 buf_lo_incore_commit(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824
Steven Whitehouse484adff2006-03-29 09:12:12 -0500825 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500827 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
828 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
829 atomic_read(&sdp->sd_log_thresh2)))
830 wake_up(&sdp->sd_logd_waitq);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831}
832
833/**
834 * gfs2_log_shutdown - write a shutdown header into a journal
835 * @sdp: the filesystem
836 *
837 */
838
839void gfs2_log_shutdown(struct gfs2_sbd *sdp)
840{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500841 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
846 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
847 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
848 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
849
850 sdp->sd_log_flush_head = sdp->sd_log_head;
851 sdp->sd_log_flush_wrapped = 0;
852
Robert Peterson2332c442007-06-18 14:50:20 -0500853 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
854 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855
Steven Whitehousefd041f02007-11-08 14:55:03 +0000856 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400857 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
858 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859
860 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 sdp->sd_log_tail = sdp->sd_log_head;
862
Steven Whitehouse484adff2006-03-29 09:12:12 -0500863 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864}
865
Steven Whitehousea25311c2006-11-23 11:06:35 -0500866
867/**
868 * gfs2_meta_syncfs - sync all the buffers in a filesystem
869 * @sdp: the filesystem
870 *
871 */
872
873void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
874{
875 gfs2_log_flush(sdp, NULL);
876 for (;;) {
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500877 gfs2_ail1_start(sdp);
Steven Whitehousea25311c2006-11-23 11:06:35 -0500878 if (gfs2_ail1_empty(sdp, DIO_ALL))
879 break;
880 msleep(10);
881 }
882}
883
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500884static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
885{
886 return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
887}
888
889static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
890{
891 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
892 return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
893}
Steven Whitehouseec69b182007-11-09 10:01:41 +0000894
895/**
896 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
897 * @sdp: Pointer to GFS2 superblock
898 *
899 * Also, periodically check to make sure that we're using the most recent
900 * journal index.
901 */
902
903int gfs2_logd(void *data)
904{
905 struct gfs2_sbd *sdp = data;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500906 unsigned long t = 1;
907 DEFINE_WAIT(wait);
908 unsigned preflush;
Steven Whitehouseec69b182007-11-09 10:01:41 +0000909
910 while (!kthread_should_stop()) {
Steven Whitehouseec69b182007-11-09 10:01:41 +0000911
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500912 preflush = atomic_read(&sdp->sd_log_pinned);
913 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
914 gfs2_ail1_empty(sdp, DIO_ALL);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000915 gfs2_log_flush(sdp, NULL);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500916 gfs2_ail1_empty(sdp, DIO_ALL);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000917 }
918
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500919 if (gfs2_ail_flush_reqd(sdp)) {
920 gfs2_ail1_start(sdp);
921 io_schedule();
922 gfs2_ail1_empty(sdp, 0);
923 gfs2_log_flush(sdp, NULL);
924 gfs2_ail1_empty(sdp, DIO_ALL);
925 }
926
927 wake_up(&sdp->sd_log_waitq);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000928 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
929 if (freezing(current))
930 refrigerator();
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500931
932 do {
933 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
934 TASK_UNINTERRUPTIBLE);
935 if (!gfs2_ail_flush_reqd(sdp) &&
936 !gfs2_jrnl_flush_reqd(sdp) &&
937 !kthread_should_stop())
938 t = schedule_timeout(t);
939 } while(t && !gfs2_ail_flush_reqd(sdp) &&
940 !gfs2_jrnl_flush_reqd(sdp) &&
941 !kthread_should_stop());
942 finish_wait(&sdp->sd_logd_waitq, &wait);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000943 }
944
945 return 0;
946}
947