blob: 6112d648b7ee1198cee9d5f350514b0fdcaa64b3 [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000018
19#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#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;
Steven Whitehouse74669412006-09-19 11:17:38 -040065 struct list_head *first;
66 struct gfs2_ail *first_ai, *ai, *tmp;
67 int done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +000068
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;
Steven Whitehouse74669412006-09-19 11:17:38 -040079 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
David Teiglandb3b94fa2006-01-16 16:50:04 +000080
81 if (flags & DIO_ALL)
82 first = NULL;
83
Steven Whitehouse74669412006-09-19 11:17:38 -040084 while(!done) {
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
Steven Whitehouse74669412006-09-19 11:17:38 -040089 done = 1;
90 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000091 if (ai->ai_sync_gen >= sync_gen)
92 continue;
93 ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -040094 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
95 done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +000096 break;
97 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000098 }
99
100 gfs2_log_unlock(sdp);
101}
102
103int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
104{
105 struct gfs2_ail *ai, *s;
106 int ret;
107
108 gfs2_log_lock(sdp);
109
110 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
111 if (gfs2_ail1_empty_one(sdp, ai, flags))
112 list_move(&ai->ai_list, &sdp->sd_ail2_list);
113 else if (!(flags & DIO_ALL))
114 break;
115 }
116
117 ret = list_empty(&sdp->sd_ail1_list);
118
119 gfs2_log_unlock(sdp);
120
121 return ret;
122}
123
124static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
125{
126 struct gfs2_ail *ai, *safe;
127 unsigned int old_tail = sdp->sd_log_tail;
128 int wrap = (new_tail < old_tail);
129 int a, b, rm;
130
131 gfs2_log_lock(sdp);
132
133 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
134 a = (old_tail <= ai->ai_first);
135 b = (ai->ai_first < new_tail);
136 rm = (wrap) ? (a || b) : (a && b);
137 if (!rm)
138 continue;
139
140 gfs2_ail2_empty_one(sdp, ai);
141 list_del(&ai->ai_list);
142 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
143 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
144 kfree(ai);
145 }
146
147 gfs2_log_unlock(sdp);
148}
149
150/**
151 * gfs2_log_reserve - Make a log reservation
152 * @sdp: The GFS2 superblock
153 * @blks: The number of blocks to reserve
154 *
155 * Returns: errno
156 */
157
158int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
159{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 unsigned int try = 0;
161
162 if (gfs2_assert_warn(sdp, blks) ||
163 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
164 return -EINVAL;
165
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500166 mutex_lock(&sdp->sd_log_reserve_mutex);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500167 gfs2_log_lock(sdp);
168 while(sdp->sd_log_blks_free <= blks) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000170 gfs2_ail1_empty(sdp, 0);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400171 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172
173 if (try++)
174 gfs2_ail1_start(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500175 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 }
Steven Whitehouse484adff2006-03-29 09:12:12 -0500177 sdp->sd_log_blks_free -= blks;
178 gfs2_log_unlock(sdp);
179 mutex_unlock(&sdp->sd_log_reserve_mutex);
180
181 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182
183 return 0;
184}
185
186/**
187 * gfs2_log_release - Release a given number of log blocks
188 * @sdp: The GFS2 superblock
189 * @blks: The number of blocks
190 *
191 */
192
193void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
194{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195
196 gfs2_log_lock(sdp);
197 sdp->sd_log_blks_free += blks;
198 gfs2_assert_withdraw(sdp,
199 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
200 gfs2_log_unlock(sdp);
Steven Whitehouseed386502006-04-07 16:28:07 -0400201 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202}
203
Steven Whitehousecd915492006-09-04 12:49:07 -0400204static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000205{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 int error;
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400207 struct buffer_head bh_map;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400209 error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, 0, &bh_map, 1);
210 if (error || !bh_map.b_blocknr)
211 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, bh_map.b_blocknr, lbn);
212 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400214 return bh_map.b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215}
216
217/**
218 * log_distance - Compute distance between two journal blocks
219 * @sdp: The GFS2 superblock
220 * @newer: The most recent journal block of the pair
221 * @older: The older journal block of the pair
222 *
223 * Compute the distance (in the journal direction) between two
224 * blocks in the journal
225 *
226 * Returns: the distance in blocks
227 */
228
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400229static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 unsigned int older)
231{
232 int dist;
233
234 dist = newer - older;
235 if (dist < 0)
236 dist += sdp->sd_jdesc->jd_blocks;
237
238 return dist;
239}
240
241static unsigned int current_tail(struct gfs2_sbd *sdp)
242{
243 struct gfs2_ail *ai;
244 unsigned int tail;
245
246 gfs2_log_lock(sdp);
247
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400248 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400250 } else {
251 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252 tail = ai->ai_first;
253 }
254
255 gfs2_log_unlock(sdp);
256
257 return tail;
258}
259
260static inline void log_incr_head(struct gfs2_sbd *sdp)
261{
262 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400263 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000264
265 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
266 sdp->sd_log_flush_head = 0;
267 sdp->sd_log_flush_wrapped = 1;
268 }
269}
270
271/**
272 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
273 * @sdp: The GFS2 superblock
274 *
275 * Returns: the buffer_head
276 */
277
278struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
279{
Steven Whitehousecd915492006-09-04 12:49:07 -0400280 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281 struct gfs2_log_buf *lb;
282 struct buffer_head *bh;
283
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000284 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
286
287 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
288 lock_buffer(bh);
289 memset(bh->b_data, 0, bh->b_size);
290 set_buffer_uptodate(bh);
291 clear_buffer_dirty(bh);
292 unlock_buffer(bh);
293
294 log_incr_head(sdp);
295
296 return bh;
297}
298
299/**
300 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
301 * @sdp: the filesystem
302 * @data: the data the buffer_head should point to
303 *
304 * Returns: the log buffer descriptor
305 */
306
307struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
308 struct buffer_head *real)
309{
Steven Whitehousecd915492006-09-04 12:49:07 -0400310 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311 struct gfs2_log_buf *lb;
312 struct buffer_head *bh;
313
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000314 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
316 lb->lb_real = real;
317
318 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
319 atomic_set(&bh->b_count, 1);
320 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000321 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322 bh->b_blocknr = blkno;
323 bh->b_size = sdp->sd_sb.sb_bsize;
324 bh->b_bdev = sdp->sd_vfs->s_bdev;
325
326 log_incr_head(sdp);
327
328 return bh;
329}
330
331static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
332{
333 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
334
335 ail2_empty(sdp, new_tail);
336
337 gfs2_log_lock(sdp);
Jan Engelhardtc53921242006-09-05 14:30:40 +0200338 sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400339 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340 gfs2_log_unlock(sdp);
341
342 sdp->sd_log_tail = new_tail;
343}
344
345/**
346 * log_write_header - Get and initialize a journal header buffer
347 * @sdp: The GFS2 superblock
348 *
349 * Returns: the initialized log buffer descriptor
350 */
351
Steven Whitehousecd915492006-09-04 12:49:07 -0400352static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353{
Steven Whitehousecd915492006-09-04 12:49:07 -0400354 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000355 struct buffer_head *bh;
356 struct gfs2_log_header *lh;
357 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400358 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000359
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360 bh = sb_getblk(sdp->sd_vfs, blkno);
361 lock_buffer(bh);
362 memset(bh->b_data, 0, bh->b_size);
363 set_buffer_uptodate(bh);
364 clear_buffer_dirty(bh);
365 unlock_buffer(bh);
366
367 gfs2_ail1_empty(sdp, 0);
368 tail = current_tail(sdp);
369
370 lh = (struct gfs2_log_header *)bh->b_data;
371 memset(lh, 0, sizeof(struct gfs2_log_header));
372 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500373 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
374 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400375 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
376 lh->lh_flags = cpu_to_be32(flags);
377 lh->lh_tail = cpu_to_be32(tail);
378 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000379 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
380 lh->lh_hash = cpu_to_be32(hash);
381
382 set_buffer_dirty(bh);
383 if (sync_dirty_buffer(bh))
384 gfs2_io_error_bh(sdp, bh);
385 brelse(bh);
386
387 if (sdp->sd_log_tail != tail)
388 log_pull_tail(sdp, tail, pull);
389 else
390 gfs2_assert_withdraw(sdp, !pull);
391
392 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
393 log_incr_head(sdp);
394}
395
396static void log_flush_commit(struct gfs2_sbd *sdp)
397{
398 struct list_head *head = &sdp->sd_log_flush_list;
399 struct gfs2_log_buf *lb;
400 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000401
402 while (!list_empty(head)) {
403 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
404 list_del(&lb->lb_list);
405 bh = lb->lb_bh;
406
407 wait_on_buffer(bh);
408 if (!buffer_uptodate(bh))
409 gfs2_io_error_bh(sdp, bh);
410 if (lb->lb_real) {
411 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
412 schedule();
413 free_buffer_head(bh);
414 } else
415 brelse(bh);
416 kfree(lb);
417 }
418
419 log_write_header(sdp, 0, 0);
420}
421
422/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400423 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424 * @sdp: the filesystem
425 * @gl: The glock structure to flush. If NULL, flush the whole incore log
426 *
427 */
428
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400429void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430{
431 struct gfs2_ail *ai;
432
Steven Whitehouse484adff2006-03-29 09:12:12 -0500433 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000434
435 if (gl) {
436 gfs2_log_lock(sdp);
437 if (list_empty(&gl->gl_le.le_list)) {
438 gfs2_log_unlock(sdp);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500439 up_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000440 return;
441 }
442 gfs2_log_unlock(sdp);
443 }
444
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400445 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
446 INIT_LIST_HEAD(&ai->ai_ail1_list);
447 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400449 gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000450 gfs2_assert_withdraw(sdp,
451 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
452
David Teiglandb3b94fa2006-01-16 16:50:04 +0000453 sdp->sd_log_flush_head = sdp->sd_log_head;
454 sdp->sd_log_flush_wrapped = 0;
455 ai->ai_first = sdp->sd_log_flush_head;
456
457 lops_before_commit(sdp);
458 if (!list_empty(&sdp->sd_log_flush_list))
459 log_flush_commit(sdp);
460 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
461 log_write_header(sdp, 0, PULL);
462 lops_after_commit(sdp, ai);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000463 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400464
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400465 sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000466
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400467 sdp->sd_log_blks_reserved = 0;
468 sdp->sd_log_commited_buf = 0;
469 sdp->sd_log_num_hdrs = 0;
470 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471
472 gfs2_log_lock(sdp);
473 if (!list_empty(&ai->ai_ail1_list)) {
474 list_add(&ai->ai_list, &sdp->sd_ail1_list);
475 ai = NULL;
476 }
477 gfs2_log_unlock(sdp);
478
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479 sdp->sd_vfs->s_dirt = 0;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500480 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481
482 kfree(ai);
483}
484
485static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
486{
Benjamin Marzinski5dc39fe2006-08-24 14:47:17 -0500487 unsigned int reserved = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488 unsigned int old;
489
490 gfs2_log_lock(sdp);
491
492 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
493 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
494 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
495 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
496
497 if (sdp->sd_log_commited_buf)
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400498 reserved += sdp->sd_log_commited_buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 if (sdp->sd_log_commited_revoke)
500 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
Steven Whitehousecd915492006-09-04 12:49:07 -0400501 sizeof(u64));
Benjamin Marzinski5dc39fe2006-08-24 14:47:17 -0500502 if (reserved)
503 reserved++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000504
505 old = sdp->sd_log_blks_free;
506 sdp->sd_log_blks_free += tr->tr_reserved -
507 (reserved - sdp->sd_log_blks_reserved);
508
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400509 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510 gfs2_assert_withdraw(sdp,
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400511 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
512 sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513
514 sdp->sd_log_blks_reserved = reserved;
515
516 gfs2_log_unlock(sdp);
517}
518
519/**
520 * gfs2_log_commit - Commit a transaction to the log
521 * @sdp: the filesystem
522 * @tr: the transaction
523 *
524 * Returns: errno
525 */
526
527void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
528{
529 log_refund(sdp, tr);
530 lops_incore_commit(sdp, tr);
531
532 sdp->sd_vfs->s_dirt = 1;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500533 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535 gfs2_log_lock(sdp);
536 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
537 gfs2_log_unlock(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400538 gfs2_log_flush(sdp, NULL);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400539 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 gfs2_log_unlock(sdp);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400541 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000542}
543
544/**
545 * gfs2_log_shutdown - write a shutdown header into a journal
546 * @sdp: the filesystem
547 *
548 */
549
550void gfs2_log_shutdown(struct gfs2_sbd *sdp)
551{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500552 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
555 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
556 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000557 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
559 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
560 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400561 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
563
564 sdp->sd_log_flush_head = sdp->sd_log_head;
565 sdp->sd_log_flush_wrapped = 0;
566
567 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
568
Steven Whitehousea74604b2006-04-21 15:10:46 -0400569 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
570 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
571 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572
573 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 sdp->sd_log_tail = sdp->sd_log_head;
575
Steven Whitehouse484adff2006-03-29 09:12:12 -0500576 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577}
578