blob: 8fce592f4011d97301aeb690cfdfb60a27bae967 [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>
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;
Steven Whitehousecd915492006-09-04 12:49:07 -040065 u64 sync_gen;
David Teiglandb3b94fa2006-01-16 16:50:04 +000066 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;
180 gfs2_log_unlock(sdp);
181 mutex_unlock(&sdp->sd_log_reserve_mutex);
182
183 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184
185 return 0;
186}
187
188/**
189 * gfs2_log_release - Release a given number of log blocks
190 * @sdp: The GFS2 superblock
191 * @blks: The number of blocks
192 *
193 */
194
195void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
196{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197
198 gfs2_log_lock(sdp);
199 sdp->sd_log_blks_free += blks;
200 gfs2_assert_withdraw(sdp,
201 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
202 gfs2_log_unlock(sdp);
Steven Whitehouseed386502006-04-07 16:28:07 -0400203 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204}
205
Steven Whitehousecd915492006-09-04 12:49:07 -0400206static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207{
208 int new = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -0400209 u64 dbn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210 int error;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400211 int bdy;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212
Steven Whitehousefd88de562006-05-05 16:59:11 -0400213 error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, &new, &dbn, &bdy);
Steven Whitehousea67cdbd2006-09-05 14:41:30 -0400214 if (error || !dbn)
Steven Whitehousefd4de2d2006-07-05 13:14:59 -0400215 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, (unsigned long long)dbn, lbn);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 gfs2_assert_withdraw(sdp, !error && dbn);
217
218 return dbn;
219}
220
221/**
222 * log_distance - Compute distance between two journal blocks
223 * @sdp: The GFS2 superblock
224 * @newer: The most recent journal block of the pair
225 * @older: The older journal block of the pair
226 *
227 * Compute the distance (in the journal direction) between two
228 * blocks in the journal
229 *
230 * Returns: the distance in blocks
231 */
232
233static inline unsigned int log_distance(struct gfs2_sbd *sdp,
234 unsigned int newer,
235 unsigned int older)
236{
237 int dist;
238
239 dist = newer - older;
240 if (dist < 0)
241 dist += sdp->sd_jdesc->jd_blocks;
242
243 return dist;
244}
245
246static unsigned int current_tail(struct gfs2_sbd *sdp)
247{
248 struct gfs2_ail *ai;
249 unsigned int tail;
250
251 gfs2_log_lock(sdp);
252
253 if (list_empty(&sdp->sd_ail1_list))
254 tail = sdp->sd_log_head;
255 else {
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400256 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail,
257 ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000258 tail = ai->ai_first;
259 }
260
261 gfs2_log_unlock(sdp);
262
263 return tail;
264}
265
266static inline void log_incr_head(struct gfs2_sbd *sdp)
267{
268 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
269 gfs2_assert_withdraw(sdp,
270 sdp->sd_log_flush_head == sdp->sd_log_head);
271
272 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
273 sdp->sd_log_flush_head = 0;
274 sdp->sd_log_flush_wrapped = 1;
275 }
276}
277
278/**
279 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
280 * @sdp: The GFS2 superblock
281 *
282 * Returns: the buffer_head
283 */
284
285struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
286{
Steven Whitehousecd915492006-09-04 12:49:07 -0400287 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288 struct gfs2_log_buf *lb;
289 struct buffer_head *bh;
290
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000291 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
293
294 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
295 lock_buffer(bh);
296 memset(bh->b_data, 0, bh->b_size);
297 set_buffer_uptodate(bh);
298 clear_buffer_dirty(bh);
299 unlock_buffer(bh);
300
301 log_incr_head(sdp);
302
303 return bh;
304}
305
306/**
307 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
308 * @sdp: the filesystem
309 * @data: the data the buffer_head should point to
310 *
311 * Returns: the log buffer descriptor
312 */
313
314struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
315 struct buffer_head *real)
316{
Steven Whitehousecd915492006-09-04 12:49:07 -0400317 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318 struct gfs2_log_buf *lb;
319 struct buffer_head *bh;
320
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000321 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
323 lb->lb_real = real;
324
325 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
326 atomic_set(&bh->b_count, 1);
327 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000328 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329 bh->b_blocknr = blkno;
330 bh->b_size = sdp->sd_sb.sb_bsize;
331 bh->b_bdev = sdp->sd_vfs->s_bdev;
332
333 log_incr_head(sdp);
334
335 return bh;
336}
337
338static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
339{
340 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
341
342 ail2_empty(sdp, new_tail);
343
344 gfs2_log_lock(sdp);
Jan Engelhardtc5392122006-09-05 14:30:40 +0200345 sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
346 /* 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 +0000347 gfs2_assert_withdraw(sdp,
348 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
349 gfs2_log_unlock(sdp);
350
351 sdp->sd_log_tail = new_tail;
352}
353
354/**
355 * log_write_header - Get and initialize a journal header buffer
356 * @sdp: The GFS2 superblock
357 *
358 * Returns: the initialized log buffer descriptor
359 */
360
Steven Whitehousecd915492006-09-04 12:49:07 -0400361static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362{
Steven Whitehousecd915492006-09-04 12:49:07 -0400363 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000364 struct buffer_head *bh;
365 struct gfs2_log_header *lh;
366 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400367 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000368
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400369 /* printk(KERN_INFO "log write header start (flags=%08x, pull=%d)\n", flags, pull); */
370
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371 bh = sb_getblk(sdp->sd_vfs, blkno);
372 lock_buffer(bh);
373 memset(bh->b_data, 0, bh->b_size);
374 set_buffer_uptodate(bh);
375 clear_buffer_dirty(bh);
376 unlock_buffer(bh);
377
378 gfs2_ail1_empty(sdp, 0);
379 tail = current_tail(sdp);
380
381 lh = (struct gfs2_log_header *)bh->b_data;
382 memset(lh, 0, sizeof(struct gfs2_log_header));
383 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500384 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
385 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400386 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
387 lh->lh_flags = cpu_to_be32(flags);
388 lh->lh_tail = cpu_to_be32(tail);
389 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
391 lh->lh_hash = cpu_to_be32(hash);
392
393 set_buffer_dirty(bh);
394 if (sync_dirty_buffer(bh))
395 gfs2_io_error_bh(sdp, bh);
396 brelse(bh);
397
398 if (sdp->sd_log_tail != tail)
399 log_pull_tail(sdp, tail, pull);
400 else
401 gfs2_assert_withdraw(sdp, !pull);
402
403 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
404 log_incr_head(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400405
406 /* printk(KERN_INFO "log write header out\n"); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407}
408
409static void log_flush_commit(struct gfs2_sbd *sdp)
410{
411 struct list_head *head = &sdp->sd_log_flush_list;
412 struct gfs2_log_buf *lb;
413 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414
415 while (!list_empty(head)) {
416 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
417 list_del(&lb->lb_list);
418 bh = lb->lb_bh;
419
420 wait_on_buffer(bh);
421 if (!buffer_uptodate(bh))
422 gfs2_io_error_bh(sdp, bh);
423 if (lb->lb_real) {
424 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
425 schedule();
426 free_buffer_head(bh);
427 } else
428 brelse(bh);
429 kfree(lb);
430 }
431
432 log_write_header(sdp, 0, 0);
433}
434
435/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400436 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437 * @sdp: the filesystem
438 * @gl: The glock structure to flush. If NULL, flush the whole incore log
439 *
440 */
441
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400442void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443{
444 struct gfs2_ail *ai;
445
Steven Whitehouse484adff2006-03-29 09:12:12 -0500446 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000447
448 if (gl) {
449 gfs2_log_lock(sdp);
450 if (list_empty(&gl->gl_le.le_list)) {
451 gfs2_log_unlock(sdp);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500452 up_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000453 return;
454 }
455 gfs2_log_unlock(sdp);
456 }
457
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400458 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
459 INIT_LIST_HEAD(&ai->ai_ail1_list);
460 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461
462 gfs2_assert_withdraw(sdp,
463 sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
464 gfs2_assert_withdraw(sdp,
465 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
466
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467 sdp->sd_log_flush_head = sdp->sd_log_head;
468 sdp->sd_log_flush_wrapped = 0;
469 ai->ai_first = sdp->sd_log_flush_head;
470
471 lops_before_commit(sdp);
472 if (!list_empty(&sdp->sd_log_flush_list))
473 log_flush_commit(sdp);
474 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
475 log_write_header(sdp, 0, PULL);
476 lops_after_commit(sdp, ai);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400478
479 /* printk(KERN_INFO "sd_log_num_hdrs %u\n", sdp->sd_log_num_hdrs); */
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400480 sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481
482 sdp->sd_log_blks_reserved =
483 sdp->sd_log_commited_buf =
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400484 sdp->sd_log_num_hdrs =
David Teiglandb3b94fa2006-01-16 16:50:04 +0000485 sdp->sd_log_commited_revoke = 0;
486
487 gfs2_log_lock(sdp);
488 if (!list_empty(&ai->ai_ail1_list)) {
489 list_add(&ai->ai_list, &sdp->sd_ail1_list);
490 ai = NULL;
491 }
492 gfs2_log_unlock(sdp);
493
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494 sdp->sd_vfs->s_dirt = 0;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500495 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000496
497 kfree(ai);
498}
499
500static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
501{
Benjamin Marzinski5dc39fe2006-08-24 14:47:17 -0500502 unsigned int reserved = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503 unsigned int old;
504
505 gfs2_log_lock(sdp);
506
507 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
508 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
509 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
510 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
511
512 if (sdp->sd_log_commited_buf)
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400513 reserved += sdp->sd_log_commited_buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000514 if (sdp->sd_log_commited_revoke)
515 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
Steven Whitehousecd915492006-09-04 12:49:07 -0400516 sizeof(u64));
Benjamin Marzinski5dc39fe2006-08-24 14:47:17 -0500517 if (reserved)
518 reserved++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000519
520 old = sdp->sd_log_blks_free;
521 sdp->sd_log_blks_free += tr->tr_reserved -
522 (reserved - sdp->sd_log_blks_reserved);
523
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400524 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525 gfs2_assert_withdraw(sdp,
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400526 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
527 sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528
529 sdp->sd_log_blks_reserved = reserved;
530
531 gfs2_log_unlock(sdp);
532}
533
534/**
535 * gfs2_log_commit - Commit a transaction to the log
536 * @sdp: the filesystem
537 * @tr: the transaction
538 *
539 * Returns: errno
540 */
541
542void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
543{
544 log_refund(sdp, tr);
545 lops_incore_commit(sdp, tr);
546
547 sdp->sd_vfs->s_dirt = 1;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500548 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550 gfs2_log_lock(sdp);
551 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
552 gfs2_log_unlock(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400553 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554 } else
555 gfs2_log_unlock(sdp);
556}
557
558/**
559 * gfs2_log_shutdown - write a shutdown header into a journal
560 * @sdp: the filesystem
561 *
562 */
563
564void gfs2_log_shutdown(struct gfs2_sbd *sdp)
565{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500566 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000567
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
569 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
570 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000571 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
573 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
574 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400575 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
577
578 sdp->sd_log_flush_head = sdp->sd_log_head;
579 sdp->sd_log_flush_wrapped = 0;
580
581 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
582
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400583 /* 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 -0400584 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
585 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
586 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587
588 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 sdp->sd_log_tail = sdp->sd_log_head;
590
Steven Whitehouse484adff2006-03-29 09:12:12 -0500591 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592}
593