blob: f7c0608332fb6ce3c59ce04937e8c4202675d50a [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000019
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "bmap.h"
23#include "glock.h"
24#include "log.h"
25#include "lops.h"
26#include "meta_io.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050027#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050028#include "dir.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
30#define PULL 1
31
David Teiglandb3b94fa2006-01-16 16:50:04 +000032/**
33 * gfs2_struct2blk - compute stuff
34 * @sdp: the filesystem
35 * @nstruct: the number of structures
36 * @ssize: the size of the structures
37 *
38 * Compute the number of log descriptor blocks needed to hold a certain number
39 * of structures of a certain size.
40 *
41 * Returns: the number of blocks needed (minimum is always 1)
42 */
43
44unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
45 unsigned int ssize)
46{
47 unsigned int blks;
48 unsigned int first, second;
49
50 blks = 1;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -040051 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
David Teiglandb3b94fa2006-01-16 16:50:04 +000052
53 if (nstruct > first) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -050054 second = (sdp->sd_sb.sb_bsize -
55 sizeof(struct gfs2_meta_header)) / ssize;
Steven Whitehouse5c676f62006-02-27 17:23:27 -050056 blks += DIV_ROUND_UP(nstruct - first, second);
David Teiglandb3b94fa2006-01-16 16:50:04 +000057 }
58
59 return blks;
60}
61
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040062/**
63 * gfs2_ail1_start_one - Start I/O on a part of the AIL
64 * @sdp: the filesystem
65 * @tr: the part of the AIL
66 *
67 */
68
69static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
70{
71 struct gfs2_bufdata *bd, *s;
72 struct buffer_head *bh;
73 int retry;
74
75 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
76
77 do {
78 retry = 0;
79
80 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
81 bd_ail_st_list) {
82 bh = bd->bd_bh;
83
84 gfs2_assert(sdp, bd->bd_ail == ai);
85
86 if (!buffer_busy(bh)) {
87 if (!buffer_uptodate(bh)) {
88 gfs2_log_unlock(sdp);
89 gfs2_io_error_bh(sdp, bh);
90 gfs2_log_lock(sdp);
91 }
92 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
93 continue;
94 }
95
96 if (!buffer_dirty(bh))
97 continue;
98
99 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
100
101 gfs2_log_unlock(sdp);
102 wait_on_buffer(bh);
103 ll_rw_block(WRITE, 1, &bh);
104 gfs2_log_lock(sdp);
105
106 retry = 1;
107 break;
108 }
109 } while (retry);
110}
111
112/**
113 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
114 * @sdp: the filesystem
115 * @ai: the AIL entry
116 *
117 */
118
119static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
120{
121 struct gfs2_bufdata *bd, *s;
122 struct buffer_head *bh;
123
124 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
125 bd_ail_st_list) {
126 bh = bd->bd_bh;
127
128 gfs2_assert(sdp, bd->bd_ail == ai);
129
130 if (buffer_busy(bh)) {
131 if (flags & DIO_ALL)
132 continue;
133 else
134 break;
135 }
136
137 if (!buffer_uptodate(bh))
138 gfs2_io_error_bh(sdp, bh);
139
140 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
141 }
142
143 return list_empty(&ai->ai_ail1_list);
144}
145
Steven Whitehousea25311c2006-11-23 11:06:35 -0500146static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147{
Bob Peterson693ddea2007-07-24 14:07:33 -0500148 struct list_head *head;
Steven Whitehousecd915492006-09-04 12:49:07 -0400149 u64 sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400150 struct list_head *first;
151 struct gfs2_ail *first_ai, *ai, *tmp;
152 int done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153
154 gfs2_log_lock(sdp);
Bob Peterson693ddea2007-07-24 14:07:33 -0500155 head = &sdp->sd_ail1_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156 if (list_empty(head)) {
157 gfs2_log_unlock(sdp);
158 return;
159 }
160 sync_gen = sdp->sd_ail_sync_gen++;
161
162 first = head->prev;
163 first_ai = list_entry(first, struct gfs2_ail, ai_list);
164 first_ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400165 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166
167 if (flags & DIO_ALL)
168 first = NULL;
169
Steven Whitehouse74669412006-09-19 11:17:38 -0400170 while(!done) {
Steven Whitehouse484adff2006-03-29 09:12:12 -0500171 if (first && (head->prev != first ||
172 gfs2_ail1_empty_one(sdp, first_ai, 0)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173 break;
174
Steven Whitehouse74669412006-09-19 11:17:38 -0400175 done = 1;
176 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 if (ai->ai_sync_gen >= sync_gen)
178 continue;
179 ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400180 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
181 done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 break;
183 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184 }
185
186 gfs2_log_unlock(sdp);
187}
188
189int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
190{
191 struct gfs2_ail *ai, *s;
192 int ret;
193
194 gfs2_log_lock(sdp);
195
196 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
197 if (gfs2_ail1_empty_one(sdp, ai, flags))
198 list_move(&ai->ai_list, &sdp->sd_ail2_list);
199 else if (!(flags & DIO_ALL))
200 break;
201 }
202
203 ret = list_empty(&sdp->sd_ail1_list);
204
205 gfs2_log_unlock(sdp);
206
207 return ret;
208}
209
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400210
211/**
212 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
213 * @sdp: the filesystem
214 * @ai: the AIL entry
215 *
216 */
217
218static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
219{
220 struct list_head *head = &ai->ai_ail2_list;
221 struct gfs2_bufdata *bd;
222
223 while (!list_empty(head)) {
224 bd = list_entry(head->prev, struct gfs2_bufdata,
225 bd_ail_st_list);
226 gfs2_assert(sdp, bd->bd_ail == ai);
227 bd->bd_ail = NULL;
228 list_del(&bd->bd_ail_st_list);
229 list_del(&bd->bd_ail_gl_list);
230 atomic_dec(&bd->bd_gl->gl_ail_count);
Steven Whitehousea0a24742007-07-09 15:43:07 +0100231 brelse(bd->bd_bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400232 }
233}
234
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
236{
237 struct gfs2_ail *ai, *safe;
238 unsigned int old_tail = sdp->sd_log_tail;
239 int wrap = (new_tail < old_tail);
240 int a, b, rm;
241
242 gfs2_log_lock(sdp);
243
244 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
245 a = (old_tail <= ai->ai_first);
246 b = (ai->ai_first < new_tail);
247 rm = (wrap) ? (a || b) : (a && b);
248 if (!rm)
249 continue;
250
251 gfs2_ail2_empty_one(sdp, ai);
252 list_del(&ai->ai_list);
253 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
254 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
255 kfree(ai);
256 }
257
258 gfs2_log_unlock(sdp);
259}
260
261/**
262 * gfs2_log_reserve - Make a log reservation
263 * @sdp: The GFS2 superblock
264 * @blks: The number of blocks to reserve
265 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100266 * Note that we never give out the last few blocks of the journal. Thats
Robert Peterson2332c442007-06-18 14:50:20 -0500267 * due to the fact that there is a small number of header blocks
Steven Whitehouseb0041572006-11-23 10:51:34 -0500268 * associated with each log flush. The exact number can't be known until
269 * flush time, so we ensure that we have just enough free blocks at all
270 * times to avoid running out during a log flush.
271 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272 * Returns: errno
273 */
274
275int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
276{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277 unsigned int try = 0;
Steven Whitehouse89918642007-06-01 15:19:33 +0100278 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279
280 if (gfs2_assert_warn(sdp, blks) ||
281 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
282 return -EINVAL;
283
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500284 mutex_lock(&sdp->sd_log_reserve_mutex);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500285 gfs2_log_lock(sdp);
Steven Whitehouse89918642007-06-01 15:19:33 +0100286 while(sdp->sd_log_blks_free <= (blks + reserved_blks)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288 gfs2_ail1_empty(sdp, 0);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400289 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290
291 if (try++)
292 gfs2_ail1_start(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500293 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000294 }
Steven Whitehouse484adff2006-03-29 09:12:12 -0500295 sdp->sd_log_blks_free -= blks;
296 gfs2_log_unlock(sdp);
297 mutex_unlock(&sdp->sd_log_reserve_mutex);
298
299 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300
301 return 0;
302}
303
304/**
305 * gfs2_log_release - Release a given number of log blocks
306 * @sdp: The GFS2 superblock
307 * @blks: The number of blocks
308 *
309 */
310
311void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
312{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000313
314 gfs2_log_lock(sdp);
315 sdp->sd_log_blks_free += blks;
316 gfs2_assert_withdraw(sdp,
317 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
318 gfs2_log_unlock(sdp);
Steven Whitehouseed386502006-04-07 16:28:07 -0400319 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000320}
321
Steven Whitehousecd915492006-09-04 12:49:07 -0400322static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323{
Steven Whitehouse23591252006-10-13 17:25:45 -0400324 struct inode *inode = sdp->sd_jdesc->jd_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000325 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400326 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327
Steven Whitehouse23591252006-10-13 17:25:45 -0400328 bh_map.b_size = 1 << inode->i_blkbits;
329 error = gfs2_block_map(inode, lbn, 0, &bh_map);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400330 if (error || !bh_map.b_blocknr)
Ryusuke Konishiaed32552006-11-28 02:53:22 +0900331 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error,
332 (unsigned long long)bh_map.b_blocknr, lbn);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400333 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400335 return bh_map.b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336}
337
338/**
339 * log_distance - Compute distance between two journal blocks
340 * @sdp: The GFS2 superblock
341 * @newer: The most recent journal block of the pair
342 * @older: The older journal block of the pair
343 *
344 * Compute the distance (in the journal direction) between two
345 * blocks in the journal
346 *
347 * Returns: the distance in blocks
348 */
349
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400350static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351 unsigned int older)
352{
353 int dist;
354
355 dist = newer - older;
356 if (dist < 0)
357 dist += sdp->sd_jdesc->jd_blocks;
358
359 return dist;
360}
361
Robert Peterson2332c442007-06-18 14:50:20 -0500362/**
363 * calc_reserved - Calculate the number of blocks to reserve when
364 * refunding a transaction's unused buffers.
365 * @sdp: The GFS2 superblock
366 *
367 * This is complex. We need to reserve room for all our currently used
368 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
369 * all our journaled data buffers for journaled files (e.g. files in the
370 * meta_fs like rindex, or files for which chattr +j was done.)
371 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
372 * will count it as free space (sd_log_blks_free) and corruption will follow.
373 *
374 * We can have metadata bufs and jdata bufs in the same journal. So each
375 * type gets its own log header, for which we need to reserve a block.
376 * In fact, each type has the potential for needing more than one header
377 * in cases where we have more buffers than will fit on a journal page.
378 * Metadata journal entries take up half the space of journaled buffer entries.
379 * Thus, metadata entries have buf_limit (502) and journaled buffers have
380 * databuf_limit (251) before they cause a wrap around.
381 *
382 * Also, we need to reserve blocks for revoke journal entries and one for an
383 * overall header for the lot.
384 *
385 * Returns: the number of blocks reserved
386 */
387static unsigned int calc_reserved(struct gfs2_sbd *sdp)
388{
389 unsigned int reserved = 0;
390 unsigned int mbuf_limit, metabufhdrs_needed;
391 unsigned int dbuf_limit, databufhdrs_needed;
392 unsigned int revokes = 0;
393
394 mbuf_limit = buf_limit(sdp);
395 metabufhdrs_needed = (sdp->sd_log_commited_buf +
396 (mbuf_limit - 1)) / mbuf_limit;
397 dbuf_limit = databuf_limit(sdp);
398 databufhdrs_needed = (sdp->sd_log_commited_databuf +
399 (dbuf_limit - 1)) / dbuf_limit;
400
401 if (sdp->sd_log_commited_revoke)
402 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
403 sizeof(u64));
404
405 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
406 sdp->sd_log_commited_databuf + databufhdrs_needed +
407 revokes;
408 /* One for the overall header */
409 if (reserved)
410 reserved++;
411 return reserved;
412}
413
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414static unsigned int current_tail(struct gfs2_sbd *sdp)
415{
416 struct gfs2_ail *ai;
417 unsigned int tail;
418
419 gfs2_log_lock(sdp);
420
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400421 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400423 } else {
424 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 tail = ai->ai_first;
426 }
427
428 gfs2_log_unlock(sdp);
429
430 return tail;
431}
432
433static inline void log_incr_head(struct gfs2_sbd *sdp)
434{
435 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400436 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437
438 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
439 sdp->sd_log_flush_head = 0;
440 sdp->sd_log_flush_wrapped = 1;
441 }
442}
443
444/**
445 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
446 * @sdp: The GFS2 superblock
447 *
448 * Returns: the buffer_head
449 */
450
451struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
452{
Steven Whitehousecd915492006-09-04 12:49:07 -0400453 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454 struct gfs2_log_buf *lb;
455 struct buffer_head *bh;
456
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000457 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
459
460 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
461 lock_buffer(bh);
462 memset(bh->b_data, 0, bh->b_size);
463 set_buffer_uptodate(bh);
464 clear_buffer_dirty(bh);
465 unlock_buffer(bh);
466
467 log_incr_head(sdp);
468
469 return bh;
470}
471
472/**
473 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
474 * @sdp: the filesystem
475 * @data: the data the buffer_head should point to
476 *
477 * Returns: the log buffer descriptor
478 */
479
480struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
481 struct buffer_head *real)
482{
Steven Whitehousecd915492006-09-04 12:49:07 -0400483 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484 struct gfs2_log_buf *lb;
485 struct buffer_head *bh;
486
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000487 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
489 lb->lb_real = real;
490
491 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
492 atomic_set(&bh->b_count, 1);
493 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000494 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495 bh->b_blocknr = blkno;
496 bh->b_size = sdp->sd_sb.sb_bsize;
497 bh->b_bdev = sdp->sd_vfs->s_bdev;
498
499 log_incr_head(sdp);
500
501 return bh;
502}
503
Robert Peterson2332c442007-06-18 14:50:20 -0500504static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505{
506 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
507
508 ail2_empty(sdp, new_tail);
509
510 gfs2_log_lock(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500511 sdp->sd_log_blks_free += dist;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400512 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 gfs2_log_unlock(sdp);
514
515 sdp->sd_log_tail = new_tail;
516}
517
518/**
519 * log_write_header - Get and initialize a journal header buffer
520 * @sdp: The GFS2 superblock
521 *
522 * Returns: the initialized log buffer descriptor
523 */
524
Steven Whitehousecd915492006-09-04 12:49:07 -0400525static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526{
Steven Whitehousecd915492006-09-04 12:49:07 -0400527 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528 struct buffer_head *bh;
529 struct gfs2_log_header *lh;
530 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400531 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533 bh = sb_getblk(sdp->sd_vfs, blkno);
534 lock_buffer(bh);
535 memset(bh->b_data, 0, bh->b_size);
536 set_buffer_uptodate(bh);
537 clear_buffer_dirty(bh);
538 unlock_buffer(bh);
539
540 gfs2_ail1_empty(sdp, 0);
541 tail = current_tail(sdp);
542
543 lh = (struct gfs2_log_header *)bh->b_data;
544 memset(lh, 0, sizeof(struct gfs2_log_header));
545 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500546 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
547 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400548 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
549 lh->lh_flags = cpu_to_be32(flags);
550 lh->lh_tail = cpu_to_be32(tail);
551 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
553 lh->lh_hash = cpu_to_be32(hash);
554
555 set_buffer_dirty(bh);
556 if (sync_dirty_buffer(bh))
557 gfs2_io_error_bh(sdp, bh);
558 brelse(bh);
559
560 if (sdp->sd_log_tail != tail)
Robert Peterson2332c442007-06-18 14:50:20 -0500561 log_pull_tail(sdp, tail);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 else
563 gfs2_assert_withdraw(sdp, !pull);
564
565 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
566 log_incr_head(sdp);
567}
568
569static void log_flush_commit(struct gfs2_sbd *sdp)
570{
571 struct list_head *head = &sdp->sd_log_flush_list;
572 struct gfs2_log_buf *lb;
573 struct buffer_head *bh;
Steven Whitehousea0a24742007-07-09 15:43:07 +0100574 int flushcount = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575
576 while (!list_empty(head)) {
577 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
578 list_del(&lb->lb_list);
579 bh = lb->lb_bh;
580
581 wait_on_buffer(bh);
582 if (!buffer_uptodate(bh))
583 gfs2_io_error_bh(sdp, bh);
584 if (lb->lb_real) {
585 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
586 schedule();
587 free_buffer_head(bh);
588 } else
589 brelse(bh);
590 kfree(lb);
Steven Whitehousea0a24742007-07-09 15:43:07 +0100591 flushcount++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592 }
593
Steven Whitehousea0a24742007-07-09 15:43:07 +0100594 /* If nothing was journaled, the header is unplanned and unwanted. */
595 if (flushcount) {
596 log_write_header(sdp, 0, 0);
597 } else {
598 unsigned int tail;
599 tail = current_tail(sdp);
600
601 gfs2_ail1_empty(sdp, 0);
602 if (sdp->sd_log_tail != tail)
603 log_pull_tail(sdp, tail);
604 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605}
606
607/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400608 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609 * @sdp: the filesystem
610 * @gl: The glock structure to flush. If NULL, flush the whole incore log
611 *
612 */
613
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400614void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615{
616 struct gfs2_ail *ai;
617
Steven Whitehouse484adff2006-03-29 09:12:12 -0500618 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000619
620 if (gl) {
621 gfs2_log_lock(sdp);
622 if (list_empty(&gl->gl_le.le_list)) {
623 gfs2_log_unlock(sdp);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500624 up_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000625 return;
626 }
627 gfs2_log_unlock(sdp);
628 }
629
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400630 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
631 INIT_LIST_HEAD(&ai->ai_ail1_list);
632 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000633
Robert Peterson2332c442007-06-18 14:50:20 -0500634 gfs2_assert_withdraw(sdp,
635 sdp->sd_log_num_buf + sdp->sd_log_num_jdata ==
636 sdp->sd_log_commited_buf +
637 sdp->sd_log_commited_databuf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638 gfs2_assert_withdraw(sdp,
639 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
640
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 sdp->sd_log_flush_head = sdp->sd_log_head;
642 sdp->sd_log_flush_wrapped = 0;
643 ai->ai_first = sdp->sd_log_flush_head;
644
645 lops_before_commit(sdp);
646 if (!list_empty(&sdp->sd_log_flush_list))
647 log_flush_commit(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500648 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
649 gfs2_log_lock(sdp);
650 sdp->sd_log_blks_free--; /* Adjust for unreserved buffer */
651 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652 log_write_header(sdp, 0, PULL);
Robert Peterson2332c442007-06-18 14:50:20 -0500653 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400655
656 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400658 sdp->sd_log_blks_reserved = 0;
659 sdp->sd_log_commited_buf = 0;
Robert Peterson2332c442007-06-18 14:50:20 -0500660 sdp->sd_log_commited_databuf = 0;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400661 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662
David Teiglandb3b94fa2006-01-16 16:50:04 +0000663 if (!list_empty(&ai->ai_ail1_list)) {
664 list_add(&ai->ai_list, &sdp->sd_ail1_list);
665 ai = NULL;
666 }
667 gfs2_log_unlock(sdp);
668
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669 sdp->sd_vfs->s_dirt = 0;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500670 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671
672 kfree(ai);
673}
674
675static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
676{
Robert Peterson2332c442007-06-18 14:50:20 -0500677 unsigned int reserved;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 unsigned int old;
679
680 gfs2_log_lock(sdp);
681
682 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500683 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
684 tr->tr_num_databuf_rm;
685 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
686 (((int)sdp->sd_log_commited_databuf) >= 0));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
688 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
Robert Peterson2332c442007-06-18 14:50:20 -0500689 reserved = calc_reserved(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690 old = sdp->sd_log_blks_free;
691 sdp->sd_log_blks_free += tr->tr_reserved -
692 (reserved - sdp->sd_log_blks_reserved);
693
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400694 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
Robert Peterson2332c442007-06-18 14:50:20 -0500695 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <=
696 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697
698 sdp->sd_log_blks_reserved = reserved;
699
700 gfs2_log_unlock(sdp);
701}
702
703/**
704 * gfs2_log_commit - Commit a transaction to the log
705 * @sdp: the filesystem
706 * @tr: the transaction
707 *
708 * Returns: errno
709 */
710
711void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
712{
713 log_refund(sdp, tr);
714 lops_incore_commit(sdp, tr);
715
716 sdp->sd_vfs->s_dirt = 1;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500717 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719 gfs2_log_lock(sdp);
Steven Whitehouseb0041572006-11-23 10:51:34 -0500720 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
721 wake_up_process(sdp->sd_logd_process);
722 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723}
724
725/**
726 * gfs2_log_shutdown - write a shutdown header into a journal
727 * @sdp: the filesystem
728 *
729 */
730
731void gfs2_log_shutdown(struct gfs2_sbd *sdp)
732{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500733 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
736 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
737 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000738 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
740 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
741 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
742 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
743
744 sdp->sd_log_flush_head = sdp->sd_log_head;
745 sdp->sd_log_flush_wrapped = 0;
746
Robert Peterson2332c442007-06-18 14:50:20 -0500747 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
748 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749
Steven Whitehousea74604b2006-04-21 15:10:46 -0400750 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
751 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
752 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753
754 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755 sdp->sd_log_tail = sdp->sd_log_head;
756
Steven Whitehouse484adff2006-03-29 09:12:12 -0500757 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000758}
759
Steven Whitehousea25311c2006-11-23 11:06:35 -0500760
761/**
762 * gfs2_meta_syncfs - sync all the buffers in a filesystem
763 * @sdp: the filesystem
764 *
765 */
766
767void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
768{
769 gfs2_log_flush(sdp, NULL);
770 for (;;) {
771 gfs2_ail1_start(sdp, DIO_ALL);
772 if (gfs2_ail1_empty(sdp, DIO_ALL))
773 break;
774 msleep(10);
775 }
776}
777