blob: 586923d24e6e43addffdf4a6641027f77bec1cbc [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{
148 struct list_head *head = &sdp->sd_ail1_list;
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);
155 if (list_empty(head)) {
156 gfs2_log_unlock(sdp);
157 return;
158 }
159 sync_gen = sdp->sd_ail_sync_gen++;
160
161 first = head->prev;
162 first_ai = list_entry(first, struct gfs2_ail, ai_list);
163 first_ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400164 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000165
166 if (flags & DIO_ALL)
167 first = NULL;
168
Steven Whitehouse74669412006-09-19 11:17:38 -0400169 while(!done) {
Steven Whitehouse484adff2006-03-29 09:12:12 -0500170 if (first && (head->prev != first ||
171 gfs2_ail1_empty_one(sdp, first_ai, 0)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 break;
173
Steven Whitehouse74669412006-09-19 11:17:38 -0400174 done = 1;
175 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 if (ai->ai_sync_gen >= sync_gen)
177 continue;
178 ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400179 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
180 done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 break;
182 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183 }
184
185 gfs2_log_unlock(sdp);
186}
187
188int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
189{
190 struct gfs2_ail *ai, *s;
191 int ret;
192
193 gfs2_log_lock(sdp);
194
195 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
196 if (gfs2_ail1_empty_one(sdp, ai, flags))
197 list_move(&ai->ai_list, &sdp->sd_ail2_list);
198 else if (!(flags & DIO_ALL))
199 break;
200 }
201
202 ret = list_empty(&sdp->sd_ail1_list);
203
204 gfs2_log_unlock(sdp);
205
206 return ret;
207}
208
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400209
210/**
211 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
212 * @sdp: the filesystem
213 * @ai: the AIL entry
214 *
215 */
216
217static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
218{
219 struct list_head *head = &ai->ai_ail2_list;
220 struct gfs2_bufdata *bd;
221
222 while (!list_empty(head)) {
223 bd = list_entry(head->prev, struct gfs2_bufdata,
224 bd_ail_st_list);
225 gfs2_assert(sdp, bd->bd_ail == ai);
226 bd->bd_ail = NULL;
227 list_del(&bd->bd_ail_st_list);
228 list_del(&bd->bd_ail_gl_list);
229 atomic_dec(&bd->bd_gl->gl_ail_count);
230 brelse(bd->bd_bh);
231 }
232}
233
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
235{
236 struct gfs2_ail *ai, *safe;
237 unsigned int old_tail = sdp->sd_log_tail;
238 int wrap = (new_tail < old_tail);
239 int a, b, rm;
240
241 gfs2_log_lock(sdp);
242
243 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
244 a = (old_tail <= ai->ai_first);
245 b = (ai->ai_first < new_tail);
246 rm = (wrap) ? (a || b) : (a && b);
247 if (!rm)
248 continue;
249
250 gfs2_ail2_empty_one(sdp, ai);
251 list_del(&ai->ai_list);
252 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
253 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
254 kfree(ai);
255 }
256
257 gfs2_log_unlock(sdp);
258}
259
260/**
261 * gfs2_log_reserve - Make a log reservation
262 * @sdp: The GFS2 superblock
263 * @blks: The number of blocks to reserve
264 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100265 * Note that we never give out the last few blocks of the journal. Thats
Steven Whitehouseb0041572006-11-23 10:51:34 -0500266 * due to the fact that there is are a small number of header blocks
267 * associated with each log flush. The exact number can't be known until
268 * flush time, so we ensure that we have just enough free blocks at all
269 * times to avoid running out during a log flush.
270 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 * Returns: errno
272 */
273
274int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
275{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000276 unsigned int try = 0;
Steven Whitehouse89918642007-06-01 15:19:33 +0100277 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278
279 if (gfs2_assert_warn(sdp, blks) ||
280 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
281 return -EINVAL;
282
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500283 mutex_lock(&sdp->sd_log_reserve_mutex);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500284 gfs2_log_lock(sdp);
Steven Whitehouse89918642007-06-01 15:19:33 +0100285 while(sdp->sd_log_blks_free <= (blks + reserved_blks)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000286 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287 gfs2_ail1_empty(sdp, 0);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400288 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000289
290 if (try++)
291 gfs2_ail1_start(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500292 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000293 }
Steven Whitehouse484adff2006-03-29 09:12:12 -0500294 sdp->sd_log_blks_free -= blks;
295 gfs2_log_unlock(sdp);
296 mutex_unlock(&sdp->sd_log_reserve_mutex);
297
298 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299
300 return 0;
301}
302
303/**
304 * gfs2_log_release - Release a given number of log blocks
305 * @sdp: The GFS2 superblock
306 * @blks: The number of blocks
307 *
308 */
309
310void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
311{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000312
313 gfs2_log_lock(sdp);
314 sdp->sd_log_blks_free += blks;
315 gfs2_assert_withdraw(sdp,
316 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
317 gfs2_log_unlock(sdp);
Steven Whitehouseed386502006-04-07 16:28:07 -0400318 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319}
320
Steven Whitehousecd915492006-09-04 12:49:07 -0400321static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322{
Steven Whitehouse23591252006-10-13 17:25:45 -0400323 struct inode *inode = sdp->sd_jdesc->jd_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000324 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400325 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000326
Steven Whitehouse23591252006-10-13 17:25:45 -0400327 bh_map.b_size = 1 << inode->i_blkbits;
328 error = gfs2_block_map(inode, lbn, 0, &bh_map);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400329 if (error || !bh_map.b_blocknr)
Ryusuke Konishiaed32552006-11-28 02:53:22 +0900330 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error,
331 (unsigned long long)bh_map.b_blocknr, lbn);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400332 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400334 return bh_map.b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335}
336
337/**
338 * log_distance - Compute distance between two journal blocks
339 * @sdp: The GFS2 superblock
340 * @newer: The most recent journal block of the pair
341 * @older: The older journal block of the pair
342 *
343 * Compute the distance (in the journal direction) between two
344 * blocks in the journal
345 *
346 * Returns: the distance in blocks
347 */
348
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400349static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 unsigned int older)
351{
352 int dist;
353
354 dist = newer - older;
355 if (dist < 0)
356 dist += sdp->sd_jdesc->jd_blocks;
357
358 return dist;
359}
360
361static unsigned int current_tail(struct gfs2_sbd *sdp)
362{
363 struct gfs2_ail *ai;
364 unsigned int tail;
365
366 gfs2_log_lock(sdp);
367
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400368 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400370 } else {
371 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372 tail = ai->ai_first;
373 }
374
375 gfs2_log_unlock(sdp);
376
377 return tail;
378}
379
380static inline void log_incr_head(struct gfs2_sbd *sdp)
381{
382 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400383 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384
385 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
386 sdp->sd_log_flush_head = 0;
387 sdp->sd_log_flush_wrapped = 1;
388 }
389}
390
391/**
392 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
393 * @sdp: The GFS2 superblock
394 *
395 * Returns: the buffer_head
396 */
397
398struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
399{
Steven Whitehousecd915492006-09-04 12:49:07 -0400400 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000401 struct gfs2_log_buf *lb;
402 struct buffer_head *bh;
403
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000404 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
406
407 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
408 lock_buffer(bh);
409 memset(bh->b_data, 0, bh->b_size);
410 set_buffer_uptodate(bh);
411 clear_buffer_dirty(bh);
412 unlock_buffer(bh);
413
414 log_incr_head(sdp);
415
416 return bh;
417}
418
419/**
420 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
421 * @sdp: the filesystem
422 * @data: the data the buffer_head should point to
423 *
424 * Returns: the log buffer descriptor
425 */
426
427struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
428 struct buffer_head *real)
429{
Steven Whitehousecd915492006-09-04 12:49:07 -0400430 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431 struct gfs2_log_buf *lb;
432 struct buffer_head *bh;
433
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000434 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
436 lb->lb_real = real;
437
438 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
439 atomic_set(&bh->b_count, 1);
440 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000441 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 bh->b_blocknr = blkno;
443 bh->b_size = sdp->sd_sb.sb_bsize;
444 bh->b_bdev = sdp->sd_vfs->s_bdev;
445
446 log_incr_head(sdp);
447
448 return bh;
449}
450
451static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
452{
453 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
454
455 ail2_empty(sdp, new_tail);
456
457 gfs2_log_lock(sdp);
Jan Engelhardtc5392122006-09-05 14:30:40 +0200458 sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400459 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 gfs2_log_unlock(sdp);
461
462 sdp->sd_log_tail = new_tail;
463}
464
465/**
466 * log_write_header - Get and initialize a journal header buffer
467 * @sdp: The GFS2 superblock
468 *
469 * Returns: the initialized log buffer descriptor
470 */
471
Steven Whitehousecd915492006-09-04 12:49:07 -0400472static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473{
Steven Whitehousecd915492006-09-04 12:49:07 -0400474 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 struct buffer_head *bh;
476 struct gfs2_log_header *lh;
477 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400478 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 bh = sb_getblk(sdp->sd_vfs, blkno);
481 lock_buffer(bh);
482 memset(bh->b_data, 0, bh->b_size);
483 set_buffer_uptodate(bh);
484 clear_buffer_dirty(bh);
485 unlock_buffer(bh);
486
487 gfs2_ail1_empty(sdp, 0);
488 tail = current_tail(sdp);
489
490 lh = (struct gfs2_log_header *)bh->b_data;
491 memset(lh, 0, sizeof(struct gfs2_log_header));
492 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500493 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
494 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400495 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
496 lh->lh_flags = cpu_to_be32(flags);
497 lh->lh_tail = cpu_to_be32(tail);
498 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
500 lh->lh_hash = cpu_to_be32(hash);
501
502 set_buffer_dirty(bh);
503 if (sync_dirty_buffer(bh))
504 gfs2_io_error_bh(sdp, bh);
505 brelse(bh);
506
507 if (sdp->sd_log_tail != tail)
508 log_pull_tail(sdp, tail, pull);
509 else
510 gfs2_assert_withdraw(sdp, !pull);
511
512 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
513 log_incr_head(sdp);
514}
515
516static void log_flush_commit(struct gfs2_sbd *sdp)
517{
518 struct list_head *head = &sdp->sd_log_flush_list;
519 struct gfs2_log_buf *lb;
520 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521
522 while (!list_empty(head)) {
523 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
524 list_del(&lb->lb_list);
525 bh = lb->lb_bh;
526
527 wait_on_buffer(bh);
528 if (!buffer_uptodate(bh))
529 gfs2_io_error_bh(sdp, bh);
530 if (lb->lb_real) {
531 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
532 schedule();
533 free_buffer_head(bh);
534 } else
535 brelse(bh);
536 kfree(lb);
537 }
538
539 log_write_header(sdp, 0, 0);
540}
541
542/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400543 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544 * @sdp: the filesystem
545 * @gl: The glock structure to flush. If NULL, flush the whole incore log
546 *
547 */
548
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400549void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550{
551 struct gfs2_ail *ai;
552
Steven Whitehouse484adff2006-03-29 09:12:12 -0500553 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000554
555 if (gl) {
556 gfs2_log_lock(sdp);
557 if (list_empty(&gl->gl_le.le_list)) {
558 gfs2_log_unlock(sdp);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500559 up_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000560 return;
561 }
562 gfs2_log_unlock(sdp);
563 }
564
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400565 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
566 INIT_LIST_HEAD(&ai->ai_ail1_list);
567 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400569 gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570 gfs2_assert_withdraw(sdp,
571 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
572
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573 sdp->sd_log_flush_head = sdp->sd_log_head;
574 sdp->sd_log_flush_wrapped = 0;
575 ai->ai_first = sdp->sd_log_flush_head;
576
577 lops_before_commit(sdp);
578 if (!list_empty(&sdp->sd_log_flush_list))
579 log_flush_commit(sdp);
580 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
581 log_write_header(sdp, 0, PULL);
582 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400583
584 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400586 sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400587 sdp->sd_log_blks_reserved = 0;
588 sdp->sd_log_commited_buf = 0;
589 sdp->sd_log_num_hdrs = 0;
590 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592 if (!list_empty(&ai->ai_ail1_list)) {
593 list_add(&ai->ai_list, &sdp->sd_ail1_list);
594 ai = NULL;
595 }
596 gfs2_log_unlock(sdp);
597
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598 sdp->sd_vfs->s_dirt = 0;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500599 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600
601 kfree(ai);
602}
603
604static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
605{
Benjamin Marzinski5dc39fe2006-08-24 14:47:17 -0500606 unsigned int reserved = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 unsigned int old;
608
609 gfs2_log_lock(sdp);
610
611 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
612 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
613 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
614 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
615
616 if (sdp->sd_log_commited_buf)
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400617 reserved += sdp->sd_log_commited_buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618 if (sdp->sd_log_commited_revoke)
619 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
Steven Whitehousecd915492006-09-04 12:49:07 -0400620 sizeof(u64));
Benjamin Marzinski5dc39fe2006-08-24 14:47:17 -0500621 if (reserved)
622 reserved++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623
624 old = sdp->sd_log_blks_free;
625 sdp->sd_log_blks_free += tr->tr_reserved -
626 (reserved - sdp->sd_log_blks_reserved);
627
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400628 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629 gfs2_assert_withdraw(sdp,
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400630 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
631 sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632
633 sdp->sd_log_blks_reserved = reserved;
634
635 gfs2_log_unlock(sdp);
636}
637
638/**
639 * gfs2_log_commit - Commit a transaction to the log
640 * @sdp: the filesystem
641 * @tr: the transaction
642 *
643 * Returns: errno
644 */
645
646void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
647{
648 log_refund(sdp, tr);
649 lops_incore_commit(sdp, tr);
650
651 sdp->sd_vfs->s_dirt = 1;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500652 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000653
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 gfs2_log_lock(sdp);
Steven Whitehouseb0041572006-11-23 10:51:34 -0500655 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
656 wake_up_process(sdp->sd_logd_process);
657 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658}
659
660/**
661 * gfs2_log_shutdown - write a shutdown header into a journal
662 * @sdp: the filesystem
663 *
664 */
665
666void gfs2_log_shutdown(struct gfs2_sbd *sdp)
667{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500668 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
671 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
672 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000673 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
675 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
676 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400677 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
679
680 sdp->sd_log_flush_head = sdp->sd_log_head;
681 sdp->sd_log_flush_wrapped = 0;
682
683 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
684
Steven Whitehousea74604b2006-04-21 15:10:46 -0400685 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
686 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
687 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688
689 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690 sdp->sd_log_tail = sdp->sd_log_head;
691
Steven Whitehouse484adff2006-03-29 09:12:12 -0500692 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693}
694
Steven Whitehousea25311c2006-11-23 11:06:35 -0500695
696/**
697 * gfs2_meta_syncfs - sync all the buffers in a filesystem
698 * @sdp: the filesystem
699 *
700 */
701
702void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
703{
704 gfs2_log_flush(sdp, NULL);
705 for (;;) {
706 gfs2_ail1_start(sdp, DIO_ALL);
707 if (gfs2_ail1_empty(sdp, DIO_ALL))
708 break;
709 msleep(10);
710 }
711}
712