blob: 72eec6542d6a74adb1eff38ae850d7882e772a8c [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
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040061/**
62 * gfs2_ail1_start_one - Start I/O on a part of the AIL
63 * @sdp: the filesystem
64 * @tr: the part of the AIL
65 *
66 */
67
68static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
69{
70 struct gfs2_bufdata *bd, *s;
71 struct buffer_head *bh;
72 int retry;
73
74 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
75
76 do {
77 retry = 0;
78
79 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
80 bd_ail_st_list) {
81 bh = bd->bd_bh;
82
83 gfs2_assert(sdp, bd->bd_ail == ai);
84
85 if (!buffer_busy(bh)) {
86 if (!buffer_uptodate(bh)) {
87 gfs2_log_unlock(sdp);
88 gfs2_io_error_bh(sdp, bh);
89 gfs2_log_lock(sdp);
90 }
91 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
92 continue;
93 }
94
95 if (!buffer_dirty(bh))
96 continue;
97
98 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
99
100 gfs2_log_unlock(sdp);
101 wait_on_buffer(bh);
102 ll_rw_block(WRITE, 1, &bh);
103 gfs2_log_lock(sdp);
104
105 retry = 1;
106 break;
107 }
108 } while (retry);
109}
110
111/**
112 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
113 * @sdp: the filesystem
114 * @ai: the AIL entry
115 *
116 */
117
118static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
119{
120 struct gfs2_bufdata *bd, *s;
121 struct buffer_head *bh;
122
123 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
124 bd_ail_st_list) {
125 bh = bd->bd_bh;
126
127 gfs2_assert(sdp, bd->bd_ail == ai);
128
129 if (buffer_busy(bh)) {
130 if (flags & DIO_ALL)
131 continue;
132 else
133 break;
134 }
135
136 if (!buffer_uptodate(bh))
137 gfs2_io_error_bh(sdp, bh);
138
139 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
140 }
141
142 return list_empty(&ai->ai_ail1_list);
143}
144
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
146{
147 struct list_head *head = &sdp->sd_ail1_list;
Steven Whitehousecd915492006-09-04 12:49:07 -0400148 u64 sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400149 struct list_head *first;
150 struct gfs2_ail *first_ai, *ai, *tmp;
151 int done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152
153 gfs2_log_lock(sdp);
154 if (list_empty(head)) {
155 gfs2_log_unlock(sdp);
156 return;
157 }
158 sync_gen = sdp->sd_ail_sync_gen++;
159
160 first = head->prev;
161 first_ai = list_entry(first, struct gfs2_ail, ai_list);
162 first_ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400163 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164
165 if (flags & DIO_ALL)
166 first = NULL;
167
Steven Whitehouse74669412006-09-19 11:17:38 -0400168 while(!done) {
Steven Whitehouse484adff2006-03-29 09:12:12 -0500169 if (first && (head->prev != first ||
170 gfs2_ail1_empty_one(sdp, first_ai, 0)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 break;
172
Steven Whitehouse74669412006-09-19 11:17:38 -0400173 done = 1;
174 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 if (ai->ai_sync_gen >= sync_gen)
176 continue;
177 ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400178 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
179 done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180 break;
181 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 }
183
184 gfs2_log_unlock(sdp);
185}
186
187int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
188{
189 struct gfs2_ail *ai, *s;
190 int ret;
191
192 gfs2_log_lock(sdp);
193
194 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
195 if (gfs2_ail1_empty_one(sdp, ai, flags))
196 list_move(&ai->ai_list, &sdp->sd_ail2_list);
197 else if (!(flags & DIO_ALL))
198 break;
199 }
200
201 ret = list_empty(&sdp->sd_ail1_list);
202
203 gfs2_log_unlock(sdp);
204
205 return ret;
206}
207
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400208
209/**
210 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
211 * @sdp: the filesystem
212 * @ai: the AIL entry
213 *
214 */
215
216static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
217{
218 struct list_head *head = &ai->ai_ail2_list;
219 struct gfs2_bufdata *bd;
220
221 while (!list_empty(head)) {
222 bd = list_entry(head->prev, struct gfs2_bufdata,
223 bd_ail_st_list);
224 gfs2_assert(sdp, bd->bd_ail == ai);
225 bd->bd_ail = NULL;
226 list_del(&bd->bd_ail_st_list);
227 list_del(&bd->bd_ail_gl_list);
228 atomic_dec(&bd->bd_gl->gl_ail_count);
229 brelse(bd->bd_bh);
230 }
231}
232
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
234{
235 struct gfs2_ail *ai, *safe;
236 unsigned int old_tail = sdp->sd_log_tail;
237 int wrap = (new_tail < old_tail);
238 int a, b, rm;
239
240 gfs2_log_lock(sdp);
241
242 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
243 a = (old_tail <= ai->ai_first);
244 b = (ai->ai_first < new_tail);
245 rm = (wrap) ? (a || b) : (a && b);
246 if (!rm)
247 continue;
248
249 gfs2_ail2_empty_one(sdp, ai);
250 list_del(&ai->ai_list);
251 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
252 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
253 kfree(ai);
254 }
255
256 gfs2_log_unlock(sdp);
257}
258
259/**
260 * gfs2_log_reserve - Make a log reservation
261 * @sdp: The GFS2 superblock
262 * @blks: The number of blocks to reserve
263 *
264 * Returns: errno
265 */
266
267int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
268{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269 unsigned int try = 0;
270
271 if (gfs2_assert_warn(sdp, blks) ||
272 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
273 return -EINVAL;
274
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500275 mutex_lock(&sdp->sd_log_reserve_mutex);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500276 gfs2_log_lock(sdp);
277 while(sdp->sd_log_blks_free <= blks) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279 gfs2_ail1_empty(sdp, 0);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400280 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281
282 if (try++)
283 gfs2_ail1_start(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500284 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285 }
Steven Whitehouse484adff2006-03-29 09:12:12 -0500286 sdp->sd_log_blks_free -= blks;
287 gfs2_log_unlock(sdp);
288 mutex_unlock(&sdp->sd_log_reserve_mutex);
289
290 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000291
292 return 0;
293}
294
295/**
296 * gfs2_log_release - Release a given number of log blocks
297 * @sdp: The GFS2 superblock
298 * @blks: The number of blocks
299 *
300 */
301
302void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
303{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304
305 gfs2_log_lock(sdp);
306 sdp->sd_log_blks_free += blks;
307 gfs2_assert_withdraw(sdp,
308 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
309 gfs2_log_unlock(sdp);
Steven Whitehouseed386502006-04-07 16:28:07 -0400310 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311}
312
Steven Whitehousecd915492006-09-04 12:49:07 -0400313static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315 int error;
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400316 struct buffer_head bh_map;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400318 error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, 0, &bh_map, 1);
319 if (error || !bh_map.b_blocknr)
320 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, bh_map.b_blocknr, lbn);
321 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400323 return bh_map.b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000324}
325
326/**
327 * log_distance - Compute distance between two journal blocks
328 * @sdp: The GFS2 superblock
329 * @newer: The most recent journal block of the pair
330 * @older: The older journal block of the pair
331 *
332 * Compute the distance (in the journal direction) between two
333 * blocks in the journal
334 *
335 * Returns: the distance in blocks
336 */
337
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400338static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339 unsigned int older)
340{
341 int dist;
342
343 dist = newer - older;
344 if (dist < 0)
345 dist += sdp->sd_jdesc->jd_blocks;
346
347 return dist;
348}
349
350static unsigned int current_tail(struct gfs2_sbd *sdp)
351{
352 struct gfs2_ail *ai;
353 unsigned int tail;
354
355 gfs2_log_lock(sdp);
356
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400357 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400359 } else {
360 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361 tail = ai->ai_first;
362 }
363
364 gfs2_log_unlock(sdp);
365
366 return tail;
367}
368
369static inline void log_incr_head(struct gfs2_sbd *sdp)
370{
371 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400372 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373
374 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
375 sdp->sd_log_flush_head = 0;
376 sdp->sd_log_flush_wrapped = 1;
377 }
378}
379
380/**
381 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
382 * @sdp: The GFS2 superblock
383 *
384 * Returns: the buffer_head
385 */
386
387struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
388{
Steven Whitehousecd915492006-09-04 12:49:07 -0400389 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390 struct gfs2_log_buf *lb;
391 struct buffer_head *bh;
392
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000393 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
395
396 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
397 lock_buffer(bh);
398 memset(bh->b_data, 0, bh->b_size);
399 set_buffer_uptodate(bh);
400 clear_buffer_dirty(bh);
401 unlock_buffer(bh);
402
403 log_incr_head(sdp);
404
405 return bh;
406}
407
408/**
409 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
410 * @sdp: the filesystem
411 * @data: the data the buffer_head should point to
412 *
413 * Returns: the log buffer descriptor
414 */
415
416struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
417 struct buffer_head *real)
418{
Steven Whitehousecd915492006-09-04 12:49:07 -0400419 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 struct gfs2_log_buf *lb;
421 struct buffer_head *bh;
422
Steven Whitehouse4f3df042006-01-18 13:20:16 +0000423 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
425 lb->lb_real = real;
426
427 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
428 atomic_set(&bh->b_count, 1);
429 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000430 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431 bh->b_blocknr = blkno;
432 bh->b_size = sdp->sd_sb.sb_bsize;
433 bh->b_bdev = sdp->sd_vfs->s_bdev;
434
435 log_incr_head(sdp);
436
437 return bh;
438}
439
440static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
441{
442 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
443
444 ail2_empty(sdp, new_tail);
445
446 gfs2_log_lock(sdp);
Jan Engelhardtc53921242006-09-05 14:30:40 +0200447 sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400448 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449 gfs2_log_unlock(sdp);
450
451 sdp->sd_log_tail = new_tail;
452}
453
454/**
455 * log_write_header - Get and initialize a journal header buffer
456 * @sdp: The GFS2 superblock
457 *
458 * Returns: the initialized log buffer descriptor
459 */
460
Steven Whitehousecd915492006-09-04 12:49:07 -0400461static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462{
Steven Whitehousecd915492006-09-04 12:49:07 -0400463 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464 struct buffer_head *bh;
465 struct gfs2_log_header *lh;
466 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400467 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 bh = sb_getblk(sdp->sd_vfs, blkno);
470 lock_buffer(bh);
471 memset(bh->b_data, 0, bh->b_size);
472 set_buffer_uptodate(bh);
473 clear_buffer_dirty(bh);
474 unlock_buffer(bh);
475
476 gfs2_ail1_empty(sdp, 0);
477 tail = current_tail(sdp);
478
479 lh = (struct gfs2_log_header *)bh->b_data;
480 memset(lh, 0, sizeof(struct gfs2_log_header));
481 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500482 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
483 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400484 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
485 lh->lh_flags = cpu_to_be32(flags);
486 lh->lh_tail = cpu_to_be32(tail);
487 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
489 lh->lh_hash = cpu_to_be32(hash);
490
491 set_buffer_dirty(bh);
492 if (sync_dirty_buffer(bh))
493 gfs2_io_error_bh(sdp, bh);
494 brelse(bh);
495
496 if (sdp->sd_log_tail != tail)
497 log_pull_tail(sdp, tail, pull);
498 else
499 gfs2_assert_withdraw(sdp, !pull);
500
501 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
502 log_incr_head(sdp);
503}
504
505static void log_flush_commit(struct gfs2_sbd *sdp)
506{
507 struct list_head *head = &sdp->sd_log_flush_list;
508 struct gfs2_log_buf *lb;
509 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510
511 while (!list_empty(head)) {
512 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
513 list_del(&lb->lb_list);
514 bh = lb->lb_bh;
515
516 wait_on_buffer(bh);
517 if (!buffer_uptodate(bh))
518 gfs2_io_error_bh(sdp, bh);
519 if (lb->lb_real) {
520 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
521 schedule();
522 free_buffer_head(bh);
523 } else
524 brelse(bh);
525 kfree(lb);
526 }
527
528 log_write_header(sdp, 0, 0);
529}
530
531/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400532 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533 * @sdp: the filesystem
534 * @gl: The glock structure to flush. If NULL, flush the whole incore log
535 *
536 */
537
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400538void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539{
540 struct gfs2_ail *ai;
541
Steven Whitehouse484adff2006-03-29 09:12:12 -0500542 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000543
544 if (gl) {
545 gfs2_log_lock(sdp);
546 if (list_empty(&gl->gl_le.le_list)) {
547 gfs2_log_unlock(sdp);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500548 up_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000549 return;
550 }
551 gfs2_log_unlock(sdp);
552 }
553
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400554 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
555 INIT_LIST_HEAD(&ai->ai_ail1_list);
556 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400558 gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559 gfs2_assert_withdraw(sdp,
560 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
561
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 sdp->sd_log_flush_head = sdp->sd_log_head;
563 sdp->sd_log_flush_wrapped = 0;
564 ai->ai_first = sdp->sd_log_flush_head;
565
566 lops_before_commit(sdp);
567 if (!list_empty(&sdp->sd_log_flush_list))
568 log_flush_commit(sdp);
569 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
570 log_write_header(sdp, 0, PULL);
571 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400572
573 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400575 sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400576 sdp->sd_log_blks_reserved = 0;
577 sdp->sd_log_commited_buf = 0;
578 sdp->sd_log_num_hdrs = 0;
579 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581 if (!list_empty(&ai->ai_ail1_list)) {
582 list_add(&ai->ai_list, &sdp->sd_ail1_list);
583 ai = NULL;
584 }
585 gfs2_log_unlock(sdp);
586
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 sdp->sd_vfs->s_dirt = 0;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500588 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589
590 kfree(ai);
591}
592
593static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
594{
Benjamin Marzinski5dc39fe2006-08-24 14:47:17 -0500595 unsigned int reserved = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 unsigned int old;
597
598 gfs2_log_lock(sdp);
599
600 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
601 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
602 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
603 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
604
605 if (sdp->sd_log_commited_buf)
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400606 reserved += sdp->sd_log_commited_buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 if (sdp->sd_log_commited_revoke)
608 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
Steven Whitehousecd915492006-09-04 12:49:07 -0400609 sizeof(u64));
Benjamin Marzinski5dc39fe2006-08-24 14:47:17 -0500610 if (reserved)
611 reserved++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612
613 old = sdp->sd_log_blks_free;
614 sdp->sd_log_blks_free += tr->tr_reserved -
615 (reserved - sdp->sd_log_blks_reserved);
616
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400617 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618 gfs2_assert_withdraw(sdp,
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400619 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
620 sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621
622 sdp->sd_log_blks_reserved = reserved;
623
624 gfs2_log_unlock(sdp);
625}
626
627/**
628 * gfs2_log_commit - Commit a transaction to the log
629 * @sdp: the filesystem
630 * @tr: the transaction
631 *
632 * Returns: errno
633 */
634
635void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
636{
637 log_refund(sdp, tr);
638 lops_incore_commit(sdp, tr);
639
640 sdp->sd_vfs->s_dirt = 1;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500641 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643 gfs2_log_lock(sdp);
644 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
645 gfs2_log_unlock(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400646 gfs2_log_flush(sdp, NULL);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400647 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000648 gfs2_log_unlock(sdp);
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400649 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650}
651
652/**
653 * gfs2_log_shutdown - write a shutdown header into a journal
654 * @sdp: the filesystem
655 *
656 */
657
658void gfs2_log_shutdown(struct gfs2_sbd *sdp)
659{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500660 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
663 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
664 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000665 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
667 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
668 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400669 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
671
672 sdp->sd_log_flush_head = sdp->sd_log_head;
673 sdp->sd_log_flush_wrapped = 0;
674
675 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
676
Steven Whitehousea74604b2006-04-21 15:10:46 -0400677 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
678 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
679 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680
681 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682 sdp->sd_log_tail = sdp->sd_log_head;
683
Steven Whitehouse484adff2006-03-29 09:12:12 -0500684 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685}
686