blob: cec26c00b50d8f53b683b8cf2edfcaf3c7aa3683 [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 Whitehouse4f1de012011-04-26 10:23:56 +010092static int 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 +0100103 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, bd_ail_st_list) {
104 bh = bd->bd_bh;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400105
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100106 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400107
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100108 if (!buffer_busy(bh)) {
109 if (!buffer_uptodate(bh))
110 gfs2_io_error_bh(sdp, bh);
111 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
112 continue;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400113 }
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100114
115 if (!buffer_dirty(bh))
116 continue;
117 if (gl == bd->bd_gl)
118 continue;
119 gl = bd->bd_gl;
120 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
121 mapping = bh->b_page->mapping;
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100122 if (!mapping)
123 continue;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100124 spin_unlock(&sdp->sd_ail_lock);
125 generic_writepages(mapping, wbc);
126 spin_lock(&sdp->sd_ail_lock);
127 if (wbc->nr_to_write <= 0)
128 break;
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100129 return 1;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100130 }
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100131
132 return 0;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100133}
134
135
136/**
137 * gfs2_ail1_flush - start writeback of some ail1 entries
138 * @sdp: The super block
139 * @wbc: The writeback control structure
140 *
141 * Writes back some ail1 entries, according to the limits in the
142 * writeback control structure
143 */
144
145void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
146{
147 struct list_head *head = &sdp->sd_ail1_list;
148 struct gfs2_ail *ai;
149
Steven Whitehousec83ae9c2011-04-18 14:18:38 +0100150 trace_gfs2_ail_flush(sdp, wbc, 1);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100151 spin_lock(&sdp->sd_ail_lock);
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100152restart:
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100153 list_for_each_entry_reverse(ai, head, ai_list) {
154 if (wbc->nr_to_write <= 0)
155 break;
Steven Whitehouse4f1de012011-04-26 10:23:56 +0100156 if (gfs2_ail1_start_one(sdp, wbc, ai))
157 goto restart;
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100158 }
159 spin_unlock(&sdp->sd_ail_lock);
Steven Whitehousec83ae9c2011-04-18 14:18:38 +0100160 trace_gfs2_ail_flush(sdp, wbc, 0);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100161}
162
163/**
164 * gfs2_ail1_start - start writeback of all ail1 entries
165 * @sdp: The superblock
166 */
167
168static void gfs2_ail1_start(struct gfs2_sbd *sdp)
169{
170 struct writeback_control wbc = {
171 .sync_mode = WB_SYNC_NONE,
172 .nr_to_write = LONG_MAX,
173 .range_start = 0,
174 .range_end = LLONG_MAX,
175 };
176
177 return gfs2_ail1_flush(sdp, &wbc);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400178}
179
180/**
181 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
182 * @sdp: the filesystem
183 * @ai: the AIL entry
184 *
185 */
186
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100187static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400188{
189 struct gfs2_bufdata *bd, *s;
190 struct buffer_head *bh;
191
192 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
193 bd_ail_st_list) {
194 bh = bd->bd_bh;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400195 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100196 if (buffer_busy(bh))
197 continue;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400198 if (!buffer_uptodate(bh))
199 gfs2_io_error_bh(sdp, bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400200 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
201 }
202
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400203}
204
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100205/**
206 * gfs2_ail1_empty - Try to empty the ail1 lists
207 * @sdp: The superblock
208 *
209 * Tries to empty the ail1 lists, starting with the oldest first
210 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100212static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213{
214 struct gfs2_ail *ai, *s;
215 int ret;
216
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000217 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000218 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100219 gfs2_ail1_empty_one(sdp, ai);
220 if (list_empty(&ai->ai_ail1_list))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000221 list_move(&ai->ai_list, &sdp->sd_ail2_list);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100222 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223 break;
224 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225 ret = list_empty(&sdp->sd_ail1_list);
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000226 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227
228 return ret;
229}
230
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400231
232/**
233 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
234 * @sdp: the filesystem
235 * @ai: the AIL entry
236 *
237 */
238
239static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
240{
241 struct list_head *head = &ai->ai_ail2_list;
242 struct gfs2_bufdata *bd;
243
244 while (!list_empty(head)) {
245 bd = list_entry(head->prev, struct gfs2_bufdata,
246 bd_ail_st_list);
247 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100248 gfs2_remove_from_ail(bd);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400249 }
250}
251
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
253{
254 struct gfs2_ail *ai, *safe;
255 unsigned int old_tail = sdp->sd_log_tail;
256 int wrap = (new_tail < old_tail);
257 int a, b, rm;
258
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000259 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260
261 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
262 a = (old_tail <= ai->ai_first);
263 b = (ai->ai_first < new_tail);
264 rm = (wrap) ? (a || b) : (a && b);
265 if (!rm)
266 continue;
267
268 gfs2_ail2_empty_one(sdp, ai);
269 list_del(&ai->ai_list);
270 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
271 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
272 kfree(ai);
273 }
274
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000275 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000276}
277
278/**
279 * gfs2_log_reserve - Make a log reservation
280 * @sdp: The GFS2 superblock
281 * @blks: The number of blocks to reserve
282 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100283 * Note that we never give out the last few blocks of the journal. Thats
Robert Peterson2332c442007-06-18 14:50:20 -0500284 * due to the fact that there is a small number of header blocks
Steven Whitehouseb0041572006-11-23 10:51:34 -0500285 * associated with each log flush. The exact number can't be known until
286 * flush time, so we ensure that we have just enough free blocks at all
287 * times to avoid running out during a log flush.
288 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500289 * We no longer flush the log here, instead we wake up logd to do that
290 * for us. To avoid the thundering herd and to ensure that we deal fairly
291 * with queued waiters, we use an exclusive wait. This means that when we
292 * get woken with enough journal space to get our reservation, we need to
293 * wake the next waiter on the list.
294 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295 * Returns: errno
296 */
297
298int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
299{
Steven Whitehouse89918642007-06-01 15:19:33 +0100300 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500301 unsigned wanted = blks + reserved_blks;
302 DEFINE_WAIT(wait);
303 int did_wait = 0;
304 unsigned int free_blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305
306 if (gfs2_assert_warn(sdp, blks) ||
307 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
308 return -EINVAL;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500309retry:
310 free_blocks = atomic_read(&sdp->sd_log_blks_free);
311 if (unlikely(free_blocks <= wanted)) {
312 do {
313 prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
314 TASK_UNINTERRUPTIBLE);
315 wake_up(&sdp->sd_logd_waitq);
316 did_wait = 1;
317 if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
318 io_schedule();
319 free_blocks = atomic_read(&sdp->sd_log_blks_free);
320 } while(free_blocks <= wanted);
321 finish_wait(&sdp->sd_log_waitq, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322 }
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500323 if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
324 free_blocks - blks) != free_blocks)
325 goto retry;
Steven Whitehouse63997772009-06-12 08:49:20 +0100326 trace_gfs2_log_blocks(sdp, -blks);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500327
328 /*
329 * If we waited, then so might others, wake them up _after_ we get
330 * our share of the log.
331 */
332 if (unlikely(did_wait))
333 wake_up(&sdp->sd_log_waitq);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500334
335 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336
337 return 0;
338}
339
Steven Whitehousecd915492006-09-04 12:49:07 -0400340static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341{
Bob Petersonda6dd402007-12-11 18:49:21 -0600342 struct gfs2_journal_extent *je;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343
Bob Petersonda6dd402007-12-11 18:49:21 -0600344 list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
345 if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
Steven Whitehouseff91cc92007-12-14 14:04:34 +0000346 return je->dblock + lbn - je->lblock;
Bob Petersonda6dd402007-12-11 18:49:21 -0600347 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348
Bob Petersonda6dd402007-12-11 18:49:21 -0600349 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350}
351
352/**
353 * log_distance - Compute distance between two journal blocks
354 * @sdp: The GFS2 superblock
355 * @newer: The most recent journal block of the pair
356 * @older: The older journal block of the pair
357 *
358 * Compute the distance (in the journal direction) between two
359 * blocks in the journal
360 *
361 * Returns: the distance in blocks
362 */
363
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400364static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365 unsigned int older)
366{
367 int dist;
368
369 dist = newer - older;
370 if (dist < 0)
371 dist += sdp->sd_jdesc->jd_blocks;
372
373 return dist;
374}
375
Robert Peterson2332c442007-06-18 14:50:20 -0500376/**
377 * calc_reserved - Calculate the number of blocks to reserve when
378 * refunding a transaction's unused buffers.
379 * @sdp: The GFS2 superblock
380 *
381 * This is complex. We need to reserve room for all our currently used
382 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
383 * all our journaled data buffers for journaled files (e.g. files in the
384 * meta_fs like rindex, or files for which chattr +j was done.)
385 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
386 * will count it as free space (sd_log_blks_free) and corruption will follow.
387 *
388 * We can have metadata bufs and jdata bufs in the same journal. So each
389 * type gets its own log header, for which we need to reserve a block.
390 * In fact, each type has the potential for needing more than one header
391 * in cases where we have more buffers than will fit on a journal page.
392 * Metadata journal entries take up half the space of journaled buffer entries.
393 * Thus, metadata entries have buf_limit (502) and journaled buffers have
394 * databuf_limit (251) before they cause a wrap around.
395 *
396 * Also, we need to reserve blocks for revoke journal entries and one for an
397 * overall header for the lot.
398 *
399 * Returns: the number of blocks reserved
400 */
401static unsigned int calc_reserved(struct gfs2_sbd *sdp)
402{
403 unsigned int reserved = 0;
404 unsigned int mbuf_limit, metabufhdrs_needed;
405 unsigned int dbuf_limit, databufhdrs_needed;
406 unsigned int revokes = 0;
407
408 mbuf_limit = buf_limit(sdp);
409 metabufhdrs_needed = (sdp->sd_log_commited_buf +
410 (mbuf_limit - 1)) / mbuf_limit;
411 dbuf_limit = databuf_limit(sdp);
412 databufhdrs_needed = (sdp->sd_log_commited_databuf +
413 (dbuf_limit - 1)) / dbuf_limit;
414
Benjamin Marzinski2e95e3f2010-03-10 18:10:19 -0600415 if (sdp->sd_log_commited_revoke > 0)
Robert Peterson2332c442007-06-18 14:50:20 -0500416 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
417 sizeof(u64));
418
419 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
420 sdp->sd_log_commited_databuf + databufhdrs_needed +
421 revokes;
422 /* One for the overall header */
423 if (reserved)
424 reserved++;
425 return reserved;
426}
427
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428static unsigned int current_tail(struct gfs2_sbd *sdp)
429{
430 struct gfs2_ail *ai;
431 unsigned int tail;
432
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000433 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000434
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400435 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400437 } else {
438 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439 tail = ai->ai_first;
440 }
441
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000442 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443
444 return tail;
445}
446
Steven Whitehouse16615be2007-09-17 10:59:52 +0100447void gfs2_log_incr_head(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448{
449 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehouse16615be2007-09-17 10:59:52 +0100450 BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451
452 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
453 sdp->sd_log_flush_head = 0;
454 sdp->sd_log_flush_wrapped = 1;
455 }
456}
457
458/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100459 * gfs2_log_write_endio - End of I/O for a log buffer
460 * @bh: The buffer head
461 * @uptodate: I/O Status
462 *
463 */
464
465static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
466{
467 struct gfs2_sbd *sdp = bh->b_private;
468 bh->b_private = NULL;
469
470 end_buffer_write_sync(bh, uptodate);
471 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
472 wake_up(&sdp->sd_log_flush_wait);
473}
474
475/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
477 * @sdp: The GFS2 superblock
478 *
479 * Returns: the buffer_head
480 */
481
482struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
483{
Steven Whitehousecd915492006-09-04 12:49:07 -0400484 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000485 struct buffer_head *bh;
486
Steven Whitehouse16615be2007-09-17 10:59:52 +0100487 bh = sb_getblk(sdp->sd_vfs, blkno);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488 lock_buffer(bh);
489 memset(bh->b_data, 0, bh->b_size);
490 set_buffer_uptodate(bh);
491 clear_buffer_dirty(bh);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100492 gfs2_log_incr_head(sdp);
493 atomic_inc(&sdp->sd_log_in_flight);
494 bh->b_private = sdp;
495 bh->b_end_io = gfs2_log_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000496
497 return bh;
498}
499
500/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100501 * gfs2_fake_write_endio -
502 * @bh: The buffer head
503 * @uptodate: The I/O Status
504 *
505 */
506
507static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
508{
509 struct buffer_head *real_bh = bh->b_private;
Steven Whitehouse5a60c532007-09-26 09:39:31 +0100510 struct gfs2_bufdata *bd = real_bh->b_private;
511 struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100512
513 end_buffer_write_sync(bh, uptodate);
514 free_buffer_head(bh);
515 unlock_buffer(real_bh);
516 brelse(real_bh);
517 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
518 wake_up(&sdp->sd_log_flush_wait);
519}
520
521/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
523 * @sdp: the filesystem
524 * @data: the data the buffer_head should point to
525 *
526 * Returns: the log buffer descriptor
527 */
528
529struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
530 struct buffer_head *real)
531{
Steven Whitehousecd915492006-09-04 12:49:07 -0400532 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533 struct buffer_head *bh;
534
Steven Whitehouse16615be2007-09-17 10:59:52 +0100535 bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 atomic_set(&bh->b_count, 1);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100537 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000538 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 bh->b_blocknr = blkno;
540 bh->b_size = sdp->sd_sb.sb_bsize;
541 bh->b_bdev = sdp->sd_vfs->s_bdev;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100542 bh->b_private = real;
543 bh->b_end_io = gfs2_fake_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544
Steven Whitehouse16615be2007-09-17 10:59:52 +0100545 gfs2_log_incr_head(sdp);
546 atomic_inc(&sdp->sd_log_in_flight);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547
548 return bh;
549}
550
Robert Peterson2332c442007-06-18 14:50:20 -0500551static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552{
553 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
554
555 ail2_empty(sdp, new_tail);
556
Steven Whitehousefd041f02007-11-08 14:55:03 +0000557 atomic_add(dist, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100558 trace_gfs2_log_blocks(sdp, dist);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500559 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
560 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000561
562 sdp->sd_log_tail = new_tail;
563}
564
565/**
566 * log_write_header - Get and initialize a journal header buffer
567 * @sdp: The GFS2 superblock
568 *
569 * Returns: the initialized log buffer descriptor
570 */
571
Steven Whitehousecd915492006-09-04 12:49:07 -0400572static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573{
Steven Whitehousecd915492006-09-04 12:49:07 -0400574 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 struct buffer_head *bh;
576 struct gfs2_log_header *lh;
577 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400578 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 bh = sb_getblk(sdp->sd_vfs, blkno);
581 lock_buffer(bh);
582 memset(bh->b_data, 0, bh->b_size);
583 set_buffer_uptodate(bh);
584 clear_buffer_dirty(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100586 gfs2_ail1_empty(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 tail = current_tail(sdp);
588
589 lh = (struct gfs2_log_header *)bh->b_data;
590 memset(lh, 0, sizeof(struct gfs2_log_header));
591 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500592 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000593 lh->lh_header.__pad0 = cpu_to_be64(0);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500594 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehouse0ab7d132009-11-06 16:20:51 +0000595 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400596 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
597 lh->lh_flags = cpu_to_be32(flags);
598 lh->lh_tail = cpu_to_be32(tail);
599 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
601 lh->lh_hash = cpu_to_be32(hash);
602
Steven Whitehouse254db572008-09-26 10:23:22 +0100603 bh->b_end_io = end_buffer_write_sync;
Steven Whitehouse254db572008-09-26 10:23:22 +0100604 get_bh(bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400605 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200606 submit_bh(WRITE_SYNC | REQ_META, bh);
Christoph Hellwigf1e4d512010-08-18 05:29:13 -0400607 else
608 submit_bh(WRITE_FLUSH_FUA | REQ_META, bh);
609 wait_on_buffer(bh);
610
Steven Whitehouse254db572008-09-26 10:23:22 +0100611 if (!buffer_uptodate(bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 gfs2_io_error_bh(sdp, bh);
613 brelse(bh);
614
615 if (sdp->sd_log_tail != tail)
Robert Peterson2332c442007-06-18 14:50:20 -0500616 log_pull_tail(sdp, tail);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617 else
618 gfs2_assert_withdraw(sdp, !pull);
619
620 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100621 gfs2_log_incr_head(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622}
623
624static void log_flush_commit(struct gfs2_sbd *sdp)
625{
Steven Whitehouse16615be2007-09-17 10:59:52 +0100626 DEFINE_WAIT(wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000627
Steven Whitehouse16615be2007-09-17 10:59:52 +0100628 if (atomic_read(&sdp->sd_log_in_flight)) {
629 do {
630 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
631 TASK_UNINTERRUPTIBLE);
632 if (atomic_read(&sdp->sd_log_in_flight))
633 io_schedule();
634 } while(atomic_read(&sdp->sd_log_in_flight));
635 finish_wait(&sdp->sd_log_flush_wait, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636 }
637
Steven Whitehouse16615be2007-09-17 10:59:52 +0100638 log_write_header(sdp, 0, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639}
640
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100641static void gfs2_ordered_write(struct gfs2_sbd *sdp)
642{
643 struct gfs2_bufdata *bd;
644 struct buffer_head *bh;
645 LIST_HEAD(written);
646
647 gfs2_log_lock(sdp);
648 while (!list_empty(&sdp->sd_log_le_ordered)) {
649 bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
650 list_move(&bd->bd_le.le_list, &written);
651 bh = bd->bd_bh;
652 if (!buffer_dirty(bh))
653 continue;
654 get_bh(bh);
655 gfs2_log_unlock(sdp);
656 lock_buffer(bh);
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100657 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100658 bh->b_end_io = end_buffer_write_sync;
Jens Axboe721a9602011-03-09 11:56:30 +0100659 submit_bh(WRITE_SYNC, bh);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100660 } else {
661 unlock_buffer(bh);
662 brelse(bh);
663 }
664 gfs2_log_lock(sdp);
665 }
666 list_splice(&written, &sdp->sd_log_le_ordered);
667 gfs2_log_unlock(sdp);
668}
669
670static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
671{
672 struct gfs2_bufdata *bd;
673 struct buffer_head *bh;
674
675 gfs2_log_lock(sdp);
676 while (!list_empty(&sdp->sd_log_le_ordered)) {
677 bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
678 bh = bd->bd_bh;
679 if (buffer_locked(bh)) {
680 get_bh(bh);
681 gfs2_log_unlock(sdp);
682 wait_on_buffer(bh);
683 brelse(bh);
684 gfs2_log_lock(sdp);
685 continue;
686 }
687 list_del_init(&bd->bd_le.le_list);
688 }
689 gfs2_log_unlock(sdp);
690}
691
David Teiglandb3b94fa2006-01-16 16:50:04 +0000692/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400693 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 * @sdp: the filesystem
695 * @gl: The glock structure to flush. If NULL, flush the whole incore log
696 *
697 */
698
Bob Petersoned4878e2010-05-20 23:30:11 -0400699void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700{
701 struct gfs2_ail *ai;
702
Steven Whitehouse484adff2006-03-29 09:12:12 -0500703 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000704
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000705 /* Log might have been flushed while we waited for the flush lock */
706 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
707 up_write(&sdp->sd_log_flush_lock);
708 return;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000709 }
Steven Whitehouse63997772009-06-12 08:49:20 +0100710 trace_gfs2_log_flush(sdp, 1);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000711
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400712 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
713 INIT_LIST_HEAD(&ai->ai_ail1_list);
714 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715
Steven Whitehouse16615be2007-09-17 10:59:52 +0100716 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
717 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
718 sdp->sd_log_commited_buf);
719 gfs2_assert_withdraw(sdp, 0);
720 }
721 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
722 printk(KERN_INFO "GFS2: log databuf %u %u\n",
723 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
724 gfs2_assert_withdraw(sdp, 0);
725 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 gfs2_assert_withdraw(sdp,
727 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
728
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 sdp->sd_log_flush_head = sdp->sd_log_head;
730 sdp->sd_log_flush_wrapped = 0;
731 ai->ai_first = sdp->sd_log_flush_head;
732
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100733 gfs2_ordered_write(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 lops_before_commit(sdp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100735 gfs2_ordered_wait(sdp);
736
Steven Whitehouse16615be2007-09-17 10:59:52 +0100737 if (sdp->sd_log_head != sdp->sd_log_flush_head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 log_flush_commit(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500739 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
740 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000741 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
Steven Whitehouse63997772009-06-12 08:49:20 +0100742 trace_gfs2_log_blocks(sdp, -1);
Robert Peterson2332c442007-06-18 14:50:20 -0500743 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 log_write_header(sdp, 0, PULL);
Robert Peterson2332c442007-06-18 14:50:20 -0500745 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400747
748 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400750 sdp->sd_log_blks_reserved = 0;
751 sdp->sd_log_commited_buf = 0;
Robert Peterson2332c442007-06-18 14:50:20 -0500752 sdp->sd_log_commited_databuf = 0;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400753 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000755 spin_lock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 if (!list_empty(&ai->ai_ail1_list)) {
757 list_add(&ai->ai_list, &sdp->sd_ail1_list);
758 ai = NULL;
759 }
Dave Chinnerd6a079e2011-03-11 11:52:25 +0000760 spin_unlock(&sdp->sd_ail_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761 gfs2_log_unlock(sdp);
Steven Whitehouse63997772009-06-12 08:49:20 +0100762 trace_gfs2_log_flush(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500763 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764
765 kfree(ai);
766}
767
768static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
769{
Robert Peterson2332c442007-06-18 14:50:20 -0500770 unsigned int reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000771 unsigned int unused;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772
773 gfs2_log_lock(sdp);
774
775 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500776 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
777 tr->tr_num_databuf_rm;
778 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
779 (((int)sdp->sd_log_commited_databuf) >= 0));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500781 reserved = calc_reserved(sdp);
Roel Kluin62be1f72008-04-17 17:25:37 +0200782 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000783 unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
Steven Whitehouseac39aad2008-01-10 14:49:43 +0000784 atomic_add(unused, &sdp->sd_log_blks_free);
Steven Whitehouse63997772009-06-12 08:49:20 +0100785 trace_gfs2_log_blocks(sdp, unused);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000786 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
Robert Peterson2332c442007-06-18 14:50:20 -0500787 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 sdp->sd_log_blks_reserved = reserved;
789
790 gfs2_log_unlock(sdp);
791}
792
Bob Petersond0109bf2008-01-28 11:20:10 -0600793static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
794{
795 struct list_head *head = &tr->tr_list_buf;
796 struct gfs2_bufdata *bd;
797
798 gfs2_log_lock(sdp);
799 while (!list_empty(head)) {
800 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
801 list_del_init(&bd->bd_list_tr);
802 tr->tr_num_buf--;
803 }
804 gfs2_log_unlock(sdp);
805 gfs2_assert_warn(sdp, !tr->tr_num_buf);
806}
807
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808/**
809 * gfs2_log_commit - Commit a transaction to the log
810 * @sdp: the filesystem
811 * @tr: the transaction
812 *
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500813 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
814 * or the total number of used blocks (pinned blocks plus AIL blocks)
815 * is greater than thresh2.
816 *
817 * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
818 * journal size.
819 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820 * Returns: errno
821 */
822
823void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
824{
825 log_refund(sdp, tr);
Bob Petersond0109bf2008-01-28 11:20:10 -0600826 buf_lo_incore_commit(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827
Steven Whitehouse484adff2006-03-29 09:12:12 -0500828 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500830 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
831 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
832 atomic_read(&sdp->sd_log_thresh2)))
833 wake_up(&sdp->sd_logd_waitq);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834}
835
836/**
837 * gfs2_log_shutdown - write a shutdown header into a journal
838 * @sdp: the filesystem
839 *
840 */
841
842void gfs2_log_shutdown(struct gfs2_sbd *sdp)
843{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500844 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000847 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
849 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
850 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
851 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
852
853 sdp->sd_log_flush_head = sdp->sd_log_head;
854 sdp->sd_log_flush_wrapped = 0;
855
Robert Peterson2332c442007-06-18 14:50:20 -0500856 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
857 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858
Steven Whitehousefd041f02007-11-08 14:55:03 +0000859 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400860 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
861 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862
863 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 sdp->sd_log_tail = sdp->sd_log_head;
865
Steven Whitehouse484adff2006-03-29 09:12:12 -0500866 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867}
868
Steven Whitehousea25311c2006-11-23 11:06:35 -0500869
870/**
871 * gfs2_meta_syncfs - sync all the buffers in a filesystem
872 * @sdp: the filesystem
873 *
874 */
875
876void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
877{
878 gfs2_log_flush(sdp, NULL);
879 for (;;) {
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500880 gfs2_ail1_start(sdp);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100881 if (gfs2_ail1_empty(sdp))
Steven Whitehousea25311c2006-11-23 11:06:35 -0500882 break;
883 msleep(10);
884 }
885}
886
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500887static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
888{
889 return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
890}
891
892static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
893{
894 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
895 return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
896}
Steven Whitehouseec69b182007-11-09 10:01:41 +0000897
898/**
899 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
900 * @sdp: Pointer to GFS2 superblock
901 *
902 * Also, periodically check to make sure that we're using the most recent
903 * journal index.
904 */
905
906int gfs2_logd(void *data)
907{
908 struct gfs2_sbd *sdp = data;
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500909 unsigned long t = 1;
910 DEFINE_WAIT(wait);
911 unsigned preflush;
Steven Whitehouseec69b182007-11-09 10:01:41 +0000912
913 while (!kthread_should_stop()) {
Steven Whitehouseec69b182007-11-09 10:01:41 +0000914
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500915 preflush = atomic_read(&sdp->sd_log_pinned);
916 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100917 gfs2_ail1_empty(sdp);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000918 gfs2_log_flush(sdp, NULL);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000919 }
920
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500921 if (gfs2_ail_flush_reqd(sdp)) {
922 gfs2_ail1_start(sdp);
923 io_schedule();
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100924 gfs2_ail1_empty(sdp);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500925 gfs2_log_flush(sdp, NULL);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500926 }
927
928 wake_up(&sdp->sd_log_waitq);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000929 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
930 if (freezing(current))
931 refrigerator();
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500932
933 do {
934 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
Steven Whitehouse5f487492010-09-09 14:45:00 +0100935 TASK_INTERRUPTIBLE);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -0500936 if (!gfs2_ail_flush_reqd(sdp) &&
937 !gfs2_jrnl_flush_reqd(sdp) &&
938 !kthread_should_stop())
939 t = schedule_timeout(t);
940 } while(t && !gfs2_ail_flush_reqd(sdp) &&
941 !gfs2_jrnl_flush_reqd(sdp) &&
942 !kthread_should_stop());
943 finish_wait(&sdp->sd_logd_waitq, &wait);
Steven Whitehouseec69b182007-11-09 10:01:41 +0000944 }
945
946 return 0;
947}
948