blob: 9192398408f239db4845e12e23c39e5141af87ba [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>
Steven Whitehousea25311c2006-11-23 11:06:35 -050018#include <linux/delay.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000019
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "bmap.h"
23#include "glock.h"
24#include "log.h"
25#include "lops.h"
26#include "meta_io.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050027#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050028#include "dir.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
30#define PULL 1
31
David Teiglandb3b94fa2006-01-16 16:50:04 +000032/**
33 * gfs2_struct2blk - compute stuff
34 * @sdp: the filesystem
35 * @nstruct: the number of structures
36 * @ssize: the size of the structures
37 *
38 * Compute the number of log descriptor blocks needed to hold a certain number
39 * of structures of a certain size.
40 *
41 * Returns: the number of blocks needed (minimum is always 1)
42 */
43
44unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
45 unsigned int ssize)
46{
47 unsigned int blks;
48 unsigned int first, second;
49
50 blks = 1;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -040051 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / 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
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040062/**
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010063 * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
64 * @mapping: The associated mapping (maybe NULL)
65 * @bd: The gfs2_bufdata to remove
66 *
67 * The log lock _must_ be held when calling this function
68 *
69 */
70
Steven Whitehousef91a0d32007-10-15 16:29:05 +010071void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010072{
73 bd->bd_ail = NULL;
Steven Whitehouse1ad38c42007-09-03 11:01:33 +010074 list_del_init(&bd->bd_ail_st_list);
75 list_del_init(&bd->bd_ail_gl_list);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010076 atomic_dec(&bd->bd_gl->gl_ail_count);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +010077 brelse(bd->bd_bh);
78}
79
80/**
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040081 * gfs2_ail1_start_one - Start I/O on a part of the AIL
82 * @sdp: the filesystem
83 * @tr: the part of the AIL
84 *
85 */
86
87static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
88{
89 struct gfs2_bufdata *bd, *s;
90 struct buffer_head *bh;
91 int retry;
92
93 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
94
95 do {
96 retry = 0;
97
98 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
99 bd_ail_st_list) {
100 bh = bd->bd_bh;
101
102 gfs2_assert(sdp, bd->bd_ail == ai);
103
104 if (!buffer_busy(bh)) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100105 if (!buffer_uptodate(bh))
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400106 gfs2_io_error_bh(sdp, bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400107 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
108 continue;
109 }
110
111 if (!buffer_dirty(bh))
112 continue;
113
114 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
115
Steven Whitehouse16615be2007-09-17 10:59:52 +0100116 get_bh(bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400117 gfs2_log_unlock(sdp);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100118 lock_buffer(bh);
119 if (test_clear_buffer_dirty(bh)) {
120 bh->b_end_io = end_buffer_write_sync;
121 submit_bh(WRITE, bh);
122 } else {
123 unlock_buffer(bh);
124 brelse(bh);
125 }
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400126 gfs2_log_lock(sdp);
127
128 retry = 1;
129 break;
130 }
131 } while (retry);
132}
133
134/**
135 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
136 * @sdp: the filesystem
137 * @ai: the AIL entry
138 *
139 */
140
141static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
142{
143 struct gfs2_bufdata *bd, *s;
144 struct buffer_head *bh;
145
146 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
147 bd_ail_st_list) {
148 bh = bd->bd_bh;
149
150 gfs2_assert(sdp, bd->bd_ail == ai);
151
152 if (buffer_busy(bh)) {
153 if (flags & DIO_ALL)
154 continue;
155 else
156 break;
157 }
158
159 if (!buffer_uptodate(bh))
160 gfs2_io_error_bh(sdp, bh);
161
162 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
163 }
164
165 return list_empty(&ai->ai_ail1_list);
166}
167
Steven Whitehousea25311c2006-11-23 11:06:35 -0500168static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169{
Bob Peterson693ddea2007-07-24 14:07:33 -0500170 struct list_head *head;
Steven Whitehousecd915492006-09-04 12:49:07 -0400171 u64 sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400172 struct list_head *first;
173 struct gfs2_ail *first_ai, *ai, *tmp;
174 int done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175
176 gfs2_log_lock(sdp);
Bob Peterson693ddea2007-07-24 14:07:33 -0500177 head = &sdp->sd_ail1_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178 if (list_empty(head)) {
179 gfs2_log_unlock(sdp);
180 return;
181 }
182 sync_gen = sdp->sd_ail_sync_gen++;
183
184 first = head->prev;
185 first_ai = list_entry(first, struct gfs2_ail, ai_list);
186 first_ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400187 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188
189 if (flags & DIO_ALL)
190 first = NULL;
191
Steven Whitehouse74669412006-09-19 11:17:38 -0400192 while(!done) {
Steven Whitehouse484adff2006-03-29 09:12:12 -0500193 if (first && (head->prev != first ||
194 gfs2_ail1_empty_one(sdp, first_ai, 0)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195 break;
196
Steven Whitehouse74669412006-09-19 11:17:38 -0400197 done = 1;
198 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199 if (ai->ai_sync_gen >= sync_gen)
200 continue;
201 ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400202 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
203 done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204 break;
205 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 }
207
208 gfs2_log_unlock(sdp);
209}
210
211int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
212{
213 struct gfs2_ail *ai, *s;
214 int ret;
215
216 gfs2_log_lock(sdp);
217
218 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
219 if (gfs2_ail1_empty_one(sdp, ai, flags))
220 list_move(&ai->ai_list, &sdp->sd_ail2_list);
221 else if (!(flags & DIO_ALL))
222 break;
223 }
224
225 ret = list_empty(&sdp->sd_ail1_list);
226
227 gfs2_log_unlock(sdp);
228
229 return ret;
230}
231
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400232
233/**
234 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
235 * @sdp: the filesystem
236 * @ai: the AIL entry
237 *
238 */
239
240static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
241{
242 struct list_head *head = &ai->ai_ail2_list;
243 struct gfs2_bufdata *bd;
244
245 while (!list_empty(head)) {
246 bd = list_entry(head->prev, struct gfs2_bufdata,
247 bd_ail_st_list);
248 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100249 gfs2_remove_from_ail(bd);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400250 }
251}
252
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
254{
255 struct gfs2_ail *ai, *safe;
256 unsigned int old_tail = sdp->sd_log_tail;
257 int wrap = (new_tail < old_tail);
258 int a, b, rm;
259
260 gfs2_log_lock(sdp);
261
262 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
263 a = (old_tail <= ai->ai_first);
264 b = (ai->ai_first < new_tail);
265 rm = (wrap) ? (a || b) : (a && b);
266 if (!rm)
267 continue;
268
269 gfs2_ail2_empty_one(sdp, ai);
270 list_del(&ai->ai_list);
271 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
272 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
273 kfree(ai);
274 }
275
276 gfs2_log_unlock(sdp);
277}
278
279/**
280 * gfs2_log_reserve - Make a log reservation
281 * @sdp: The GFS2 superblock
282 * @blks: The number of blocks to reserve
283 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100284 * Note that we never give out the last few blocks of the journal. Thats
Robert Peterson2332c442007-06-18 14:50:20 -0500285 * due to the fact that there is a small number of header blocks
Steven Whitehouseb0041572006-11-23 10:51:34 -0500286 * associated with each log flush. The exact number can't be known until
287 * flush time, so we ensure that we have just enough free blocks at all
288 * times to avoid running out during a log flush.
289 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290 * Returns: errno
291 */
292
293int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
294{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295 unsigned int try = 0;
Steven Whitehouse89918642007-06-01 15:19:33 +0100296 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297
298 if (gfs2_assert_warn(sdp, blks) ||
299 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
300 return -EINVAL;
301
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500302 mutex_lock(&sdp->sd_log_reserve_mutex);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500303 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000304 while(atomic_read(&sdp->sd_log_blks_free) <= (blks + reserved_blks)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306 gfs2_ail1_empty(sdp, 0);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400307 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308
309 if (try++)
310 gfs2_ail1_start(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500311 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000312 }
Steven Whitehousefd041f02007-11-08 14:55:03 +0000313 atomic_sub(blks, &sdp->sd_log_blks_free);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500314 gfs2_log_unlock(sdp);
315 mutex_unlock(&sdp->sd_log_reserve_mutex);
316
317 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318
319 return 0;
320}
321
322/**
323 * gfs2_log_release - Release a given number of log blocks
324 * @sdp: The GFS2 superblock
325 * @blks: The number of blocks
326 *
327 */
328
329void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
330{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331
332 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000333 atomic_add(blks, &sdp->sd_log_blks_free);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334 gfs2_assert_withdraw(sdp,
Steven Whitehousefd041f02007-11-08 14:55:03 +0000335 atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336 gfs2_log_unlock(sdp);
Steven Whitehouseed386502006-04-07 16:28:07 -0400337 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338}
339
Steven Whitehousecd915492006-09-04 12:49:07 -0400340static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341{
Steven Whitehouse23591252006-10-13 17:25:45 -0400342 struct inode *inode = sdp->sd_jdesc->jd_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400344 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345
Steven Whitehouse23591252006-10-13 17:25:45 -0400346 bh_map.b_size = 1 << inode->i_blkbits;
347 error = gfs2_block_map(inode, lbn, 0, &bh_map);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400348 if (error || !bh_map.b_blocknr)
Ryusuke Konishiaed32552006-11-28 02:53:22 +0900349 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error,
350 (unsigned long long)bh_map.b_blocknr, lbn);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400351 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400353 return bh_map.b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354}
355
356/**
357 * log_distance - Compute distance between two journal blocks
358 * @sdp: The GFS2 superblock
359 * @newer: The most recent journal block of the pair
360 * @older: The older journal block of the pair
361 *
362 * Compute the distance (in the journal direction) between two
363 * blocks in the journal
364 *
365 * Returns: the distance in blocks
366 */
367
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400368static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 unsigned int older)
370{
371 int dist;
372
373 dist = newer - older;
374 if (dist < 0)
375 dist += sdp->sd_jdesc->jd_blocks;
376
377 return dist;
378}
379
Robert Peterson2332c442007-06-18 14:50:20 -0500380/**
381 * calc_reserved - Calculate the number of blocks to reserve when
382 * refunding a transaction's unused buffers.
383 * @sdp: The GFS2 superblock
384 *
385 * This is complex. We need to reserve room for all our currently used
386 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
387 * all our journaled data buffers for journaled files (e.g. files in the
388 * meta_fs like rindex, or files for which chattr +j was done.)
389 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
390 * will count it as free space (sd_log_blks_free) and corruption will follow.
391 *
392 * We can have metadata bufs and jdata bufs in the same journal. So each
393 * type gets its own log header, for which we need to reserve a block.
394 * In fact, each type has the potential for needing more than one header
395 * in cases where we have more buffers than will fit on a journal page.
396 * Metadata journal entries take up half the space of journaled buffer entries.
397 * Thus, metadata entries have buf_limit (502) and journaled buffers have
398 * databuf_limit (251) before they cause a wrap around.
399 *
400 * Also, we need to reserve blocks for revoke journal entries and one for an
401 * overall header for the lot.
402 *
403 * Returns: the number of blocks reserved
404 */
405static unsigned int calc_reserved(struct gfs2_sbd *sdp)
406{
407 unsigned int reserved = 0;
408 unsigned int mbuf_limit, metabufhdrs_needed;
409 unsigned int dbuf_limit, databufhdrs_needed;
410 unsigned int revokes = 0;
411
412 mbuf_limit = buf_limit(sdp);
413 metabufhdrs_needed = (sdp->sd_log_commited_buf +
414 (mbuf_limit - 1)) / mbuf_limit;
415 dbuf_limit = databuf_limit(sdp);
416 databufhdrs_needed = (sdp->sd_log_commited_databuf +
417 (dbuf_limit - 1)) / dbuf_limit;
418
419 if (sdp->sd_log_commited_revoke)
420 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
421 sizeof(u64));
422
423 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
424 sdp->sd_log_commited_databuf + databufhdrs_needed +
425 revokes;
426 /* One for the overall header */
427 if (reserved)
428 reserved++;
429 return reserved;
430}
431
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432static unsigned int current_tail(struct gfs2_sbd *sdp)
433{
434 struct gfs2_ail *ai;
435 unsigned int tail;
436
437 gfs2_log_lock(sdp);
438
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400439 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400441 } else {
442 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443 tail = ai->ai_first;
444 }
445
446 gfs2_log_unlock(sdp);
447
448 return tail;
449}
450
Steven Whitehouse16615be2007-09-17 10:59:52 +0100451void gfs2_log_incr_head(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000452{
453 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehouse16615be2007-09-17 10:59:52 +0100454 BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455
456 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
457 sdp->sd_log_flush_head = 0;
458 sdp->sd_log_flush_wrapped = 1;
459 }
460}
461
462/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100463 * gfs2_log_write_endio - End of I/O for a log buffer
464 * @bh: The buffer head
465 * @uptodate: I/O Status
466 *
467 */
468
469static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
470{
471 struct gfs2_sbd *sdp = bh->b_private;
472 bh->b_private = NULL;
473
474 end_buffer_write_sync(bh, uptodate);
475 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
476 wake_up(&sdp->sd_log_flush_wait);
477}
478
479/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
481 * @sdp: The GFS2 superblock
482 *
483 * Returns: the buffer_head
484 */
485
486struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
487{
Steven Whitehousecd915492006-09-04 12:49:07 -0400488 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 struct buffer_head *bh;
490
Steven Whitehouse16615be2007-09-17 10:59:52 +0100491 bh = sb_getblk(sdp->sd_vfs, blkno);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 lock_buffer(bh);
493 memset(bh->b_data, 0, bh->b_size);
494 set_buffer_uptodate(bh);
495 clear_buffer_dirty(bh);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100496 gfs2_log_incr_head(sdp);
497 atomic_inc(&sdp->sd_log_in_flight);
498 bh->b_private = sdp;
499 bh->b_end_io = gfs2_log_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000500
501 return bh;
502}
503
504/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100505 * gfs2_fake_write_endio -
506 * @bh: The buffer head
507 * @uptodate: The I/O Status
508 *
509 */
510
511static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
512{
513 struct buffer_head *real_bh = bh->b_private;
Steven Whitehouse5a60c532007-09-26 09:39:31 +0100514 struct gfs2_bufdata *bd = real_bh->b_private;
515 struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100516
517 end_buffer_write_sync(bh, uptodate);
518 free_buffer_head(bh);
519 unlock_buffer(real_bh);
520 brelse(real_bh);
521 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
522 wake_up(&sdp->sd_log_flush_wait);
523}
524
525/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
527 * @sdp: the filesystem
528 * @data: the data the buffer_head should point to
529 *
530 * Returns: the log buffer descriptor
531 */
532
533struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
534 struct buffer_head *real)
535{
Steven Whitehousecd915492006-09-04 12:49:07 -0400536 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537 struct buffer_head *bh;
538
Steven Whitehouse16615be2007-09-17 10:59:52 +0100539 bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 atomic_set(&bh->b_count, 1);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100541 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000542 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 bh->b_blocknr = blkno;
544 bh->b_size = sdp->sd_sb.sb_bsize;
545 bh->b_bdev = sdp->sd_vfs->s_bdev;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100546 bh->b_private = real;
547 bh->b_end_io = gfs2_fake_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548
Steven Whitehouse16615be2007-09-17 10:59:52 +0100549 gfs2_log_incr_head(sdp);
550 atomic_inc(&sdp->sd_log_in_flight);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551
552 return bh;
553}
554
Robert Peterson2332c442007-06-18 14:50:20 -0500555static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556{
557 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
558
559 ail2_empty(sdp, new_tail);
560
561 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000562 atomic_add(dist, &sdp->sd_log_blks_free);
563 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000564 gfs2_log_unlock(sdp);
565
566 sdp->sd_log_tail = new_tail;
567}
568
569/**
570 * log_write_header - Get and initialize a journal header buffer
571 * @sdp: The GFS2 superblock
572 *
573 * Returns: the initialized log buffer descriptor
574 */
575
Steven Whitehousecd915492006-09-04 12:49:07 -0400576static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577{
Steven Whitehousecd915492006-09-04 12:49:07 -0400578 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 struct buffer_head *bh;
580 struct gfs2_log_header *lh;
581 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400582 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 bh = sb_getblk(sdp->sd_vfs, blkno);
585 lock_buffer(bh);
586 memset(bh->b_data, 0, bh->b_size);
587 set_buffer_uptodate(bh);
588 clear_buffer_dirty(bh);
589 unlock_buffer(bh);
590
591 gfs2_ail1_empty(sdp, 0);
592 tail = current_tail(sdp);
593
594 lh = (struct gfs2_log_header *)bh->b_data;
595 memset(lh, 0, sizeof(struct gfs2_log_header));
596 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500597 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
598 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400599 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
600 lh->lh_flags = cpu_to_be32(flags);
601 lh->lh_tail = cpu_to_be32(tail);
602 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
604 lh->lh_hash = cpu_to_be32(hash);
605
606 set_buffer_dirty(bh);
607 if (sync_dirty_buffer(bh))
608 gfs2_io_error_bh(sdp, bh);
609 brelse(bh);
610
611 if (sdp->sd_log_tail != tail)
Robert Peterson2332c442007-06-18 14:50:20 -0500612 log_pull_tail(sdp, tail);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 else
614 gfs2_assert_withdraw(sdp, !pull);
615
616 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100617 gfs2_log_incr_head(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618}
619
620static void log_flush_commit(struct gfs2_sbd *sdp)
621{
Steven Whitehouse16615be2007-09-17 10:59:52 +0100622 DEFINE_WAIT(wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623
Steven Whitehouse16615be2007-09-17 10:59:52 +0100624 if (atomic_read(&sdp->sd_log_in_flight)) {
625 do {
626 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
627 TASK_UNINTERRUPTIBLE);
628 if (atomic_read(&sdp->sd_log_in_flight))
629 io_schedule();
630 } while(atomic_read(&sdp->sd_log_in_flight));
631 finish_wait(&sdp->sd_log_flush_wait, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632 }
633
Steven Whitehouse16615be2007-09-17 10:59:52 +0100634 log_write_header(sdp, 0, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635}
636
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100637static void gfs2_ordered_write(struct gfs2_sbd *sdp)
638{
639 struct gfs2_bufdata *bd;
640 struct buffer_head *bh;
641 LIST_HEAD(written);
642
643 gfs2_log_lock(sdp);
644 while (!list_empty(&sdp->sd_log_le_ordered)) {
645 bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
646 list_move(&bd->bd_le.le_list, &written);
647 bh = bd->bd_bh;
648 if (!buffer_dirty(bh))
649 continue;
650 get_bh(bh);
651 gfs2_log_unlock(sdp);
652 lock_buffer(bh);
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100653 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100654 bh->b_end_io = end_buffer_write_sync;
655 submit_bh(WRITE, bh);
656 } else {
657 unlock_buffer(bh);
658 brelse(bh);
659 }
660 gfs2_log_lock(sdp);
661 }
662 list_splice(&written, &sdp->sd_log_le_ordered);
663 gfs2_log_unlock(sdp);
664}
665
666static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
667{
668 struct gfs2_bufdata *bd;
669 struct buffer_head *bh;
670
671 gfs2_log_lock(sdp);
672 while (!list_empty(&sdp->sd_log_le_ordered)) {
673 bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
674 bh = bd->bd_bh;
675 if (buffer_locked(bh)) {
676 get_bh(bh);
677 gfs2_log_unlock(sdp);
678 wait_on_buffer(bh);
679 brelse(bh);
680 gfs2_log_lock(sdp);
681 continue;
682 }
683 list_del_init(&bd->bd_le.le_list);
684 }
685 gfs2_log_unlock(sdp);
686}
687
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400689 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690 * @sdp: the filesystem
691 * @gl: The glock structure to flush. If NULL, flush the whole incore log
692 *
693 */
694
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000695void __gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696{
697 struct gfs2_ail *ai;
698
Steven Whitehouse484adff2006-03-29 09:12:12 -0500699 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000700
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000701 /* Log might have been flushed while we waited for the flush lock */
702 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
703 up_write(&sdp->sd_log_flush_lock);
704 return;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000705 }
706
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400707 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
708 INIT_LIST_HEAD(&ai->ai_ail1_list);
709 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710
Steven Whitehouse16615be2007-09-17 10:59:52 +0100711 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
712 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
713 sdp->sd_log_commited_buf);
714 gfs2_assert_withdraw(sdp, 0);
715 }
716 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
717 printk(KERN_INFO "GFS2: log databuf %u %u\n",
718 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
719 gfs2_assert_withdraw(sdp, 0);
720 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721 gfs2_assert_withdraw(sdp,
722 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
723
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 sdp->sd_log_flush_head = sdp->sd_log_head;
725 sdp->sd_log_flush_wrapped = 0;
726 ai->ai_first = sdp->sd_log_flush_head;
727
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100728 gfs2_ordered_write(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 lops_before_commit(sdp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100730 gfs2_ordered_wait(sdp);
731
Steven Whitehouse16615be2007-09-17 10:59:52 +0100732 if (sdp->sd_log_head != sdp->sd_log_flush_head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 log_flush_commit(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500734 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
735 gfs2_log_lock(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000736 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
Robert Peterson2332c442007-06-18 14:50:20 -0500737 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 log_write_header(sdp, 0, PULL);
Robert Peterson2332c442007-06-18 14:50:20 -0500739 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400741
742 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400744 sdp->sd_log_blks_reserved = 0;
745 sdp->sd_log_commited_buf = 0;
Robert Peterson2332c442007-06-18 14:50:20 -0500746 sdp->sd_log_commited_databuf = 0;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400747 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000748
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 if (!list_empty(&ai->ai_ail1_list)) {
750 list_add(&ai->ai_list, &sdp->sd_ail1_list);
751 ai = NULL;
752 }
753 gfs2_log_unlock(sdp);
754
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755 sdp->sd_vfs->s_dirt = 0;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500756 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757
758 kfree(ai);
759}
760
761static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
762{
Robert Peterson2332c442007-06-18 14:50:20 -0500763 unsigned int reserved;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 unsigned int old;
765
766 gfs2_log_lock(sdp);
767
768 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500769 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
770 tr->tr_num_databuf_rm;
771 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
772 (((int)sdp->sd_log_commited_databuf) >= 0));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
774 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
Robert Peterson2332c442007-06-18 14:50:20 -0500775 reserved = calc_reserved(sdp);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000776 old = atomic_read(&sdp->sd_log_blks_free);
777 atomic_add(tr->tr_reserved - (reserved - sdp->sd_log_blks_reserved),
778 &sdp->sd_log_blks_free);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779
Steven Whitehousefd041f02007-11-08 14:55:03 +0000780 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) >= old);
781 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
Robert Peterson2332c442007-06-18 14:50:20 -0500782 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783
784 sdp->sd_log_blks_reserved = reserved;
785
786 gfs2_log_unlock(sdp);
787}
788
789/**
790 * gfs2_log_commit - Commit a transaction to the log
791 * @sdp: the filesystem
792 * @tr: the transaction
793 *
794 * Returns: errno
795 */
796
797void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
798{
799 log_refund(sdp, tr);
800 lops_incore_commit(sdp, tr);
801
802 sdp->sd_vfs->s_dirt = 1;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500803 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 gfs2_log_lock(sdp);
Steven Whitehouseb0041572006-11-23 10:51:34 -0500806 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
807 wake_up_process(sdp->sd_logd_process);
808 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809}
810
811/**
812 * gfs2_log_shutdown - write a shutdown header into a journal
813 * @sdp: the filesystem
814 *
815 */
816
817void gfs2_log_shutdown(struct gfs2_sbd *sdp)
818{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500819 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
824 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
825 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
826 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
827
828 sdp->sd_log_flush_head = sdp->sd_log_head;
829 sdp->sd_log_flush_wrapped = 0;
830
Robert Peterson2332c442007-06-18 14:50:20 -0500831 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
832 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833
Steven Whitehousefd041f02007-11-08 14:55:03 +0000834 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400835 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
836 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837
838 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839 sdp->sd_log_tail = sdp->sd_log_head;
840
Steven Whitehouse484adff2006-03-29 09:12:12 -0500841 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842}
843
Steven Whitehousea25311c2006-11-23 11:06:35 -0500844
845/**
846 * gfs2_meta_syncfs - sync all the buffers in a filesystem
847 * @sdp: the filesystem
848 *
849 */
850
851void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
852{
853 gfs2_log_flush(sdp, NULL);
854 for (;;) {
855 gfs2_ail1_start(sdp, DIO_ALL);
856 if (gfs2_ail1_empty(sdp, DIO_ALL))
857 break;
858 msleep(10);
859 }
860}
861