blob: ee704676b2f1d62a717a32465559a2c9d28b86c2 [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
71void gfs2_remove_from_ail(struct address_space *mapping, struct gfs2_bufdata *bd)
72{
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);
77 if (mapping)
78 gfs2_meta_cache_flush(GFS2_I(mapping->host));
79 brelse(bd->bd_bh);
80}
81
82/**
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040083 * gfs2_ail1_start_one - Start I/O on a part of the AIL
84 * @sdp: the filesystem
85 * @tr: the part of the AIL
86 *
87 */
88
89static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
90{
91 struct gfs2_bufdata *bd, *s;
92 struct buffer_head *bh;
93 int retry;
94
95 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
96
97 do {
98 retry = 0;
99
100 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
101 bd_ail_st_list) {
102 bh = bd->bd_bh;
103
104 gfs2_assert(sdp, bd->bd_ail == ai);
105
106 if (!buffer_busy(bh)) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100107 if (!buffer_uptodate(bh))
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400108 gfs2_io_error_bh(sdp, bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400109 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
110 continue;
111 }
112
113 if (!buffer_dirty(bh))
114 continue;
115
116 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
117
Steven Whitehouse16615be2007-09-17 10:59:52 +0100118 get_bh(bh);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400119 gfs2_log_unlock(sdp);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100120 lock_buffer(bh);
121 if (test_clear_buffer_dirty(bh)) {
122 bh->b_end_io = end_buffer_write_sync;
123 submit_bh(WRITE, bh);
124 } else {
125 unlock_buffer(bh);
126 brelse(bh);
127 }
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400128 gfs2_log_lock(sdp);
129
130 retry = 1;
131 break;
132 }
133 } while (retry);
134}
135
136/**
137 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
138 * @sdp: the filesystem
139 * @ai: the AIL entry
140 *
141 */
142
143static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
144{
145 struct gfs2_bufdata *bd, *s;
146 struct buffer_head *bh;
147
148 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
149 bd_ail_st_list) {
150 bh = bd->bd_bh;
151
152 gfs2_assert(sdp, bd->bd_ail == ai);
153
154 if (buffer_busy(bh)) {
155 if (flags & DIO_ALL)
156 continue;
157 else
158 break;
159 }
160
161 if (!buffer_uptodate(bh))
162 gfs2_io_error_bh(sdp, bh);
163
164 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
165 }
166
167 return list_empty(&ai->ai_ail1_list);
168}
169
Steven Whitehousea25311c2006-11-23 11:06:35 -0500170static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171{
Bob Peterson693ddea2007-07-24 14:07:33 -0500172 struct list_head *head;
Steven Whitehousecd915492006-09-04 12:49:07 -0400173 u64 sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400174 struct list_head *first;
175 struct gfs2_ail *first_ai, *ai, *tmp;
176 int done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177
178 gfs2_log_lock(sdp);
Bob Peterson693ddea2007-07-24 14:07:33 -0500179 head = &sdp->sd_ail1_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180 if (list_empty(head)) {
181 gfs2_log_unlock(sdp);
182 return;
183 }
184 sync_gen = sdp->sd_ail_sync_gen++;
185
186 first = head->prev;
187 first_ai = list_entry(first, struct gfs2_ail, ai_list);
188 first_ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400189 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190
191 if (flags & DIO_ALL)
192 first = NULL;
193
Steven Whitehouse74669412006-09-19 11:17:38 -0400194 while(!done) {
Steven Whitehouse484adff2006-03-29 09:12:12 -0500195 if (first && (head->prev != first ||
196 gfs2_ail1_empty_one(sdp, first_ai, 0)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197 break;
198
Steven Whitehouse74669412006-09-19 11:17:38 -0400199 done = 1;
200 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000201 if (ai->ai_sync_gen >= sync_gen)
202 continue;
203 ai->ai_sync_gen = sync_gen;
Steven Whitehouse74669412006-09-19 11:17:38 -0400204 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
205 done = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 break;
207 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 }
209
210 gfs2_log_unlock(sdp);
211}
212
213int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
214{
215 struct gfs2_ail *ai, *s;
216 int ret;
217
218 gfs2_log_lock(sdp);
219
220 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
221 if (gfs2_ail1_empty_one(sdp, ai, flags))
222 list_move(&ai->ai_list, &sdp->sd_ail2_list);
223 else if (!(flags & DIO_ALL))
224 break;
225 }
226
227 ret = list_empty(&sdp->sd_ail1_list);
228
229 gfs2_log_unlock(sdp);
230
231 return ret;
232}
233
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400234
235/**
236 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
237 * @sdp: the filesystem
238 * @ai: the AIL entry
239 *
240 */
241
242static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
243{
244 struct list_head *head = &ai->ai_ail2_list;
245 struct gfs2_bufdata *bd;
246
247 while (!list_empty(head)) {
248 bd = list_entry(head->prev, struct gfs2_bufdata,
249 bd_ail_st_list);
250 gfs2_assert(sdp, bd->bd_ail == ai);
Steven Whitehouse1e1a3d02007-08-27 09:45:26 +0100251 gfs2_remove_from_ail(bd->bd_bh->b_page->mapping, bd);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400252 }
253}
254
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
256{
257 struct gfs2_ail *ai, *safe;
258 unsigned int old_tail = sdp->sd_log_tail;
259 int wrap = (new_tail < old_tail);
260 int a, b, rm;
261
262 gfs2_log_lock(sdp);
263
264 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
265 a = (old_tail <= ai->ai_first);
266 b = (ai->ai_first < new_tail);
267 rm = (wrap) ? (a || b) : (a && b);
268 if (!rm)
269 continue;
270
271 gfs2_ail2_empty_one(sdp, ai);
272 list_del(&ai->ai_list);
273 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
274 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
275 kfree(ai);
276 }
277
278 gfs2_log_unlock(sdp);
279}
280
281/**
282 * gfs2_log_reserve - Make a log reservation
283 * @sdp: The GFS2 superblock
284 * @blks: The number of blocks to reserve
285 *
Steven Whitehouse89918642007-06-01 15:19:33 +0100286 * Note that we never give out the last few blocks of the journal. Thats
Robert Peterson2332c442007-06-18 14:50:20 -0500287 * due to the fact that there is a small number of header blocks
Steven Whitehouseb0041572006-11-23 10:51:34 -0500288 * associated with each log flush. The exact number can't be known until
289 * flush time, so we ensure that we have just enough free blocks at all
290 * times to avoid running out during a log flush.
291 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 * Returns: errno
293 */
294
295int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
296{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297 unsigned int try = 0;
Steven Whitehouse89918642007-06-01 15:19:33 +0100298 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299
300 if (gfs2_assert_warn(sdp, blks) ||
301 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
302 return -EINVAL;
303
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500304 mutex_lock(&sdp->sd_log_reserve_mutex);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500305 gfs2_log_lock(sdp);
Steven Whitehouse89918642007-06-01 15:19:33 +0100306 while(sdp->sd_log_blks_free <= (blks + reserved_blks)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308 gfs2_ail1_empty(sdp, 0);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400309 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310
311 if (try++)
312 gfs2_ail1_start(sdp, 0);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500313 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314 }
Steven Whitehouse484adff2006-03-29 09:12:12 -0500315 sdp->sd_log_blks_free -= blks;
316 gfs2_log_unlock(sdp);
317 mutex_unlock(&sdp->sd_log_reserve_mutex);
318
319 down_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000320
321 return 0;
322}
323
324/**
325 * gfs2_log_release - Release a given number of log blocks
326 * @sdp: The GFS2 superblock
327 * @blks: The number of blocks
328 *
329 */
330
331void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
332{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333
334 gfs2_log_lock(sdp);
335 sdp->sd_log_blks_free += blks;
336 gfs2_assert_withdraw(sdp,
337 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
338 gfs2_log_unlock(sdp);
Steven Whitehouseed386502006-04-07 16:28:07 -0400339 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340}
341
Steven Whitehousecd915492006-09-04 12:49:07 -0400342static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343{
Steven Whitehouse23591252006-10-13 17:25:45 -0400344 struct inode *inode = sdp->sd_jdesc->jd_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400346 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347
Steven Whitehouse23591252006-10-13 17:25:45 -0400348 bh_map.b_size = 1 << inode->i_blkbits;
349 error = gfs2_block_map(inode, lbn, 0, &bh_map);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400350 if (error || !bh_map.b_blocknr)
Ryusuke Konishiaed32552006-11-28 02:53:22 +0900351 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error,
352 (unsigned long long)bh_map.b_blocknr, lbn);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400353 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400355 return bh_map.b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356}
357
358/**
359 * log_distance - Compute distance between two journal blocks
360 * @sdp: The GFS2 superblock
361 * @newer: The most recent journal block of the pair
362 * @older: The older journal block of the pair
363 *
364 * Compute the distance (in the journal direction) between two
365 * blocks in the journal
366 *
367 * Returns: the distance in blocks
368 */
369
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400370static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371 unsigned int older)
372{
373 int dist;
374
375 dist = newer - older;
376 if (dist < 0)
377 dist += sdp->sd_jdesc->jd_blocks;
378
379 return dist;
380}
381
Robert Peterson2332c442007-06-18 14:50:20 -0500382/**
383 * calc_reserved - Calculate the number of blocks to reserve when
384 * refunding a transaction's unused buffers.
385 * @sdp: The GFS2 superblock
386 *
387 * This is complex. We need to reserve room for all our currently used
388 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
389 * all our journaled data buffers for journaled files (e.g. files in the
390 * meta_fs like rindex, or files for which chattr +j was done.)
391 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
392 * will count it as free space (sd_log_blks_free) and corruption will follow.
393 *
394 * We can have metadata bufs and jdata bufs in the same journal. So each
395 * type gets its own log header, for which we need to reserve a block.
396 * In fact, each type has the potential for needing more than one header
397 * in cases where we have more buffers than will fit on a journal page.
398 * Metadata journal entries take up half the space of journaled buffer entries.
399 * Thus, metadata entries have buf_limit (502) and journaled buffers have
400 * databuf_limit (251) before they cause a wrap around.
401 *
402 * Also, we need to reserve blocks for revoke journal entries and one for an
403 * overall header for the lot.
404 *
405 * Returns: the number of blocks reserved
406 */
407static unsigned int calc_reserved(struct gfs2_sbd *sdp)
408{
409 unsigned int reserved = 0;
410 unsigned int mbuf_limit, metabufhdrs_needed;
411 unsigned int dbuf_limit, databufhdrs_needed;
412 unsigned int revokes = 0;
413
414 mbuf_limit = buf_limit(sdp);
415 metabufhdrs_needed = (sdp->sd_log_commited_buf +
416 (mbuf_limit - 1)) / mbuf_limit;
417 dbuf_limit = databuf_limit(sdp);
418 databufhdrs_needed = (sdp->sd_log_commited_databuf +
419 (dbuf_limit - 1)) / dbuf_limit;
420
421 if (sdp->sd_log_commited_revoke)
422 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
423 sizeof(u64));
424
425 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
426 sdp->sd_log_commited_databuf + databufhdrs_needed +
427 revokes;
428 /* One for the overall header */
429 if (reserved)
430 reserved++;
431 return reserved;
432}
433
David Teiglandb3b94fa2006-01-16 16:50:04 +0000434static unsigned int current_tail(struct gfs2_sbd *sdp)
435{
436 struct gfs2_ail *ai;
437 unsigned int tail;
438
439 gfs2_log_lock(sdp);
440
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400441 if (list_empty(&sdp->sd_ail1_list)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 tail = sdp->sd_log_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400443 } else {
444 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 tail = ai->ai_first;
446 }
447
448 gfs2_log_unlock(sdp);
449
450 return tail;
451}
452
Steven Whitehouse16615be2007-09-17 10:59:52 +0100453void gfs2_log_incr_head(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454{
455 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
Steven Whitehouse16615be2007-09-17 10:59:52 +0100456 BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457
458 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
459 sdp->sd_log_flush_head = 0;
460 sdp->sd_log_flush_wrapped = 1;
461 }
462}
463
464/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100465 * gfs2_log_write_endio - End of I/O for a log buffer
466 * @bh: The buffer head
467 * @uptodate: I/O Status
468 *
469 */
470
471static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
472{
473 struct gfs2_sbd *sdp = bh->b_private;
474 bh->b_private = NULL;
475
476 end_buffer_write_sync(bh, uptodate);
477 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
478 wake_up(&sdp->sd_log_flush_wait);
479}
480
481/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
483 * @sdp: The GFS2 superblock
484 *
485 * Returns: the buffer_head
486 */
487
488struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
489{
Steven Whitehousecd915492006-09-04 12:49:07 -0400490 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491 struct buffer_head *bh;
492
Steven Whitehouse16615be2007-09-17 10:59:52 +0100493 bh = sb_getblk(sdp->sd_vfs, blkno);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494 lock_buffer(bh);
495 memset(bh->b_data, 0, bh->b_size);
496 set_buffer_uptodate(bh);
497 clear_buffer_dirty(bh);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100498 gfs2_log_incr_head(sdp);
499 atomic_inc(&sdp->sd_log_in_flight);
500 bh->b_private = sdp;
501 bh->b_end_io = gfs2_log_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502
503 return bh;
504}
505
506/**
Steven Whitehouse16615be2007-09-17 10:59:52 +0100507 * gfs2_fake_write_endio -
508 * @bh: The buffer head
509 * @uptodate: The I/O Status
510 *
511 */
512
513static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
514{
515 struct buffer_head *real_bh = bh->b_private;
516 struct gfs2_sbd *sdp = GFS2_SB(real_bh->b_page->mapping->host);
517
518 end_buffer_write_sync(bh, uptodate);
519 free_buffer_head(bh);
520 unlock_buffer(real_bh);
521 brelse(real_bh);
522 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
523 wake_up(&sdp->sd_log_flush_wait);
524}
525
526/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
528 * @sdp: the filesystem
529 * @data: the data the buffer_head should point to
530 *
531 * Returns: the log buffer descriptor
532 */
533
534struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
535 struct buffer_head *real)
536{
Steven Whitehousecd915492006-09-04 12:49:07 -0400537 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538 struct buffer_head *bh;
539
Steven Whitehouse16615be2007-09-17 10:59:52 +0100540 bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 atomic_set(&bh->b_count, 1);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100542 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000543 set_bh_page(bh, real->b_page, bh_offset(real));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544 bh->b_blocknr = blkno;
545 bh->b_size = sdp->sd_sb.sb_bsize;
546 bh->b_bdev = sdp->sd_vfs->s_bdev;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100547 bh->b_private = real;
548 bh->b_end_io = gfs2_fake_write_endio;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549
Steven Whitehouse16615be2007-09-17 10:59:52 +0100550 gfs2_log_incr_head(sdp);
551 atomic_inc(&sdp->sd_log_in_flight);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552
553 return bh;
554}
555
Robert Peterson2332c442007-06-18 14:50:20 -0500556static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557{
558 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
559
560 ail2_empty(sdp, new_tail);
561
562 gfs2_log_lock(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500563 sdp->sd_log_blks_free += dist;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400564 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 gfs2_log_unlock(sdp);
566
567 sdp->sd_log_tail = new_tail;
568}
569
570/**
571 * log_write_header - Get and initialize a journal header buffer
572 * @sdp: The GFS2 superblock
573 *
574 * Returns: the initialized log buffer descriptor
575 */
576
Steven Whitehousecd915492006-09-04 12:49:07 -0400577static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578{
Steven Whitehousecd915492006-09-04 12:49:07 -0400579 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 struct buffer_head *bh;
581 struct gfs2_log_header *lh;
582 unsigned int tail;
Steven Whitehousecd915492006-09-04 12:49:07 -0400583 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 bh = sb_getblk(sdp->sd_vfs, blkno);
586 lock_buffer(bh);
587 memset(bh->b_data, 0, bh->b_size);
588 set_buffer_uptodate(bh);
589 clear_buffer_dirty(bh);
590 unlock_buffer(bh);
591
592 gfs2_ail1_empty(sdp, 0);
593 tail = current_tail(sdp);
594
595 lh = (struct gfs2_log_header *)bh->b_data;
596 memset(lh, 0, sizeof(struct gfs2_log_header));
597 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500598 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
599 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
Steven Whitehousee0f2bf72006-07-17 09:36:28 -0400600 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
601 lh->lh_flags = cpu_to_be32(flags);
602 lh->lh_tail = cpu_to_be32(tail);
603 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
605 lh->lh_hash = cpu_to_be32(hash);
606
607 set_buffer_dirty(bh);
608 if (sync_dirty_buffer(bh))
609 gfs2_io_error_bh(sdp, bh);
610 brelse(bh);
611
612 if (sdp->sd_log_tail != tail)
Robert Peterson2332c442007-06-18 14:50:20 -0500613 log_pull_tail(sdp, tail);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000614 else
615 gfs2_assert_withdraw(sdp, !pull);
616
617 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100618 gfs2_log_incr_head(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619}
620
621static void log_flush_commit(struct gfs2_sbd *sdp)
622{
Steven Whitehouse16615be2007-09-17 10:59:52 +0100623 DEFINE_WAIT(wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624
Steven Whitehouse16615be2007-09-17 10:59:52 +0100625 if (atomic_read(&sdp->sd_log_in_flight)) {
626 do {
627 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
628 TASK_UNINTERRUPTIBLE);
629 if (atomic_read(&sdp->sd_log_in_flight))
630 io_schedule();
631 } while(atomic_read(&sdp->sd_log_in_flight));
632 finish_wait(&sdp->sd_log_flush_wait, &wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000633 }
634
Steven Whitehouse16615be2007-09-17 10:59:52 +0100635 log_write_header(sdp, 0, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636}
637
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100638static void gfs2_ordered_write(struct gfs2_sbd *sdp)
639{
640 struct gfs2_bufdata *bd;
641 struct buffer_head *bh;
642 LIST_HEAD(written);
643
644 gfs2_log_lock(sdp);
645 while (!list_empty(&sdp->sd_log_le_ordered)) {
646 bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
647 list_move(&bd->bd_le.le_list, &written);
648 bh = bd->bd_bh;
649 if (!buffer_dirty(bh))
650 continue;
651 get_bh(bh);
652 gfs2_log_unlock(sdp);
653 lock_buffer(bh);
654 if (test_clear_buffer_dirty(bh)) {
655 bh->b_end_io = end_buffer_write_sync;
656 submit_bh(WRITE, bh);
657 } else {
658 unlock_buffer(bh);
659 brelse(bh);
660 }
661 gfs2_log_lock(sdp);
662 }
663 list_splice(&written, &sdp->sd_log_le_ordered);
664 gfs2_log_unlock(sdp);
665}
666
667static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
668{
669 struct gfs2_bufdata *bd;
670 struct buffer_head *bh;
671
672 gfs2_log_lock(sdp);
673 while (!list_empty(&sdp->sd_log_le_ordered)) {
674 bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
675 bh = bd->bd_bh;
676 if (buffer_locked(bh)) {
677 get_bh(bh);
678 gfs2_log_unlock(sdp);
679 wait_on_buffer(bh);
680 brelse(bh);
681 gfs2_log_lock(sdp);
682 continue;
683 }
684 list_del_init(&bd->bd_le.le_list);
685 }
686 gfs2_log_unlock(sdp);
687}
688
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689/**
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400690 * gfs2_log_flush - flush incore transaction(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 * @sdp: the filesystem
692 * @gl: The glock structure to flush. If NULL, flush the whole incore log
693 *
694 */
695
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400696void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697{
698 struct gfs2_ail *ai;
699
Steven Whitehouse484adff2006-03-29 09:12:12 -0500700 down_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000701
702 if (gl) {
703 gfs2_log_lock(sdp);
704 if (list_empty(&gl->gl_le.le_list)) {
705 gfs2_log_unlock(sdp);
Steven Whitehouse484adff2006-03-29 09:12:12 -0500706 up_write(&sdp->sd_log_flush_lock);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000707 return;
708 }
709 gfs2_log_unlock(sdp);
710 }
711
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400712 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
713 INIT_LIST_HEAD(&ai->ai_ail1_list);
714 INIT_LIST_HEAD(&ai->ai_ail2_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715
Steven Whitehouse16615be2007-09-17 10:59:52 +0100716 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
717 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
718 sdp->sd_log_commited_buf);
719 gfs2_assert_withdraw(sdp, 0);
720 }
721 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
722 printk(KERN_INFO "GFS2: log databuf %u %u\n",
723 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
724 gfs2_assert_withdraw(sdp, 0);
725 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 gfs2_assert_withdraw(sdp,
727 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
728
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 sdp->sd_log_flush_head = sdp->sd_log_head;
730 sdp->sd_log_flush_wrapped = 0;
731 ai->ai_first = sdp->sd_log_flush_head;
732
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100733 gfs2_ordered_write(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 lops_before_commit(sdp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100735 gfs2_ordered_wait(sdp);
736
Steven Whitehouse16615be2007-09-17 10:59:52 +0100737 if (sdp->sd_log_head != sdp->sd_log_flush_head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 log_flush_commit(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500739 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
740 gfs2_log_lock(sdp);
741 sdp->sd_log_blks_free--; /* Adjust for unreserved buffer */
742 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 log_write_header(sdp, 0, PULL);
Robert Peterson2332c442007-06-18 14:50:20 -0500744 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745 lops_after_commit(sdp, ai);
Steven Whitehousefe1a6982006-10-11 13:34:59 -0400746
747 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000748 sdp->sd_log_head = sdp->sd_log_flush_head;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400749 sdp->sd_log_blks_reserved = 0;
750 sdp->sd_log_commited_buf = 0;
Robert Peterson2332c442007-06-18 14:50:20 -0500751 sdp->sd_log_commited_databuf = 0;
Steven Whitehousefaa31ce2006-09-13 11:13:27 -0400752 sdp->sd_log_commited_revoke = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754 if (!list_empty(&ai->ai_ail1_list)) {
755 list_add(&ai->ai_list, &sdp->sd_ail1_list);
756 ai = NULL;
757 }
758 gfs2_log_unlock(sdp);
759
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 sdp->sd_vfs->s_dirt = 0;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500761 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762
763 kfree(ai);
764}
765
766static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
767{
Robert Peterson2332c442007-06-18 14:50:20 -0500768 unsigned int reserved;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769 unsigned int old;
770
771 gfs2_log_lock(sdp);
772
773 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
Robert Peterson2332c442007-06-18 14:50:20 -0500774 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
775 tr->tr_num_databuf_rm;
776 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
777 (((int)sdp->sd_log_commited_databuf) >= 0));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
779 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
Robert Peterson2332c442007-06-18 14:50:20 -0500780 reserved = calc_reserved(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 old = sdp->sd_log_blks_free;
782 sdp->sd_log_blks_free += tr->tr_reserved -
783 (reserved - sdp->sd_log_blks_reserved);
784
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400785 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
Robert Peterson2332c442007-06-18 14:50:20 -0500786 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <=
787 sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788
789 sdp->sd_log_blks_reserved = reserved;
790
791 gfs2_log_unlock(sdp);
792}
793
794/**
795 * gfs2_log_commit - Commit a transaction to the log
796 * @sdp: the filesystem
797 * @tr: the transaction
798 *
799 * Returns: errno
800 */
801
802void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
803{
804 log_refund(sdp, tr);
805 lops_incore_commit(sdp, tr);
806
807 sdp->sd_vfs->s_dirt = 1;
Steven Whitehouse484adff2006-03-29 09:12:12 -0500808 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809
David Teiglandb3b94fa2006-01-16 16:50:04 +0000810 gfs2_log_lock(sdp);
Steven Whitehouseb0041572006-11-23 10:51:34 -0500811 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
812 wake_up_process(sdp->sd_logd_process);
813 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000814}
815
816/**
817 * gfs2_log_shutdown - write a shutdown header into a journal
818 * @sdp: the filesystem
819 *
820 */
821
822void gfs2_log_shutdown(struct gfs2_sbd *sdp)
823{
Steven Whitehouse484adff2006-03-29 09:12:12 -0500824 down_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
827 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
828 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
830 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
831 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
832 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
833
834 sdp->sd_log_flush_head = sdp->sd_log_head;
835 sdp->sd_log_flush_wrapped = 0;
836
Robert Peterson2332c442007-06-18 14:50:20 -0500837 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
838 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839
Steven Whitehousea74604b2006-04-21 15:10:46 -0400840 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
841 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
842 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843
844 sdp->sd_log_head = sdp->sd_log_flush_head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845 sdp->sd_log_tail = sdp->sd_log_head;
846
Steven Whitehouse484adff2006-03-29 09:12:12 -0500847 up_write(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848}
849
Steven Whitehousea25311c2006-11-23 11:06:35 -0500850
851/**
852 * gfs2_meta_syncfs - sync all the buffers in a filesystem
853 * @sdp: the filesystem
854 *
855 */
856
857void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
858{
859 gfs2_log_flush(sdp, NULL);
860 for (;;) {
861 gfs2_ail1_start(sdp, DIO_ALL);
862 if (gfs2_ail1_empty(sdp, DIO_ALL))
863 break;
864 msleep(10);
865 }
866}
867