blob: e88a684b2209cd8a3eed7454b3e1cf882184e6e8 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 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>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020017#include <linux/lm_interface.h>
Steven Whitehousea25311c2006-11-23 11:06:35 -050018#include <linux/delay.h>
Steven Whitehouseec69b182007-11-09 10:01:41 +000019#include <linux/kthread.h>
20#include <linux/freezer.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 Whitehouseec69b182007-11-09 10:01:41 +000031#include "super.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)
91{
92 struct gfs2_bufdata *bd, *s;
93 struct buffer_head *bh;
94 int retry;
95
96 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
97
98 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;
124 submit_bh(WRITE, bh);
125 } 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
Steven Whitehousea25311c2006-11-23 11:06:35 -0500171static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
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;
Steven Whitehouse74669412006-09-19 11:17:38 -0400175 struct list_head *first;
176 struct gfs2_ail *first_ai, *ai, *tmp;
177 int done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178
179 gfs2_log_lock(sdp);
Bob Peterson693ddea2007-07-24 14:07:33 -0500180 head = &sdp->sd_ail1_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 if (list_empty(head)) {
182 gfs2_log_unlock(sdp);
183 return;
184 }
185 sync_gen = sdp->sd_ail_sync_gen++;
186
187 first = head->prev;
188 first_ai = list_entry(first, struct gfs2_ail, ai_list);
189 first_ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400190 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191
192 if (flags & DIO_ALL)
193 first = NULL;
194
Steven Whitehouse74669412006-09-19 11:17:38 -0400195 while(!done) {
Steven Whitehouse484adff2006-03-29 09:12:12 -0500196 if (first && (head->prev != first ||
197 gfs2_ail1_empty_one(sdp, first_ai, 0)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198 break;
199
Steven Whitehouse74669412006-09-19 11:17:38 -0400200 done = 1;
201 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202 if (ai->ai_sync_gen >= sync_gen)
203 continue;
204 ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400205 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
206 done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207 break;
208 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 }
210
211 gfs2_log_unlock(sdp);
212}
213
Steven Whitehouseec69b182007-11-09 10:01:41 +0000214static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215{
216 struct gfs2_ail *ai, *s;
217 int ret;
218
219 gfs2_log_lock(sdp);
220
221 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
222 if (gfs2_ail1_empty_one(sdp, ai, flags))
223 list_move(&ai->ai_list, &sdp->sd_ail2_list);
224 else if (!(flags & DIO_ALL))
225 break;
226 }
227
228 ret = list_empty(&sdp->sd_ail1_list);
229
230 gfs2_log_unlock(sdp);
231
232 return ret;
233}
234
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400235
236/**
237 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
238 * @sdp: the filesystem
239 * @ai: the AIL entry
240 *
241 */
242
243static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
244{
245 struct list_head *head = &ai->ai_ail2_list;
246 struct gfs2_bufdata *bd;
247
248 while (!list_empty(head)) {
249 bd = list_entry(head->prev, struct gfs2_bufdata,
250 bd_ail_st_list);
251 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100252 gfs2_remove_from_ail(bd);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400253 }
254}
255
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
257{
258 struct gfs2_ail *ai, *safe;
259 unsigned int old_tail = sdp->sd_log_tail;
260 int wrap = (new_tail < old_tail);
261 int a, b, rm;
262
263 gfs2_log_lock(sdp);
264
265 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
266 a = (old_tail <= ai->ai_first);
267 b = (ai->ai_first < new_tail);
268 rm = (wrap) ? (a || b) : (a && b);
269 if (!rm)
270 continue;
271
272 gfs2_ail2_empty_one(sdp, ai);
273 list_del(&ai->ai_list);
274 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
275 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
276 kfree(ai);
277 }
278
279 gfs2_log_unlock(sdp);
280}
281
282/**
283 * gfs2_log_reserve - Make a log reservation
284 * @sdp: The GFS2 superblock
285 * @blks: The number of blocks to reserve
286 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100287 * Note that we never give out the last few blocks of the journal. Thats
Robert Peterson2332c442007-06-18 14:50:20 -0500288 * due to the fact that there is a small number of header blocks
Steven Whitehouseb0041572006-11-23 10:51:34 -0500289 * associated with each log flush. The exact number can't be known until
290 * flush time, so we ensure that we have just enough free blocks at all
291 * times to avoid running out during a log flush.
292 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000293 * Returns: errno
294 */
295
296int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
297{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298 unsigned int try = 0;
Steven Whitehouse89918642007-06-01 15:19:33 +0100299 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300
301 if (gfs2_assert_warn(sdp, blks) ||
302 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
303 return -EINVAL;
304
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500305 mutex_lock(&sdp->sd_log_reserve_mutex);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500306 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000307 while(atomic_read(&sdp->sd_log_blks_free) <= (blks + reserved_blks)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309 gfs2_ail1_empty(sdp, 0);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400310 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311
312 if (try++)
313 gfs2_ail1_start(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500314 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315 }
Steven Whitehousefd041f02007-11-08 14:55:03 +0000316 atomic_sub(blks, &sdp->sd_log_blks_free);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500317 gfs2_log_unlock(sdp);
318 mutex_unlock(&sdp->sd_log_reserve_mutex);
319
320 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321
322 return 0;
323}
324
325/**
326 * gfs2_log_release - Release a given number of log blocks
327 * @sdp: The GFS2 superblock
328 * @blks: The number of blocks
329 *
330 */
331
332void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
333{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334
335 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000336 atomic_add(blks, &sdp->sd_log_blks_free);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 gfs2_assert_withdraw(sdp,
Steven Whitehousefd041f02007-11-08 14:55:03 +0000338 atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339 gfs2_log_unlock(sdp);
Steven Whitehouseed386502006-04-07 16:28:07 -0400340 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341}
342
Steven Whitehousecd915492006-09-04 12:49:07 -0400343static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344{
Steven Whitehouse23591252006-10-13 17:25:45 -0400345 struct inode *inode = sdp->sd_jdesc->jd_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400347 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348
Steven Whitehouse23591252006-10-13 17:25:45 -0400349 bh_map.b_size = 1 << inode->i_blkbits;
350 error = gfs2_block_map(inode, lbn, 0, &bh_map);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400351 if (error || !bh_map.b_blocknr)
Ryusuke Konishiaed32552006-11-28 02:53:22 +0900352 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error,
353 (unsigned long long)bh_map.b_blocknr, lbn);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400354 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000355
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400356 return bh_map.b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357}
358
359/**
360 * log_distance - Compute distance between two journal blocks
361 * @sdp: The GFS2 superblock
362 * @newer: The most recent journal block of the pair
363 * @older: The older journal block of the pair
364 *
365 * Compute the distance (in the journal direction) between two
366 * blocks in the journal
367 *
368 * Returns: the distance in blocks
369 */
370
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400371static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372 unsigned int older)
373{
374 int dist;
375
376 dist = newer - older;
377 if (dist < 0)
378 dist += sdp->sd_jdesc->jd_blocks;
379
380 return dist;
381}
382
Robert Peterson2332c442007-06-18 14:50:20 -0500383/**
384 * calc_reserved - Calculate the number of blocks to reserve when
385 * refunding a transaction's unused buffers.
386 * @sdp: The GFS2 superblock
387 *
388 * This is complex. We need to reserve room for all our currently used
389 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
390 * all our journaled data buffers for journaled files (e.g. files in the
391 * meta_fs like rindex, or files for which chattr +j was done.)
392 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
393 * will count it as free space (sd_log_blks_free) and corruption will follow.
394 *
395 * We can have metadata bufs and jdata bufs in the same journal. So each
396 * type gets its own log header, for which we need to reserve a block.
397 * In fact, each type has the potential for needing more than one header
398 * in cases where we have more buffers than will fit on a journal page.
399 * Metadata journal entries take up half the space of journaled buffer entries.
400 * Thus, metadata entries have buf_limit (502) and journaled buffers have
401 * databuf_limit (251) before they cause a wrap around.
402 *
403 * Also, we need to reserve blocks for revoke journal entries and one for an
404 * overall header for the lot.
405 *
406 * Returns: the number of blocks reserved
407 */
408static unsigned int calc_reserved(struct gfs2_sbd *sdp)
409{
410 unsigned int reserved = 0;
411 unsigned int mbuf_limit, metabufhdrs_needed;
412 unsigned int dbuf_limit, databufhdrs_needed;
413 unsigned int revokes = 0;
414
415 mbuf_limit = buf_limit(sdp);
416 metabufhdrs_needed = (sdp->sd_log_commited_buf +
417 (mbuf_limit - 1)) / mbuf_limit;
418 dbuf_limit = databuf_limit(sdp);
419 databufhdrs_needed = (sdp->sd_log_commited_databuf +
420 (dbuf_limit - 1)) / dbuf_limit;
421
422 if (sdp->sd_log_commited_revoke)
423 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
424 sizeof(u64));
425
426 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
427 sdp->sd_log_commited_databuf + databufhdrs_needed +
428 revokes;
429 /* One for the overall header */
430 if (reserved)
431 reserved++;
432 return reserved;
433}
434
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435static unsigned int current_tail(struct gfs2_sbd *sdp)
436{
437 struct gfs2_ail *ai;
438 unsigned int tail;
439
440 gfs2_log_lock(sdp);
441
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400442 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400444 } else {
445 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446 tail = ai->ai_first;
447 }
448
449 gfs2_log_unlock(sdp);
450
451 return tail;
452}
453
Steven Whitehouse16615be2007-09-17 10:59:52 +0100454void gfs2_log_incr_head(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455{
456 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehouse16615be2007-09-17 10:59:52 +0100457 BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458
459 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
460 sdp->sd_log_flush_head = 0;
461 sdp->sd_log_flush_wrapped = 1;
462 }
463}
464
465/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100466 * gfs2_log_write_endio - End of I/O for a log buffer
467 * @bh: The buffer head
468 * @uptodate: I/O Status
469 *
470 */
471
472static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
473{
474 struct gfs2_sbd *sdp = bh->b_private;
475 bh->b_private = NULL;
476
477 end_buffer_write_sync(bh, uptodate);
478 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
479 wake_up(&sdp->sd_log_flush_wait);
480}
481
482/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
484 * @sdp: The GFS2 superblock
485 *
486 * Returns: the buffer_head
487 */
488
489struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
490{
Steven Whitehousecd915492006-09-04 12:49:07 -0400491 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 struct buffer_head *bh;
493
Steven Whitehouse16615be2007-09-17 10:59:52 +0100494 bh = sb_getblk(sdp->sd_vfs, blkno);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495 lock_buffer(bh);
496 memset(bh->b_data, 0, bh->b_size);
497 set_buffer_uptodate(bh);
498 clear_buffer_dirty(bh);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100499 gfs2_log_incr_head(sdp);
500 atomic_inc(&sdp->sd_log_in_flight);
501 bh->b_private = sdp;
502 bh->b_end_io = gfs2_log_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503
504 return bh;
505}
506
507/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100508 * gfs2_fake_write_endio -
509 * @bh: The buffer head
510 * @uptodate: The I/O Status
511 *
512 */
513
514static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
515{
516 struct buffer_head *real_bh = bh->b_private;
Steven Whitehouse5a60c532007-09-26 09:39:31 +0100517 struct gfs2_bufdata *bd = real_bh->b_private;
518 struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100519
520 end_buffer_write_sync(bh, uptodate);
521 free_buffer_head(bh);
522 unlock_buffer(real_bh);
523 brelse(real_bh);
524 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
525 wake_up(&sdp->sd_log_flush_wait);
526}
527
528/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
530 * @sdp: the filesystem
531 * @data: the data the buffer_head should point to
532 *
533 * Returns: the log buffer descriptor
534 */
535
536struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
537 struct buffer_head *real)
538{
Steven Whitehousecd915492006-09-04 12:49:07 -0400539 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 struct buffer_head *bh;
541
Steven Whitehouse16615be2007-09-17 10:59:52 +0100542 bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 atomic_set(&bh->b_count, 1);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100544 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000545 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546 bh->b_blocknr = blkno;
547 bh->b_size = sdp->sd_sb.sb_bsize;
548 bh->b_bdev = sdp->sd_vfs->s_bdev;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100549 bh->b_private = real;
550 bh->b_end_io = gfs2_fake_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551
Steven Whitehouse16615be2007-09-17 10:59:52 +0100552 gfs2_log_incr_head(sdp);
553 atomic_inc(&sdp->sd_log_in_flight);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554
555 return bh;
556}
557
Robert Peterson2332c442007-06-18 14:50:20 -0500558static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559{
560 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
561
562 ail2_empty(sdp, new_tail);
563
564 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000565 atomic_add(dist, &sdp->sd_log_blks_free);
566 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000567 gfs2_log_unlock(sdp);
568
569 sdp->sd_log_tail = new_tail;
570}
571
572/**
573 * log_write_header - Get and initialize a journal header buffer
574 * @sdp: The GFS2 superblock
575 *
576 * Returns: the initialized log buffer descriptor
577 */
578
Steven Whitehousecd915492006-09-04 12:49:07 -0400579static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580{
Steven Whitehousecd915492006-09-04 12:49:07 -0400581 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 struct buffer_head *bh;
583 struct gfs2_log_header *lh;
584 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400585 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 bh = sb_getblk(sdp->sd_vfs, blkno);
588 lock_buffer(bh);
589 memset(bh->b_data, 0, bh->b_size);
590 set_buffer_uptodate(bh);
591 clear_buffer_dirty(bh);
592 unlock_buffer(bh);
593
594 gfs2_ail1_empty(sdp, 0);
595 tail = current_tail(sdp);
596
597 lh = (struct gfs2_log_header *)bh->b_data;
598 memset(lh, 0, sizeof(struct gfs2_log_header));
599 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500600 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
601 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400602 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
603 lh->lh_flags = cpu_to_be32(flags);
604 lh->lh_tail = cpu_to_be32(tail);
605 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
607 lh->lh_hash = cpu_to_be32(hash);
608
609 set_buffer_dirty(bh);
610 if (sync_dirty_buffer(bh))
611 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;
658 submit_bh(WRITE, bh);
659 } 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 }
709
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400710 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
711 INIT_LIST_HEAD(&ai->ai_ail1_list);
712 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713
Steven Whitehouse16615be2007-09-17 10:59:52 +0100714 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
715 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
716 sdp->sd_log_commited_buf);
717 gfs2_assert_withdraw(sdp, 0);
718 }
719 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
720 printk(KERN_INFO "GFS2: log databuf %u %u\n",
721 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
722 gfs2_assert_withdraw(sdp, 0);
723 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 gfs2_assert_withdraw(sdp,
725 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
726
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727 sdp->sd_log_flush_head = sdp->sd_log_head;
728 sdp->sd_log_flush_wrapped = 0;
729 ai->ai_first = sdp->sd_log_flush_head;
730
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100731 gfs2_ordered_write(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 lops_before_commit(sdp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100733 gfs2_ordered_wait(sdp);
734
Steven Whitehouse16615be2007-09-17 10:59:52 +0100735 if (sdp->sd_log_head != sdp->sd_log_flush_head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 log_flush_commit(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500737 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
738 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000739 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
Robert Peterson2332c442007-06-18 14:50:20 -0500740 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741 log_write_header(sdp, 0, PULL);
Robert Peterson2332c442007-06-18 14:50:20 -0500742 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400744
745 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400747 sdp->sd_log_blks_reserved = 0;
748 sdp->sd_log_commited_buf = 0;
Robert Peterson2332c442007-06-18 14:50:20 -0500749 sdp->sd_log_commited_databuf = 0;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400750 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752 if (!list_empty(&ai->ai_ail1_list)) {
753 list_add(&ai->ai_list, &sdp->sd_ail1_list);
754 ai = NULL;
755 }
756 gfs2_log_unlock(sdp);
757
David Teiglandb3b94fa2006-01-16 16:50:04 +0000758 sdp->sd_vfs->s_dirt = 0;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500759 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760
761 kfree(ai);
762}
763
764static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
765{
Robert Peterson2332c442007-06-18 14:50:20 -0500766 unsigned int reserved;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 unsigned int old;
768
769 gfs2_log_lock(sdp);
770
771 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500772 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
773 tr->tr_num_databuf_rm;
774 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
775 (((int)sdp->sd_log_commited_databuf) >= 0));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
777 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
Robert Peterson2332c442007-06-18 14:50:20 -0500778 reserved = calc_reserved(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000779 old = atomic_read(&sdp->sd_log_blks_free);
780 atomic_add(tr->tr_reserved - (reserved - sdp->sd_log_blks_reserved),
781 &sdp->sd_log_blks_free);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782
Steven Whitehousefd041f02007-11-08 14:55:03 +0000783 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) >= old);
784 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
Robert Peterson2332c442007-06-18 14:50:20 -0500785 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786
787 sdp->sd_log_blks_reserved = reserved;
788
789 gfs2_log_unlock(sdp);
790}
791
792/**
793 * gfs2_log_commit - Commit a transaction to the log
794 * @sdp: the filesystem
795 * @tr: the transaction
796 *
797 * Returns: errno
798 */
799
800void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
801{
802 log_refund(sdp, tr);
803 lops_incore_commit(sdp, tr);
804
805 sdp->sd_vfs->s_dirt = 1;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500806 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808 gfs2_log_lock(sdp);
Steven Whitehouseb0041572006-11-23 10:51:34 -0500809 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
810 wake_up_process(sdp->sd_logd_process);
811 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812}
813
814/**
815 * gfs2_log_shutdown - write a shutdown header into a journal
816 * @sdp: the filesystem
817 *
818 */
819
820void gfs2_log_shutdown(struct gfs2_sbd *sdp)
821{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500822 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
827 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
828 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
829 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
830
831 sdp->sd_log_flush_head = sdp->sd_log_head;
832 sdp->sd_log_flush_wrapped = 0;
833
Robert Peterson2332c442007-06-18 14:50:20 -0500834 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
835 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836
Steven Whitehousefd041f02007-11-08 14:55:03 +0000837 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400838 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
839 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840
841 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 sdp->sd_log_tail = sdp->sd_log_head;
843
Steven Whitehouse484adff2006-03-29 09:12:12 -0500844 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845}
846
Steven Whitehousea25311c2006-11-23 11:06:35 -0500847
848/**
849 * gfs2_meta_syncfs - sync all the buffers in a filesystem
850 * @sdp: the filesystem
851 *
852 */
853
854void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
855{
856 gfs2_log_flush(sdp, NULL);
857 for (;;) {
858 gfs2_ail1_start(sdp, DIO_ALL);
859 if (gfs2_ail1_empty(sdp, DIO_ALL))
860 break;
861 msleep(10);
862 }
863}
864
Steven Whitehouseec69b182007-11-09 10:01:41 +0000865
866/**
867 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
868 * @sdp: Pointer to GFS2 superblock
869 *
870 * Also, periodically check to make sure that we're using the most recent
871 * journal index.
872 */
873
874int gfs2_logd(void *data)
875{
876 struct gfs2_sbd *sdp = data;
877 struct gfs2_holder ji_gh;
878 unsigned long t;
879 int need_flush;
880
881 while (!kthread_should_stop()) {
882 /* Advance the log tail */
883
884 t = sdp->sd_log_flush_time +
885 gfs2_tune_get(sdp, gt_log_flush_secs) * HZ;
886
887 gfs2_ail1_empty(sdp, DIO_ALL);
888 gfs2_log_lock(sdp);
889 need_flush = sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks);
890 gfs2_log_unlock(sdp);
891 if (need_flush || time_after_eq(jiffies, t)) {
892 gfs2_log_flush(sdp, NULL);
893 sdp->sd_log_flush_time = jiffies;
894 }
895
896 /* Check for latest journal index */
897
898 t = sdp->sd_jindex_refresh_time +
899 gfs2_tune_get(sdp, gt_jindex_refresh_secs) * HZ;
900
901 if (time_after_eq(jiffies, t)) {
902 if (!gfs2_jindex_hold(sdp, &ji_gh))
903 gfs2_glock_dq_uninit(&ji_gh);
904 sdp->sd_jindex_refresh_time = jiffies;
905 }
906
907 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
908 if (freezing(current))
909 refrigerator();
910 schedule_timeout_interruptible(t);
911 }
912
913 return 0;
914}
915