Merge branch 'topic/asoc' into for-linus
[linux-2.6-block.git] / fs / gfs2 / log.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
da6dd40d 3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
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>
5c676f6d 15#include <linux/gfs2_ondisk.h>
71b86f56 16#include <linux/crc32.h>
7d308590 17#include <linux/lm_interface.h>
a25311c8 18#include <linux/delay.h>
ec69b188
SW
19#include <linux/kthread.h>
20#include <linux/freezer.h>
254db57f 21#include <linux/bio.h>
b3b94faa
DT
22
23#include "gfs2.h"
5c676f6d 24#include "incore.h"
b3b94faa
DT
25#include "bmap.h"
26#include "glock.h"
27#include "log.h"
28#include "lops.h"
29#include "meta_io.h"
5c676f6d 30#include "util.h"
71b86f56 31#include "dir.h"
b3b94faa
DT
32
33#define PULL 1
34
b3b94faa
DT
35/**
36 * gfs2_struct2blk - compute stuff
37 * @sdp: the filesystem
38 * @nstruct: the number of structures
39 * @ssize: the size of the structures
40 *
41 * Compute the number of log descriptor blocks needed to hold a certain number
42 * of structures of a certain size.
43 *
44 * Returns: the number of blocks needed (minimum is always 1)
45 */
46
47unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
48 unsigned int ssize)
49{
50 unsigned int blks;
51 unsigned int first, second;
52
53 blks = 1;
faa31ce8 54 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
b3b94faa
DT
55
56 if (nstruct > first) {
568f4c96
SW
57 second = (sdp->sd_sb.sb_bsize -
58 sizeof(struct gfs2_meta_header)) / ssize;
5c676f6d 59 blks += DIV_ROUND_UP(nstruct - first, second);
b3b94faa
DT
60 }
61
62 return blks;
63}
64
1e1a3d03
SW
65/**
66 * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
67 * @mapping: The associated mapping (maybe NULL)
68 * @bd: The gfs2_bufdata to remove
69 *
70 * The log lock _must_ be held when calling this function
71 *
72 */
73
f91a0d3e 74void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
1e1a3d03
SW
75{
76 bd->bd_ail = NULL;
1ad38c43
SW
77 list_del_init(&bd->bd_ail_st_list);
78 list_del_init(&bd->bd_ail_gl_list);
1e1a3d03 79 atomic_dec(&bd->bd_gl->gl_ail_count);
1e1a3d03
SW
80 brelse(bd->bd_bh);
81}
82
ddacfaf7
SW
83/**
84 * gfs2_ail1_start_one - Start I/O on a part of the AIL
85 * @sdp: the filesystem
86 * @tr: the part of the AIL
87 *
88 */
89
90static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
2d81afb8
HH
91__releases(&sdp->sd_log_lock)
92__acquires(&sdp->sd_log_lock)
ddacfaf7
SW
93{
94 struct gfs2_bufdata *bd, *s;
95 struct buffer_head *bh;
96 int retry;
97
ddacfaf7
SW
98 do {
99 retry = 0;
100
101 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
102 bd_ail_st_list) {
103 bh = bd->bd_bh;
104
105 gfs2_assert(sdp, bd->bd_ail == ai);
106
107 if (!buffer_busy(bh)) {
16615be1 108 if (!buffer_uptodate(bh))
ddacfaf7 109 gfs2_io_error_bh(sdp, bh);
ddacfaf7
SW
110 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
111 continue;
112 }
113
114 if (!buffer_dirty(bh))
115 continue;
116
117 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
118
16615be1 119 get_bh(bh);
ddacfaf7 120 gfs2_log_unlock(sdp);
16615be1
SW
121 lock_buffer(bh);
122 if (test_clear_buffer_dirty(bh)) {
123 bh->b_end_io = end_buffer_write_sync;
124 submit_bh(WRITE, bh);
125 } else {
126 unlock_buffer(bh);
127 brelse(bh);
128 }
ddacfaf7
SW
129 gfs2_log_lock(sdp);
130
131 retry = 1;
132 break;
133 }
134 } while (retry);
135}
136
137/**
138 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
139 * @sdp: the filesystem
140 * @ai: the AIL entry
141 *
142 */
143
144static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
145{
146 struct gfs2_bufdata *bd, *s;
147 struct buffer_head *bh;
148
149 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
150 bd_ail_st_list) {
151 bh = bd->bd_bh;
152
153 gfs2_assert(sdp, bd->bd_ail == ai);
154
155 if (buffer_busy(bh)) {
156 if (flags & DIO_ALL)
157 continue;
158 else
159 break;
160 }
161
162 if (!buffer_uptodate(bh))
163 gfs2_io_error_bh(sdp, bh);
164
165 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
166 }
167
168 return list_empty(&ai->ai_ail1_list);
169}
170
a25311c8 171static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
b3b94faa 172{
693ddeab 173 struct list_head *head;
cd915493 174 u64 sync_gen;
74669416
SW
175 struct list_head *first;
176 struct gfs2_ail *first_ai, *ai, *tmp;
177 int done = 0;
b3b94faa
DT
178
179 gfs2_log_lock(sdp);
693ddeab 180 head = &sdp->sd_ail1_list;
b3b94faa
DT
181 if (list_empty(head)) {
182 gfs2_log_unlock(sdp);
183 return;
184 }
185 sync_gen = sdp->sd_ail_sync_gen++;
186
187 first = head->prev;
188 first_ai = list_entry(first, struct gfs2_ail, ai_list);
189 first_ai->ai_sync_gen = sync_gen;
74669416 190 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
b3b94faa
DT
191
192 if (flags & DIO_ALL)
193 first = NULL;
194
74669416 195 while(!done) {
484adff8
SW
196 if (first && (head->prev != first ||
197 gfs2_ail1_empty_one(sdp, first_ai, 0)))
b3b94faa
DT
198 break;
199
74669416
SW
200 done = 1;
201 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
b3b94faa
DT
202 if (ai->ai_sync_gen >= sync_gen)
203 continue;
204 ai->ai_sync_gen = sync_gen;
74669416
SW
205 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
206 done = 0;
b3b94faa
DT
207 break;
208 }
b3b94faa
DT
209 }
210
211 gfs2_log_unlock(sdp);
212}
213
ec69b188 214static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
b3b94faa
DT
215{
216 struct gfs2_ail *ai, *s;
217 int ret;
218
219 gfs2_log_lock(sdp);
220
221 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
222 if (gfs2_ail1_empty_one(sdp, ai, flags))
223 list_move(&ai->ai_list, &sdp->sd_ail2_list);
224 else if (!(flags & DIO_ALL))
225 break;
226 }
227
228 ret = list_empty(&sdp->sd_ail1_list);
229
230 gfs2_log_unlock(sdp);
231
232 return ret;
233}
234
ddacfaf7
SW
235
236/**
237 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
238 * @sdp: the filesystem
239 * @ai: the AIL entry
240 *
241 */
242
243static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
244{
245 struct list_head *head = &ai->ai_ail2_list;
246 struct gfs2_bufdata *bd;
247
248 while (!list_empty(head)) {
249 bd = list_entry(head->prev, struct gfs2_bufdata,
250 bd_ail_st_list);
251 gfs2_assert(sdp, bd->bd_ail == ai);
f91a0d3e 252 gfs2_remove_from_ail(bd);
ddacfaf7
SW
253 }
254}
255
b3b94faa
DT
256static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
257{
258 struct gfs2_ail *ai, *safe;
259 unsigned int old_tail = sdp->sd_log_tail;
260 int wrap = (new_tail < old_tail);
261 int a, b, rm;
262
263 gfs2_log_lock(sdp);
264
265 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
266 a = (old_tail <= ai->ai_first);
267 b = (ai->ai_first < new_tail);
268 rm = (wrap) ? (a || b) : (a && b);
269 if (!rm)
270 continue;
271
272 gfs2_ail2_empty_one(sdp, ai);
273 list_del(&ai->ai_list);
274 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
275 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
276 kfree(ai);
277 }
278
279 gfs2_log_unlock(sdp);
280}
281
282/**
283 * gfs2_log_reserve - Make a log reservation
284 * @sdp: The GFS2 superblock
285 * @blks: The number of blocks to reserve
286 *
89918647 287 * Note that we never give out the last few blocks of the journal. Thats
2332c443 288 * due to the fact that there is a small number of header blocks
b004157a
SW
289 * associated with each log flush. The exact number can't be known until
290 * flush time, so we ensure that we have just enough free blocks at all
291 * times to avoid running out during a log flush.
292 *
b3b94faa
DT
293 * Returns: errno
294 */
295
296int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
297{
b3b94faa 298 unsigned int try = 0;
89918647 299 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
b3b94faa
DT
300
301 if (gfs2_assert_warn(sdp, blks) ||
302 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
303 return -EINVAL;
304
71b86f56 305 mutex_lock(&sdp->sd_log_reserve_mutex);
484adff8 306 gfs2_log_lock(sdp);
fd041f0b 307 while(atomic_read(&sdp->sd_log_blks_free) <= (blks + reserved_blks)) {
b3b94faa 308 gfs2_log_unlock(sdp);
b3b94faa 309 gfs2_ail1_empty(sdp, 0);
b09e593d 310 gfs2_log_flush(sdp, NULL);
b3b94faa
DT
311
312 if (try++)
313 gfs2_ail1_start(sdp, 0);
484adff8 314 gfs2_log_lock(sdp);
b3b94faa 315 }
fd041f0b 316 atomic_sub(blks, &sdp->sd_log_blks_free);
484adff8
SW
317 gfs2_log_unlock(sdp);
318 mutex_unlock(&sdp->sd_log_reserve_mutex);
319
320 down_read(&sdp->sd_log_flush_lock);
b3b94faa
DT
321
322 return 0;
323}
324
325/**
326 * gfs2_log_release - Release a given number of log blocks
327 * @sdp: The GFS2 superblock
328 * @blks: The number of blocks
329 *
330 */
331
332void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
333{
b3b94faa
DT
334
335 gfs2_log_lock(sdp);
fd041f0b 336 atomic_add(blks, &sdp->sd_log_blks_free);
b3b94faa 337 gfs2_assert_withdraw(sdp,
fd041f0b 338 atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
b3b94faa 339 gfs2_log_unlock(sdp);
ed386507 340 up_read(&sdp->sd_log_flush_lock);
b3b94faa
DT
341}
342
cd915493 343static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
b3b94faa 344{
da6dd40d
BP
345 struct gfs2_journal_extent *je;
346
347 list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
348 if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
ff91cc9b 349 return je->dblock + lbn - je->lblock;
da6dd40d
BP
350 }
351
352 return -1;
b3b94faa
DT
353}
354
355/**
356 * log_distance - Compute distance between two journal blocks
357 * @sdp: The GFS2 superblock
358 * @newer: The most recent journal block of the pair
359 * @older: The older journal block of the pair
360 *
361 * Compute the distance (in the journal direction) between two
362 * blocks in the journal
363 *
364 * Returns: the distance in blocks
365 */
366
faa31ce8 367static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
b3b94faa
DT
368 unsigned int older)
369{
370 int dist;
371
372 dist = newer - older;
373 if (dist < 0)
374 dist += sdp->sd_jdesc->jd_blocks;
375
376 return dist;
377}
378
2332c443
RP
379/**
380 * calc_reserved - Calculate the number of blocks to reserve when
381 * refunding a transaction's unused buffers.
382 * @sdp: The GFS2 superblock
383 *
384 * This is complex. We need to reserve room for all our currently used
385 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
386 * all our journaled data buffers for journaled files (e.g. files in the
387 * meta_fs like rindex, or files for which chattr +j was done.)
388 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
389 * will count it as free space (sd_log_blks_free) and corruption will follow.
390 *
391 * We can have metadata bufs and jdata bufs in the same journal. So each
392 * type gets its own log header, for which we need to reserve a block.
393 * In fact, each type has the potential for needing more than one header
394 * in cases where we have more buffers than will fit on a journal page.
395 * Metadata journal entries take up half the space of journaled buffer entries.
396 * Thus, metadata entries have buf_limit (502) and journaled buffers have
397 * databuf_limit (251) before they cause a wrap around.
398 *
399 * Also, we need to reserve blocks for revoke journal entries and one for an
400 * overall header for the lot.
401 *
402 * Returns: the number of blocks reserved
403 */
404static unsigned int calc_reserved(struct gfs2_sbd *sdp)
405{
406 unsigned int reserved = 0;
407 unsigned int mbuf_limit, metabufhdrs_needed;
408 unsigned int dbuf_limit, databufhdrs_needed;
409 unsigned int revokes = 0;
410
411 mbuf_limit = buf_limit(sdp);
412 metabufhdrs_needed = (sdp->sd_log_commited_buf +
413 (mbuf_limit - 1)) / mbuf_limit;
414 dbuf_limit = databuf_limit(sdp);
415 databufhdrs_needed = (sdp->sd_log_commited_databuf +
416 (dbuf_limit - 1)) / dbuf_limit;
417
418 if (sdp->sd_log_commited_revoke)
419 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
420 sizeof(u64));
421
422 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
423 sdp->sd_log_commited_databuf + databufhdrs_needed +
424 revokes;
425 /* One for the overall header */
426 if (reserved)
427 reserved++;
428 return reserved;
429}
430
b3b94faa
DT
431static unsigned int current_tail(struct gfs2_sbd *sdp)
432{
433 struct gfs2_ail *ai;
434 unsigned int tail;
435
436 gfs2_log_lock(sdp);
437
faa31ce8 438 if (list_empty(&sdp->sd_ail1_list)) {
b3b94faa 439 tail = sdp->sd_log_head;
faa31ce8
SW
440 } else {
441 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
b3b94faa
DT
442 tail = ai->ai_first;
443 }
444
445 gfs2_log_unlock(sdp);
446
447 return tail;
448}
449
16615be1 450void gfs2_log_incr_head(struct gfs2_sbd *sdp)
b3b94faa
DT
451{
452 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
16615be1 453 BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
b3b94faa
DT
454
455 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
456 sdp->sd_log_flush_head = 0;
457 sdp->sd_log_flush_wrapped = 1;
458 }
459}
460
16615be1
SW
461/**
462 * gfs2_log_write_endio - End of I/O for a log buffer
463 * @bh: The buffer head
464 * @uptodate: I/O Status
465 *
466 */
467
468static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
469{
470 struct gfs2_sbd *sdp = bh->b_private;
471 bh->b_private = NULL;
472
473 end_buffer_write_sync(bh, uptodate);
474 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
475 wake_up(&sdp->sd_log_flush_wait);
476}
477
b3b94faa
DT
478/**
479 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
480 * @sdp: The GFS2 superblock
481 *
482 * Returns: the buffer_head
483 */
484
485struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
486{
cd915493 487 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
b3b94faa
DT
488 struct buffer_head *bh;
489
16615be1 490 bh = sb_getblk(sdp->sd_vfs, blkno);
b3b94faa
DT
491 lock_buffer(bh);
492 memset(bh->b_data, 0, bh->b_size);
493 set_buffer_uptodate(bh);
494 clear_buffer_dirty(bh);
16615be1
SW
495 gfs2_log_incr_head(sdp);
496 atomic_inc(&sdp->sd_log_in_flight);
497 bh->b_private = sdp;
498 bh->b_end_io = gfs2_log_write_endio;
b3b94faa
DT
499
500 return bh;
501}
502
16615be1
SW
503/**
504 * gfs2_fake_write_endio -
505 * @bh: The buffer head
506 * @uptodate: The I/O Status
507 *
508 */
509
510static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
511{
512 struct buffer_head *real_bh = bh->b_private;
5a60c532
SW
513 struct gfs2_bufdata *bd = real_bh->b_private;
514 struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
16615be1
SW
515
516 end_buffer_write_sync(bh, uptodate);
517 free_buffer_head(bh);
518 unlock_buffer(real_bh);
519 brelse(real_bh);
520 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
521 wake_up(&sdp->sd_log_flush_wait);
522}
523
b3b94faa
DT
524/**
525 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
526 * @sdp: the filesystem
527 * @data: the data the buffer_head should point to
528 *
529 * Returns: the log buffer descriptor
530 */
531
532struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
533 struct buffer_head *real)
534{
cd915493 535 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
b3b94faa
DT
536 struct buffer_head *bh;
537
16615be1 538 bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
b3b94faa 539 atomic_set(&bh->b_count, 1);
16615be1 540 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
18ec7d5c 541 set_bh_page(bh, real->b_page, bh_offset(real));
b3b94faa
DT
542 bh->b_blocknr = blkno;
543 bh->b_size = sdp->sd_sb.sb_bsize;
544 bh->b_bdev = sdp->sd_vfs->s_bdev;
16615be1
SW
545 bh->b_private = real;
546 bh->b_end_io = gfs2_fake_write_endio;
b3b94faa 547
16615be1
SW
548 gfs2_log_incr_head(sdp);
549 atomic_inc(&sdp->sd_log_in_flight);
b3b94faa
DT
550
551 return bh;
552}
553
2332c443 554static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
b3b94faa
DT
555{
556 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
557
558 ail2_empty(sdp, new_tail);
559
560 gfs2_log_lock(sdp);
fd041f0b
SW
561 atomic_add(dist, &sdp->sd_log_blks_free);
562 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
b3b94faa
DT
563 gfs2_log_unlock(sdp);
564
565 sdp->sd_log_tail = new_tail;
566}
567
568/**
569 * log_write_header - Get and initialize a journal header buffer
570 * @sdp: The GFS2 superblock
571 *
572 * Returns: the initialized log buffer descriptor
573 */
574
cd915493 575static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
b3b94faa 576{
cd915493 577 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
b3b94faa
DT
578 struct buffer_head *bh;
579 struct gfs2_log_header *lh;
580 unsigned int tail;
cd915493 581 u32 hash;
b3b94faa 582
b3b94faa
DT
583 bh = sb_getblk(sdp->sd_vfs, blkno);
584 lock_buffer(bh);
585 memset(bh->b_data, 0, bh->b_size);
586 set_buffer_uptodate(bh);
587 clear_buffer_dirty(bh);
b3b94faa
DT
588
589 gfs2_ail1_empty(sdp, 0);
590 tail = current_tail(sdp);
591
592 lh = (struct gfs2_log_header *)bh->b_data;
593 memset(lh, 0, sizeof(struct gfs2_log_header));
594 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
e3167ded
SW
595 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
596 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
e0f2bf78
SW
597 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
598 lh->lh_flags = cpu_to_be32(flags);
599 lh->lh_tail = cpu_to_be32(tail);
600 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
b3b94faa
DT
601 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
602 lh->lh_hash = cpu_to_be32(hash);
603
254db57f
SW
604 bh->b_end_io = end_buffer_write_sync;
605 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
606 goto skip_barrier;
607 get_bh(bh);
608 submit_bh(WRITE_BARRIER | (1 << BIO_RW_META), bh);
609 wait_on_buffer(bh);
610 if (buffer_eopnotsupp(bh)) {
611 clear_buffer_eopnotsupp(bh);
612 set_buffer_uptodate(bh);
613 set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
614 lock_buffer(bh);
615skip_barrier:
616 get_bh(bh);
617 submit_bh(WRITE_SYNC | (1 << BIO_RW_META), bh);
618 wait_on_buffer(bh);
619 }
620 if (!buffer_uptodate(bh))
b3b94faa
DT
621 gfs2_io_error_bh(sdp, bh);
622 brelse(bh);
623
624 if (sdp->sd_log_tail != tail)
2332c443 625 log_pull_tail(sdp, tail);
b3b94faa
DT
626 else
627 gfs2_assert_withdraw(sdp, !pull);
628
629 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
16615be1 630 gfs2_log_incr_head(sdp);
b3b94faa
DT
631}
632
633static void log_flush_commit(struct gfs2_sbd *sdp)
634{
16615be1
SW
635 DEFINE_WAIT(wait);
636
637 if (atomic_read(&sdp->sd_log_in_flight)) {
638 do {
639 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
640 TASK_UNINTERRUPTIBLE);
641 if (atomic_read(&sdp->sd_log_in_flight))
642 io_schedule();
643 } while(atomic_read(&sdp->sd_log_in_flight));
644 finish_wait(&sdp->sd_log_flush_wait, &wait);
b3b94faa
DT
645 }
646
16615be1 647 log_write_header(sdp, 0, 0);
b3b94faa
DT
648}
649
d7b616e2
SW
650static void gfs2_ordered_write(struct gfs2_sbd *sdp)
651{
652 struct gfs2_bufdata *bd;
653 struct buffer_head *bh;
654 LIST_HEAD(written);
655
656 gfs2_log_lock(sdp);
657 while (!list_empty(&sdp->sd_log_le_ordered)) {
658 bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
659 list_move(&bd->bd_le.le_list, &written);
660 bh = bd->bd_bh;
661 if (!buffer_dirty(bh))
662 continue;
663 get_bh(bh);
664 gfs2_log_unlock(sdp);
665 lock_buffer(bh);
b8e7cbb6 666 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
d7b616e2
SW
667 bh->b_end_io = end_buffer_write_sync;
668 submit_bh(WRITE, bh);
669 } else {
670 unlock_buffer(bh);
671 brelse(bh);
672 }
673 gfs2_log_lock(sdp);
674 }
675 list_splice(&written, &sdp->sd_log_le_ordered);
676 gfs2_log_unlock(sdp);
677}
678
679static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
680{
681 struct gfs2_bufdata *bd;
682 struct buffer_head *bh;
683
684 gfs2_log_lock(sdp);
685 while (!list_empty(&sdp->sd_log_le_ordered)) {
686 bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
687 bh = bd->bd_bh;
688 if (buffer_locked(bh)) {
689 get_bh(bh);
690 gfs2_log_unlock(sdp);
691 wait_on_buffer(bh);
692 brelse(bh);
693 gfs2_log_lock(sdp);
694 continue;
695 }
696 list_del_init(&bd->bd_le.le_list);
697 }
698 gfs2_log_unlock(sdp);
699}
700
b3b94faa 701/**
b09e593d 702 * gfs2_log_flush - flush incore transaction(s)
b3b94faa
DT
703 * @sdp: the filesystem
704 * @gl: The glock structure to flush. If NULL, flush the whole incore log
705 *
706 */
707
2bcd610d 708void __gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
b3b94faa
DT
709{
710 struct gfs2_ail *ai;
711
484adff8 712 down_write(&sdp->sd_log_flush_lock);
f55ab26a 713
2bcd610d
SW
714 /* Log might have been flushed while we waited for the flush lock */
715 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
716 up_write(&sdp->sd_log_flush_lock);
717 return;
f55ab26a
SW
718 }
719
b09e593d
SW
720 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
721 INIT_LIST_HEAD(&ai->ai_ail1_list);
722 INIT_LIST_HEAD(&ai->ai_ail2_list);
b3b94faa 723
16615be1
SW
724 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
725 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
726 sdp->sd_log_commited_buf);
727 gfs2_assert_withdraw(sdp, 0);
728 }
729 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
730 printk(KERN_INFO "GFS2: log databuf %u %u\n",
731 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
732 gfs2_assert_withdraw(sdp, 0);
733 }
b3b94faa
DT
734 gfs2_assert_withdraw(sdp,
735 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
736
b3b94faa
DT
737 sdp->sd_log_flush_head = sdp->sd_log_head;
738 sdp->sd_log_flush_wrapped = 0;
739 ai->ai_first = sdp->sd_log_flush_head;
740
d7b616e2 741 gfs2_ordered_write(sdp);
b3b94faa 742 lops_before_commit(sdp);
d7b616e2
SW
743 gfs2_ordered_wait(sdp);
744
16615be1 745 if (sdp->sd_log_head != sdp->sd_log_flush_head)
b3b94faa 746 log_flush_commit(sdp);
2332c443
RP
747 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
748 gfs2_log_lock(sdp);
fd041f0b 749 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
2332c443 750 gfs2_log_unlock(sdp);
b3b94faa 751 log_write_header(sdp, 0, PULL);
2332c443 752 }
b3b94faa 753 lops_after_commit(sdp, ai);
b09e593d 754
fe1a698f
SW
755 gfs2_log_lock(sdp);
756 sdp->sd_log_head = sdp->sd_log_flush_head;
faa31ce8
SW
757 sdp->sd_log_blks_reserved = 0;
758 sdp->sd_log_commited_buf = 0;
2332c443 759 sdp->sd_log_commited_databuf = 0;
faa31ce8 760 sdp->sd_log_commited_revoke = 0;
b3b94faa 761
b3b94faa
DT
762 if (!list_empty(&ai->ai_ail1_list)) {
763 list_add(&ai->ai_list, &sdp->sd_ail1_list);
764 ai = NULL;
765 }
766 gfs2_log_unlock(sdp);
767
b3b94faa 768 sdp->sd_vfs->s_dirt = 0;
484adff8 769 up_write(&sdp->sd_log_flush_lock);
b3b94faa
DT
770
771 kfree(ai);
772}
773
774static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
775{
2332c443 776 unsigned int reserved;
ac39aadd 777 unsigned int unused;
b3b94faa
DT
778
779 gfs2_log_lock(sdp);
780
781 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
2332c443
RP
782 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
783 tr->tr_num_databuf_rm;
784 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
785 (((int)sdp->sd_log_commited_databuf) >= 0));
b3b94faa
DT
786 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
787 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
2332c443 788 reserved = calc_reserved(sdp);
62be1f71 789 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
ac39aadd 790 unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
ac39aadd 791 atomic_add(unused, &sdp->sd_log_blks_free);
fd041f0b 792 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
2332c443 793 sdp->sd_jdesc->jd_blocks);
b3b94faa
DT
794 sdp->sd_log_blks_reserved = reserved;
795
796 gfs2_log_unlock(sdp);
797}
798
d0109bfa
BP
799static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
800{
801 struct list_head *head = &tr->tr_list_buf;
802 struct gfs2_bufdata *bd;
803
804 gfs2_log_lock(sdp);
805 while (!list_empty(head)) {
806 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
807 list_del_init(&bd->bd_list_tr);
808 tr->tr_num_buf--;
809 }
810 gfs2_log_unlock(sdp);
811 gfs2_assert_warn(sdp, !tr->tr_num_buf);
812}
813
b3b94faa
DT
814/**
815 * gfs2_log_commit - Commit a transaction to the log
816 * @sdp: the filesystem
817 * @tr: the transaction
818 *
819 * Returns: errno
820 */
821
822void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
823{
824 log_refund(sdp, tr);
d0109bfa 825 buf_lo_incore_commit(sdp, tr);
b3b94faa
DT
826
827 sdp->sd_vfs->s_dirt = 1;
484adff8 828 up_read(&sdp->sd_log_flush_lock);
b3b94faa 829
b3b94faa 830 gfs2_log_lock(sdp);
b004157a
SW
831 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
832 wake_up_process(sdp->sd_logd_process);
833 gfs2_log_unlock(sdp);
b3b94faa
DT
834}
835
836/**
837 * gfs2_log_shutdown - write a shutdown header into a journal
838 * @sdp: the filesystem
839 *
840 */
841
842void gfs2_log_shutdown(struct gfs2_sbd *sdp)
843{
484adff8 844 down_write(&sdp->sd_log_flush_lock);
b3b94faa 845
b3b94faa 846 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
b3b94faa
DT
847 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
848 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
849 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
850 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
851 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
852
853 sdp->sd_log_flush_head = sdp->sd_log_head;
854 sdp->sd_log_flush_wrapped = 0;
855
2332c443
RP
856 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
857 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
b3b94faa 858
fd041f0b 859 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
a74604be
SW
860 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
861 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
b3b94faa
DT
862
863 sdp->sd_log_head = sdp->sd_log_flush_head;
b3b94faa
DT
864 sdp->sd_log_tail = sdp->sd_log_head;
865
484adff8 866 up_write(&sdp->sd_log_flush_lock);
b3b94faa
DT
867}
868
a25311c8
SW
869
870/**
871 * gfs2_meta_syncfs - sync all the buffers in a filesystem
872 * @sdp: the filesystem
873 *
874 */
875
876void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
877{
878 gfs2_log_flush(sdp, NULL);
879 for (;;) {
880 gfs2_ail1_start(sdp, DIO_ALL);
881 if (gfs2_ail1_empty(sdp, DIO_ALL))
882 break;
883 msleep(10);
884 }
885}
886
ec69b188
SW
887
888/**
889 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
890 * @sdp: Pointer to GFS2 superblock
891 *
892 * Also, periodically check to make sure that we're using the most recent
893 * journal index.
894 */
895
896int gfs2_logd(void *data)
897{
898 struct gfs2_sbd *sdp = data;
ec69b188
SW
899 unsigned long t;
900 int need_flush;
901
902 while (!kthread_should_stop()) {
903 /* Advance the log tail */
904
905 t = sdp->sd_log_flush_time +
906 gfs2_tune_get(sdp, gt_log_flush_secs) * HZ;
907
908 gfs2_ail1_empty(sdp, DIO_ALL);
909 gfs2_log_lock(sdp);
910 need_flush = sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks);
911 gfs2_log_unlock(sdp);
912 if (need_flush || time_after_eq(jiffies, t)) {
913 gfs2_log_flush(sdp, NULL);
914 sdp->sd_log_flush_time = jiffies;
915 }
916
ec69b188
SW
917 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
918 if (freezing(current))
919 refrigerator();
920 schedule_timeout_interruptible(t);
921 }
922
923 return 0;
924}
925