blob: 60fdc94ccc8aabbe053d7cfd743060b0705a9a44 [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
7 * of the GNU General Public License v.2.
8 */
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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "lm_interface.h"
20#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "bmap.h"
22#include "glock.h"
23#include "log.h"
24#include "lops.h"
25#include "meta_io.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050026#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050027#include "dir.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028
29#define PULL 1
30
David Teiglandb3b94fa2006-01-16 16:50:04 +000031/**
32 * gfs2_struct2blk - compute stuff
33 * @sdp: the filesystem
34 * @nstruct: the number of structures
35 * @ssize: the size of the structures
36 *
37 * Compute the number of log descriptor blocks needed to hold a certain number
38 * of structures of a certain size.
39 *
40 * Returns: the number of blocks needed (minimum is always 1)
41 */
42
43unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
44 unsigned int ssize)
45{
46 unsigned int blks;
47 unsigned int first, second;
48
49 blks = 1;
Steven Whitehouse568f4c92006-02-27 12:00:42 -050050 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) /
51 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
62void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
63{
64 struct list_head *head = &sdp->sd_ail1_list;
65 uint64_t sync_gen;
66 struct list_head *first, *tmp;
67 struct gfs2_ail *first_ai, *ai;
68
69 gfs2_log_lock(sdp);
70 if (list_empty(head)) {
71 gfs2_log_unlock(sdp);
72 return;
73 }
74 sync_gen = sdp->sd_ail_sync_gen++;
75
76 first = head->prev;
77 first_ai = list_entry(first, struct gfs2_ail, ai_list);
78 first_ai->ai_sync_gen = sync_gen;
79 gfs2_ail1_start_one(sdp, first_ai);
80
81 if (flags & DIO_ALL)
82 first = NULL;
83
84 for (;;) {
Steven Whitehouse484adff2006-03-29 09:12:12 -050085 if (first && (head->prev != first ||
86 gfs2_ail1_empty_one(sdp, first_ai, 0)))
David Teiglandb3b94fa2006-01-16 16:50:04 +000087 break;
88
89 for (tmp = head->prev; tmp != head; tmp = tmp->prev) {
90 ai = list_entry(tmp, struct gfs2_ail, ai_list);
91 if (ai->ai_sync_gen >= sync_gen)
92 continue;
93 ai->ai_sync_gen = sync_gen;
94 gfs2_ail1_start_one(sdp, ai);
95 break;
96 }
97
98 if (tmp == head)
99 break;
100 }
101
102 gfs2_log_unlock(sdp);
103}
104
105int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
106{
107 struct gfs2_ail *ai, *s;
108 int ret;
109
110 gfs2_log_lock(sdp);
111
112 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
113 if (gfs2_ail1_empty_one(sdp, ai, flags))
114 list_move(&ai->ai_list, &sdp->sd_ail2_list);
115 else if (!(flags & DIO_ALL))
116 break;
117 }
118
119 ret = list_empty(&sdp->sd_ail1_list);
120
121 gfs2_log_unlock(sdp);
122
123 return ret;
124}
125
126static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
127{
128 struct gfs2_ail *ai, *safe;
129 unsigned int old_tail = sdp->sd_log_tail;
130 int wrap = (new_tail < old_tail);
131 int a, b, rm;
132
133 gfs2_log_lock(sdp);
134
135 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
136 a = (old_tail <= ai->ai_first);
137 b = (ai->ai_first < new_tail);
138 rm = (wrap) ? (a || b) : (a && b);
139 if (!rm)
140 continue;
141
142 gfs2_ail2_empty_one(sdp, ai);
143 list_del(&ai->ai_list);
144 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
145 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
146 kfree(ai);
147 }
148
149 gfs2_log_unlock(sdp);
150}
151
152/**
153 * gfs2_log_reserve - Make a log reservation
154 * @sdp: The GFS2 superblock
155 * @blks: The number of blocks to reserve
156 *
157 * Returns: errno
158 */
159
160int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
161{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 unsigned int try = 0;
163
164 if (gfs2_assert_warn(sdp, blks) ||
165 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
166 return -EINVAL;
167
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500168 mutex_lock(&sdp->sd_log_reserve_mutex);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500169 gfs2_log_lock(sdp);
170 while(sdp->sd_log_blks_free <= blks) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 gfs2_ail1_empty(sdp, 0);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400173 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174
175 if (try++)
176 gfs2_ail1_start(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500177 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178 }
Steven Whitehouse484adff2006-03-29 09:12:12 -0500179 sdp->sd_log_blks_free -= blks;
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400180 /* printk(KERN_INFO "reserved %u blocks (%u left)\n", blks, sdp->sd_log_blks_free); */
Steven Whitehouse484adff2006-03-29 09:12:12 -0500181 gfs2_log_unlock(sdp);
182 mutex_unlock(&sdp->sd_log_reserve_mutex);
183
184 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185
186 return 0;
187}
188
189/**
190 * gfs2_log_release - Release a given number of log blocks
191 * @sdp: The GFS2 superblock
192 * @blks: The number of blocks
193 *
194 */
195
196void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
197{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198
199 gfs2_log_lock(sdp);
200 sdp->sd_log_blks_free += blks;
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400201 /* printk(KERN_INFO "released %u blocks (%u left)\n", blks, sdp->sd_log_blks_free); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202 gfs2_assert_withdraw(sdp,
203 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
204 gfs2_log_unlock(sdp);
Steven Whitehouseed386502006-04-07 16:28:07 -0400205 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206}
207
208static uint64_t log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
209{
210 int new = 0;
211 uint64_t dbn;
212 int error;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400213 int bdy;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000214
Steven Whitehousefd88de562006-05-05 16:59:11 -0400215 error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, &new, &dbn, &bdy);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400216 if (!(!error && dbn)) {
Steven Whitehousefd4de2d2006-07-05 13:14:59 -0400217 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, (unsigned long long)dbn, lbn);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400218 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000219 gfs2_assert_withdraw(sdp, !error && dbn);
220
221 return dbn;
222}
223
224/**
225 * log_distance - Compute distance between two journal blocks
226 * @sdp: The GFS2 superblock
227 * @newer: The most recent journal block of the pair
228 * @older: The older journal block of the pair
229 *
230 * Compute the distance (in the journal direction) between two
231 * blocks in the journal
232 *
233 * Returns: the distance in blocks
234 */
235
236static inline unsigned int log_distance(struct gfs2_sbd *sdp,
237 unsigned int newer,
238 unsigned int older)
239{
240 int dist;
241
242 dist = newer - older;
243 if (dist < 0)
244 dist += sdp->sd_jdesc->jd_blocks;
245
246 return dist;
247}
248
249static unsigned int current_tail(struct gfs2_sbd *sdp)
250{
251 struct gfs2_ail *ai;
252 unsigned int tail;
253
254 gfs2_log_lock(sdp);
255
256 if (list_empty(&sdp->sd_ail1_list))
257 tail = sdp->sd_log_head;
258 else {
259 ai = list_entry(sdp->sd_ail1_list.prev,
260 struct gfs2_ail, ai_list);
261 tail = ai->ai_first;
262 }
263
264 gfs2_log_unlock(sdp);
265
266 return tail;
267}
268
269static inline void log_incr_head(struct gfs2_sbd *sdp)
270{
271 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
272 gfs2_assert_withdraw(sdp,
273 sdp->sd_log_flush_head == sdp->sd_log_head);
274
275 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
276 sdp->sd_log_flush_head = 0;
277 sdp->sd_log_flush_wrapped = 1;
278 }
279}
280
281/**
282 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
283 * @sdp: The GFS2 superblock
284 *
285 * Returns: the buffer_head
286 */
287
288struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
289{
290 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
291 struct gfs2_log_buf *lb;
292 struct buffer_head *bh;
293
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000294 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
296
297 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
298 lock_buffer(bh);
299 memset(bh->b_data, 0, bh->b_size);
300 set_buffer_uptodate(bh);
301 clear_buffer_dirty(bh);
302 unlock_buffer(bh);
303
304 log_incr_head(sdp);
305
306 return bh;
307}
308
309/**
310 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
311 * @sdp: the filesystem
312 * @data: the data the buffer_head should point to
313 *
314 * Returns: the log buffer descriptor
315 */
316
317struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
318 struct buffer_head *real)
319{
320 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
321 struct gfs2_log_buf *lb;
322 struct buffer_head *bh;
323
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000324 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000325 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
326 lb->lb_real = real;
327
328 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
329 atomic_set(&bh->b_count, 1);
330 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000331 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000332 bh->b_blocknr = blkno;
333 bh->b_size = sdp->sd_sb.sb_bsize;
334 bh->b_bdev = sdp->sd_vfs->s_bdev;
335
336 log_incr_head(sdp);
337
338 return bh;
339}
340
341static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
342{
343 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
344
345 ail2_empty(sdp, new_tail);
346
347 gfs2_log_lock(sdp);
348 sdp->sd_log_blks_free += dist - ((pull) ? 1 : 0);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400349 /* printk(KERN_INFO "pull tail refunding %u blocks (%u left) pull=%d\n", dist - ((pull) ? 1 : 0), sdp->sd_log_blks_free, pull); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 gfs2_assert_withdraw(sdp,
351 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
352 gfs2_log_unlock(sdp);
353
354 sdp->sd_log_tail = new_tail;
355}
356
357/**
358 * log_write_header - Get and initialize a journal header buffer
359 * @sdp: The GFS2 superblock
360 *
361 * Returns: the initialized log buffer descriptor
362 */
363
364static void log_write_header(struct gfs2_sbd *sdp, uint32_t flags, int pull)
365{
366 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
367 struct buffer_head *bh;
368 struct gfs2_log_header *lh;
369 unsigned int tail;
370 uint32_t hash;
371
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400372 /* printk(KERN_INFO "log write header start (flags=%08x, pull=%d)\n", flags, pull); */
373
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374 bh = sb_getblk(sdp->sd_vfs, blkno);
375 lock_buffer(bh);
376 memset(bh->b_data, 0, bh->b_size);
377 set_buffer_uptodate(bh);
378 clear_buffer_dirty(bh);
379 unlock_buffer(bh);
380
381 gfs2_ail1_empty(sdp, 0);
382 tail = current_tail(sdp);
383
384 lh = (struct gfs2_log_header *)bh->b_data;
385 memset(lh, 0, sizeof(struct gfs2_log_header));
386 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500387 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
388 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400389 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
390 lh->lh_flags = cpu_to_be32(flags);
391 lh->lh_tail = cpu_to_be32(tail);
392 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000393 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
394 lh->lh_hash = cpu_to_be32(hash);
395
396 set_buffer_dirty(bh);
397 if (sync_dirty_buffer(bh))
398 gfs2_io_error_bh(sdp, bh);
399 brelse(bh);
400
401 if (sdp->sd_log_tail != tail)
402 log_pull_tail(sdp, tail, pull);
403 else
404 gfs2_assert_withdraw(sdp, !pull);
405
406 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
407 log_incr_head(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400408
409 /* printk(KERN_INFO "log write header out\n"); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000410}
411
412static void log_flush_commit(struct gfs2_sbd *sdp)
413{
414 struct list_head *head = &sdp->sd_log_flush_list;
415 struct gfs2_log_buf *lb;
416 struct buffer_head *bh;
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400417#if 0
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418 unsigned int d;
419
420 d = log_distance(sdp, sdp->sd_log_flush_head, sdp->sd_log_head);
421
422 gfs2_assert_withdraw(sdp, d + 1 == sdp->sd_log_blks_reserved);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400423#endif
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424
425 while (!list_empty(head)) {
426 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
427 list_del(&lb->lb_list);
428 bh = lb->lb_bh;
429
430 wait_on_buffer(bh);
431 if (!buffer_uptodate(bh))
432 gfs2_io_error_bh(sdp, bh);
433 if (lb->lb_real) {
434 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
435 schedule();
436 free_buffer_head(bh);
437 } else
438 brelse(bh);
439 kfree(lb);
440 }
441
442 log_write_header(sdp, 0, 0);
443}
444
445/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400446 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447 * @sdp: the filesystem
448 * @gl: The glock structure to flush. If NULL, flush the whole incore log
449 *
450 */
451
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400452void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000453{
454 struct gfs2_ail *ai;
455
Steven Whitehouse484adff2006-03-29 09:12:12 -0500456 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000457
458 if (gl) {
459 gfs2_log_lock(sdp);
460 if (list_empty(&gl->gl_le.le_list)) {
461 gfs2_log_unlock(sdp);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500462 up_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000463 return;
464 }
465 gfs2_log_unlock(sdp);
466 }
467
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400468 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
469 INIT_LIST_HEAD(&ai->ai_ail1_list);
470 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471
472 gfs2_assert_withdraw(sdp,
473 sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
474 gfs2_assert_withdraw(sdp,
475 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
476
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477 sdp->sd_log_flush_head = sdp->sd_log_head;
478 sdp->sd_log_flush_wrapped = 0;
479 ai->ai_first = sdp->sd_log_flush_head;
480
481 lops_before_commit(sdp);
482 if (!list_empty(&sdp->sd_log_flush_list))
483 log_flush_commit(sdp);
484 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
485 log_write_header(sdp, 0, PULL);
486 lops_after_commit(sdp, ai);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000487 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400488
489 /* printk(KERN_INFO "sd_log_num_hdrs %u\n", sdp->sd_log_num_hdrs); */
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400490 sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491
492 sdp->sd_log_blks_reserved =
493 sdp->sd_log_commited_buf =
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400494 sdp->sd_log_num_hdrs =
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495 sdp->sd_log_commited_revoke = 0;
496
497 gfs2_log_lock(sdp);
498 if (!list_empty(&ai->ai_ail1_list)) {
499 list_add(&ai->ai_list, &sdp->sd_ail1_list);
500 ai = NULL;
501 }
502 gfs2_log_unlock(sdp);
503
David Teiglandb3b94fa2006-01-16 16:50:04 +0000504 sdp->sd_vfs->s_dirt = 0;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500505 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000506
507 kfree(ai);
508}
509
510static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
511{
512 unsigned int reserved = 1;
513 unsigned int old;
514
515 gfs2_log_lock(sdp);
516
517 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
518 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
519 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
520 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
521
522 if (sdp->sd_log_commited_buf)
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400523 reserved += sdp->sd_log_commited_buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000524 if (sdp->sd_log_commited_revoke)
525 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
526 sizeof(uint64_t));
527
528 old = sdp->sd_log_blks_free;
529 sdp->sd_log_blks_free += tr->tr_reserved -
530 (reserved - sdp->sd_log_blks_reserved);
531
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400532 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533 gfs2_assert_withdraw(sdp,
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400534 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
535 sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536
537 sdp->sd_log_blks_reserved = reserved;
538
539 gfs2_log_unlock(sdp);
540}
541
542/**
543 * gfs2_log_commit - Commit a transaction to the log
544 * @sdp: the filesystem
545 * @tr: the transaction
546 *
547 * Returns: errno
548 */
549
550void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
551{
552 log_refund(sdp, tr);
553 lops_incore_commit(sdp, tr);
554
555 sdp->sd_vfs->s_dirt = 1;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500556 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558 gfs2_log_lock(sdp);
559 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
560 gfs2_log_unlock(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400561 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 } else
563 gfs2_log_unlock(sdp);
564}
565
566/**
567 * gfs2_log_shutdown - write a shutdown header into a journal
568 * @sdp: the filesystem
569 *
570 */
571
572void gfs2_log_shutdown(struct gfs2_sbd *sdp)
573{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500574 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
577 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
578 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000579 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
581 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
582 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400583 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
585
586 sdp->sd_log_flush_head = sdp->sd_log_head;
587 sdp->sd_log_flush_wrapped = 0;
588
589 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
590
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400591 /* printk(KERN_INFO "sd_log_blks_free %u, sd_jdesc->jd_blocks %u\n", sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks); */
Steven Whitehousea74604b2006-04-21 15:10:46 -0400592 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
593 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
594 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595
596 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 sdp->sd_log_tail = sdp->sd_log_head;
598
Steven Whitehouse484adff2006-03-29 09:12:12 -0500599 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600}
601