blob: ab341cd0a76acaa049a487184ad2b9a928aaa591 [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 Whitehousefaa31ce2006-09-13 11:13:27 -040050 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
David Teiglandb3b94fa2006-01-16 16:50:04 +000051
52 if (nstruct > first) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -050053 second = (sdp->sd_sb.sb_bsize -
54 sizeof(struct gfs2_meta_header)) / ssize;
Steven Whitehouse5c676f62006-02-27 17:23:27 -050055 blks += DIV_ROUND_UP(nstruct - first, second);
David Teiglandb3b94fa2006-01-16 16:50:04 +000056 }
57
58 return blks;
59}
60
61void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
62{
63 struct list_head *head = &sdp->sd_ail1_list;
Steven Whitehousecd915492006-09-04 12:49:07 -040064 u64 sync_gen;
David Teiglandb3b94fa2006-01-16 16:50:04 +000065 struct list_head *first, *tmp;
66 struct gfs2_ail *first_ai, *ai;
67
68 gfs2_log_lock(sdp);
69 if (list_empty(head)) {
70 gfs2_log_unlock(sdp);
71 return;
72 }
73 sync_gen = sdp->sd_ail_sync_gen++;
74
75 first = head->prev;
76 first_ai = list_entry(first, struct gfs2_ail, ai_list);
77 first_ai->ai_sync_gen = sync_gen;
78 gfs2_ail1_start_one(sdp, first_ai);
79
80 if (flags & DIO_ALL)
81 first = NULL;
82
83 for (;;) {
Steven Whitehouse484adff2006-03-29 09:12:12 -050084 if (first && (head->prev != first ||
85 gfs2_ail1_empty_one(sdp, first_ai, 0)))
David Teiglandb3b94fa2006-01-16 16:50:04 +000086 break;
87
88 for (tmp = head->prev; tmp != head; tmp = tmp->prev) {
89 ai = list_entry(tmp, struct gfs2_ail, ai_list);
90 if (ai->ai_sync_gen >= sync_gen)
91 continue;
92 ai->ai_sync_gen = sync_gen;
93 gfs2_ail1_start_one(sdp, ai);
94 break;
95 }
96
97 if (tmp == head)
98 break;
99 }
100
101 gfs2_log_unlock(sdp);
102}
103
104int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
105{
106 struct gfs2_ail *ai, *s;
107 int ret;
108
109 gfs2_log_lock(sdp);
110
111 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
112 if (gfs2_ail1_empty_one(sdp, ai, flags))
113 list_move(&ai->ai_list, &sdp->sd_ail2_list);
114 else if (!(flags & DIO_ALL))
115 break;
116 }
117
118 ret = list_empty(&sdp->sd_ail1_list);
119
120 gfs2_log_unlock(sdp);
121
122 return ret;
123}
124
125static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
126{
127 struct gfs2_ail *ai, *safe;
128 unsigned int old_tail = sdp->sd_log_tail;
129 int wrap = (new_tail < old_tail);
130 int a, b, rm;
131
132 gfs2_log_lock(sdp);
133
134 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
135 a = (old_tail <= ai->ai_first);
136 b = (ai->ai_first < new_tail);
137 rm = (wrap) ? (a || b) : (a && b);
138 if (!rm)
139 continue;
140
141 gfs2_ail2_empty_one(sdp, ai);
142 list_del(&ai->ai_list);
143 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
144 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
145 kfree(ai);
146 }
147
148 gfs2_log_unlock(sdp);
149}
150
151/**
152 * gfs2_log_reserve - Make a log reservation
153 * @sdp: The GFS2 superblock
154 * @blks: The number of blocks to reserve
155 *
156 * Returns: errno
157 */
158
159int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
160{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 unsigned int try = 0;
162
163 if (gfs2_assert_warn(sdp, blks) ||
164 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
165 return -EINVAL;
166
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500167 mutex_lock(&sdp->sd_log_reserve_mutex);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500168 gfs2_log_lock(sdp);
169 while(sdp->sd_log_blks_free <= blks) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000170 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 gfs2_ail1_empty(sdp, 0);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400172 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173
174 if (try++)
175 gfs2_ail1_start(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500176 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 }
Steven Whitehouse484adff2006-03-29 09:12:12 -0500178 sdp->sd_log_blks_free -= blks;
179 gfs2_log_unlock(sdp);
180 mutex_unlock(&sdp->sd_log_reserve_mutex);
181
182 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183
184 return 0;
185}
186
187/**
188 * gfs2_log_release - Release a given number of log blocks
189 * @sdp: The GFS2 superblock
190 * @blks: The number of blocks
191 *
192 */
193
194void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
195{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196
197 gfs2_log_lock(sdp);
198 sdp->sd_log_blks_free += blks;
199 gfs2_assert_withdraw(sdp,
200 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
201 gfs2_log_unlock(sdp);
Steven Whitehouseed386502006-04-07 16:28:07 -0400202 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203}
204
Steven Whitehousecd915492006-09-04 12:49:07 -0400205static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207 int error;
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400208 struct buffer_head bh_map;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400210 error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, 0, &bh_map, 1);
211 if (error || !bh_map.b_blocknr)
212 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, bh_map.b_blocknr, lbn);
213 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000214
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400215 return bh_map.b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216}
217
218/**
219 * log_distance - Compute distance between two journal blocks
220 * @sdp: The GFS2 superblock
221 * @newer: The most recent journal block of the pair
222 * @older: The older journal block of the pair
223 *
224 * Compute the distance (in the journal direction) between two
225 * blocks in the journal
226 *
227 * Returns: the distance in blocks
228 */
229
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400230static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 unsigned int older)
232{
233 int dist;
234
235 dist = newer - older;
236 if (dist < 0)
237 dist += sdp->sd_jdesc->jd_blocks;
238
239 return dist;
240}
241
242static unsigned int current_tail(struct gfs2_sbd *sdp)
243{
244 struct gfs2_ail *ai;
245 unsigned int tail;
246
247 gfs2_log_lock(sdp);
248
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400249 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400251 } else {
252 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253 tail = ai->ai_first;
254 }
255
256 gfs2_log_unlock(sdp);
257
258 return tail;
259}
260
261static inline void log_incr_head(struct gfs2_sbd *sdp)
262{
263 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400264 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265
266 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
267 sdp->sd_log_flush_head = 0;
268 sdp->sd_log_flush_wrapped = 1;
269 }
270}
271
272/**
273 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
274 * @sdp: The GFS2 superblock
275 *
276 * Returns: the buffer_head
277 */
278
279struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
280{
Steven Whitehousecd915492006-09-04 12:49:07 -0400281 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 struct gfs2_log_buf *lb;
283 struct buffer_head *bh;
284
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000285 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000286 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
287
288 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
289 lock_buffer(bh);
290 memset(bh->b_data, 0, bh->b_size);
291 set_buffer_uptodate(bh);
292 clear_buffer_dirty(bh);
293 unlock_buffer(bh);
294
295 log_incr_head(sdp);
296
297 return bh;
298}
299
300/**
301 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
302 * @sdp: the filesystem
303 * @data: the data the buffer_head should point to
304 *
305 * Returns: the log buffer descriptor
306 */
307
308struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
309 struct buffer_head *real)
310{
Steven Whitehousecd915492006-09-04 12:49:07 -0400311 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000312 struct gfs2_log_buf *lb;
313 struct buffer_head *bh;
314
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000315 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
317 lb->lb_real = real;
318
319 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
320 atomic_set(&bh->b_count, 1);
321 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000322 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323 bh->b_blocknr = blkno;
324 bh->b_size = sdp->sd_sb.sb_bsize;
325 bh->b_bdev = sdp->sd_vfs->s_bdev;
326
327 log_incr_head(sdp);
328
329 return bh;
330}
331
332static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
333{
334 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
335
336 ail2_empty(sdp, new_tail);
337
338 gfs2_log_lock(sdp);
Jan Engelhardtc53921242006-09-05 14:30:40 +0200339 sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400340 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 gfs2_log_unlock(sdp);
342
343 sdp->sd_log_tail = new_tail;
344}
345
346/**
347 * log_write_header - Get and initialize a journal header buffer
348 * @sdp: The GFS2 superblock
349 *
350 * Returns: the initialized log buffer descriptor
351 */
352
Steven Whitehousecd915492006-09-04 12:49:07 -0400353static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354{
Steven Whitehousecd915492006-09-04 12:49:07 -0400355 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 struct buffer_head *bh;
357 struct gfs2_log_header *lh;
358 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400359 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361 bh = sb_getblk(sdp->sd_vfs, blkno);
362 lock_buffer(bh);
363 memset(bh->b_data, 0, bh->b_size);
364 set_buffer_uptodate(bh);
365 clear_buffer_dirty(bh);
366 unlock_buffer(bh);
367
368 gfs2_ail1_empty(sdp, 0);
369 tail = current_tail(sdp);
370
371 lh = (struct gfs2_log_header *)bh->b_data;
372 memset(lh, 0, sizeof(struct gfs2_log_header));
373 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500374 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
375 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400376 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
377 lh->lh_flags = cpu_to_be32(flags);
378 lh->lh_tail = cpu_to_be32(tail);
379 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000380 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
381 lh->lh_hash = cpu_to_be32(hash);
382
383 set_buffer_dirty(bh);
384 if (sync_dirty_buffer(bh))
385 gfs2_io_error_bh(sdp, bh);
386 brelse(bh);
387
388 if (sdp->sd_log_tail != tail)
389 log_pull_tail(sdp, tail, pull);
390 else
391 gfs2_assert_withdraw(sdp, !pull);
392
393 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
394 log_incr_head(sdp);
395}
396
397static void log_flush_commit(struct gfs2_sbd *sdp)
398{
399 struct list_head *head = &sdp->sd_log_flush_list;
400 struct gfs2_log_buf *lb;
401 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402
403 while (!list_empty(head)) {
404 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
405 list_del(&lb->lb_list);
406 bh = lb->lb_bh;
407
408 wait_on_buffer(bh);
409 if (!buffer_uptodate(bh))
410 gfs2_io_error_bh(sdp, bh);
411 if (lb->lb_real) {
412 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
413 schedule();
414 free_buffer_head(bh);
415 } else
416 brelse(bh);
417 kfree(lb);
418 }
419
420 log_write_header(sdp, 0, 0);
421}
422
423/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400424 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 * @sdp: the filesystem
426 * @gl: The glock structure to flush. If NULL, flush the whole incore log
427 *
428 */
429
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400430void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431{
432 struct gfs2_ail *ai;
433
Steven Whitehouse484adff2006-03-29 09:12:12 -0500434 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000435
436 if (gl) {
437 gfs2_log_lock(sdp);
438 if (list_empty(&gl->gl_le.le_list)) {
439 gfs2_log_unlock(sdp);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500440 up_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000441 return;
442 }
443 gfs2_log_unlock(sdp);
444 }
445
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400446 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
447 INIT_LIST_HEAD(&ai->ai_ail1_list);
448 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400450 gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451 gfs2_assert_withdraw(sdp,
452 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
453
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454 sdp->sd_log_flush_head = sdp->sd_log_head;
455 sdp->sd_log_flush_wrapped = 0;
456 ai->ai_first = sdp->sd_log_flush_head;
457
458 lops_before_commit(sdp);
459 if (!list_empty(&sdp->sd_log_flush_list))
460 log_flush_commit(sdp);
461 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
462 log_write_header(sdp, 0, PULL);
463 lops_after_commit(sdp, ai);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400465
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400466 sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400468 sdp->sd_log_blks_reserved = 0;
469 sdp->sd_log_commited_buf = 0;
470 sdp->sd_log_num_hdrs = 0;
471 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000472
473 gfs2_log_lock(sdp);
474 if (!list_empty(&ai->ai_ail1_list)) {
475 list_add(&ai->ai_list, &sdp->sd_ail1_list);
476 ai = NULL;
477 }
478 gfs2_log_unlock(sdp);
479
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 sdp->sd_vfs->s_dirt = 0;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500481 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482
483 kfree(ai);
484}
485
486static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
487{
Benjamin Marzinski5dc39fe2006-08-24 14:47:17 -0500488 unsigned int reserved = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 unsigned int old;
490
491 gfs2_log_lock(sdp);
492
493 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
494 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
495 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
496 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
497
498 if (sdp->sd_log_commited_buf)
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400499 reserved += sdp->sd_log_commited_buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000500 if (sdp->sd_log_commited_revoke)
501 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
Steven Whitehousecd915492006-09-04 12:49:07 -0400502 sizeof(u64));
Benjamin Marzinski5dc39fe2006-08-24 14:47:17 -0500503 if (reserved)
504 reserved++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505
506 old = sdp->sd_log_blks_free;
507 sdp->sd_log_blks_free += tr->tr_reserved -
508 (reserved - sdp->sd_log_blks_reserved);
509
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400510 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000511 gfs2_assert_withdraw(sdp,
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400512 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
513 sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000514
515 sdp->sd_log_blks_reserved = reserved;
516
517 gfs2_log_unlock(sdp);
518}
519
520/**
521 * gfs2_log_commit - Commit a transaction to the log
522 * @sdp: the filesystem
523 * @tr: the transaction
524 *
525 * Returns: errno
526 */
527
528void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
529{
530 log_refund(sdp, tr);
531 lops_incore_commit(sdp, tr);
532
533 sdp->sd_vfs->s_dirt = 1;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500534 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 gfs2_log_lock(sdp);
537 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
538 gfs2_log_unlock(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400539 gfs2_log_flush(sdp, NULL);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400540 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 gfs2_log_unlock(sdp);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400542 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543}
544
545/**
546 * gfs2_log_shutdown - write a shutdown header into a journal
547 * @sdp: the filesystem
548 *
549 */
550
551void gfs2_log_shutdown(struct gfs2_sbd *sdp)
552{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500553 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
556 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
557 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000558 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
560 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
561 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400562 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
564
565 sdp->sd_log_flush_head = sdp->sd_log_head;
566 sdp->sd_log_flush_wrapped = 0;
567
568 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
569
Steven Whitehousea74604b2006-04-21 15:10:46 -0400570 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
571 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
572 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573
574 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 sdp->sd_log_tail = sdp->sd_log_head;
576
Steven Whitehouse484adff2006-03-29 09:12:12 -0500577 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578}
579