usb: renesas_usbhs: don't re-allocation pipe buffer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / gfs2 / log.c
blob5b102c1887fd92ffd9b1c14dc01458f112736973
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
8 */
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>
15 #include <linux/gfs2_ondisk.h>
16 #include <linux/crc32.h>
17 #include <linux/delay.h>
18 #include <linux/kthread.h>
19 #include <linux/freezer.h>
20 #include <linux/bio.h>
22 #include "gfs2.h"
23 #include "incore.h"
24 #include "bmap.h"
25 #include "glock.h"
26 #include "log.h"
27 #include "lops.h"
28 #include "meta_io.h"
29 #include "util.h"
30 #include "dir.h"
31 #include "trace_gfs2.h"
33 #define PULL 1
35 /**
36 * gfs2_struct2blk - compute stuff
37 * @sdp: the filesystem
38 * @nstruct: the number of structures
39 * @ssize: the size of the structures
41 * Compute the number of log descriptor blocks needed to hold a certain number
42 * of structures of a certain size.
44 * Returns: the number of blocks needed (minimum is always 1)
47 unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
48 unsigned int ssize)
50 unsigned int blks;
51 unsigned int first, second;
53 blks = 1;
54 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
56 if (nstruct > first) {
57 second = (sdp->sd_sb.sb_bsize -
58 sizeof(struct gfs2_meta_header)) / ssize;
59 blks += DIV_ROUND_UP(nstruct - first, second);
62 return blks;
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
70 * The ail lock _must_ be held when calling this function
74 void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
76 bd->bd_ail = NULL;
77 list_del_init(&bd->bd_ail_st_list);
78 list_del_init(&bd->bd_ail_gl_list);
79 atomic_dec(&bd->bd_gl->gl_ail_count);
80 brelse(bd->bd_bh);
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
90 static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
91 __releases(&sdp->sd_ail_lock)
92 __acquires(&sdp->sd_ail_lock)
94 struct gfs2_bufdata *bd, *s;
95 struct buffer_head *bh;
96 int retry;
98 do {
99 retry = 0;
101 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
102 bd_ail_st_list) {
103 bh = bd->bd_bh;
105 gfs2_assert(sdp, bd->bd_ail == ai);
107 if (!buffer_busy(bh)) {
108 if (!buffer_uptodate(bh))
109 gfs2_io_error_bh(sdp, bh);
110 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
111 continue;
114 if (!buffer_dirty(bh))
115 continue;
117 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
119 get_bh(bh);
120 spin_unlock(&sdp->sd_ail_lock);
121 lock_buffer(bh);
122 if (test_clear_buffer_dirty(bh)) {
123 bh->b_end_io = end_buffer_write_sync;
124 submit_bh(WRITE_SYNC, bh);
125 } else {
126 unlock_buffer(bh);
127 brelse(bh);
129 spin_lock(&sdp->sd_ail_lock);
131 retry = 1;
132 break;
134 } while (retry);
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
144 static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
146 struct gfs2_bufdata *bd, *s;
147 struct buffer_head *bh;
149 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
150 bd_ail_st_list) {
151 bh = bd->bd_bh;
153 gfs2_assert(sdp, bd->bd_ail == ai);
155 if (buffer_busy(bh)) {
156 if (flags & DIO_ALL)
157 continue;
158 else
159 break;
162 if (!buffer_uptodate(bh))
163 gfs2_io_error_bh(sdp, bh);
165 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
168 return list_empty(&ai->ai_ail1_list);
171 static void gfs2_ail1_start(struct gfs2_sbd *sdp)
173 struct list_head *head;
174 u64 sync_gen;
175 struct gfs2_ail *ai;
176 int done = 0;
178 spin_lock(&sdp->sd_ail_lock);
179 head = &sdp->sd_ail1_list;
180 if (list_empty(head)) {
181 spin_unlock(&sdp->sd_ail_lock);
182 return;
184 sync_gen = sdp->sd_ail_sync_gen++;
186 while(!done) {
187 done = 1;
188 list_for_each_entry_reverse(ai, head, ai_list) {
189 if (ai->ai_sync_gen >= sync_gen)
190 continue;
191 ai->ai_sync_gen = sync_gen;
192 gfs2_ail1_start_one(sdp, ai); /* This may drop ail lock */
193 done = 0;
194 break;
198 spin_unlock(&sdp->sd_ail_lock);
201 static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
203 struct gfs2_ail *ai, *s;
204 int ret;
206 spin_lock(&sdp->sd_ail_lock);
208 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
209 if (gfs2_ail1_empty_one(sdp, ai, flags))
210 list_move(&ai->ai_list, &sdp->sd_ail2_list);
211 else if (!(flags & DIO_ALL))
212 break;
215 ret = list_empty(&sdp->sd_ail1_list);
217 spin_unlock(&sdp->sd_ail_lock);
219 return ret;
224 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
225 * @sdp: the filesystem
226 * @ai: the AIL entry
230 static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
232 struct list_head *head = &ai->ai_ail2_list;
233 struct gfs2_bufdata *bd;
235 while (!list_empty(head)) {
236 bd = list_entry(head->prev, struct gfs2_bufdata,
237 bd_ail_st_list);
238 gfs2_assert(sdp, bd->bd_ail == ai);
239 gfs2_remove_from_ail(bd);
243 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
245 struct gfs2_ail *ai, *safe;
246 unsigned int old_tail = sdp->sd_log_tail;
247 int wrap = (new_tail < old_tail);
248 int a, b, rm;
250 spin_lock(&sdp->sd_ail_lock);
252 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
253 a = (old_tail <= ai->ai_first);
254 b = (ai->ai_first < new_tail);
255 rm = (wrap) ? (a || b) : (a && b);
256 if (!rm)
257 continue;
259 gfs2_ail2_empty_one(sdp, ai);
260 list_del(&ai->ai_list);
261 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
262 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
263 kfree(ai);
266 spin_unlock(&sdp->sd_ail_lock);
270 * gfs2_log_reserve - Make a log reservation
271 * @sdp: The GFS2 superblock
272 * @blks: The number of blocks to reserve
274 * Note that we never give out the last few blocks of the journal. Thats
275 * due to the fact that there is a small number of header blocks
276 * associated with each log flush. The exact number can't be known until
277 * flush time, so we ensure that we have just enough free blocks at all
278 * times to avoid running out during a log flush.
280 * We no longer flush the log here, instead we wake up logd to do that
281 * for us. To avoid the thundering herd and to ensure that we deal fairly
282 * with queued waiters, we use an exclusive wait. This means that when we
283 * get woken with enough journal space to get our reservation, we need to
284 * wake the next waiter on the list.
286 * Returns: errno
289 int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
291 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
292 unsigned wanted = blks + reserved_blks;
293 DEFINE_WAIT(wait);
294 int did_wait = 0;
295 unsigned int free_blocks;
297 if (gfs2_assert_warn(sdp, blks) ||
298 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
299 return -EINVAL;
300 retry:
301 free_blocks = atomic_read(&sdp->sd_log_blks_free);
302 if (unlikely(free_blocks <= wanted)) {
303 do {
304 prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
305 TASK_UNINTERRUPTIBLE);
306 wake_up(&sdp->sd_logd_waitq);
307 did_wait = 1;
308 if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
309 io_schedule();
310 free_blocks = atomic_read(&sdp->sd_log_blks_free);
311 } while(free_blocks <= wanted);
312 finish_wait(&sdp->sd_log_waitq, &wait);
314 if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
315 free_blocks - blks) != free_blocks)
316 goto retry;
317 trace_gfs2_log_blocks(sdp, -blks);
320 * If we waited, then so might others, wake them up _after_ we get
321 * our share of the log.
323 if (unlikely(did_wait))
324 wake_up(&sdp->sd_log_waitq);
326 down_read(&sdp->sd_log_flush_lock);
328 return 0;
331 static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
333 struct gfs2_journal_extent *je;
335 list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
336 if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
337 return je->dblock + lbn - je->lblock;
340 return -1;
344 * log_distance - Compute distance between two journal blocks
345 * @sdp: The GFS2 superblock
346 * @newer: The most recent journal block of the pair
347 * @older: The older journal block of the pair
349 * Compute the distance (in the journal direction) between two
350 * blocks in the journal
352 * Returns: the distance in blocks
355 static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
356 unsigned int older)
358 int dist;
360 dist = newer - older;
361 if (dist < 0)
362 dist += sdp->sd_jdesc->jd_blocks;
364 return dist;
368 * calc_reserved - Calculate the number of blocks to reserve when
369 * refunding a transaction's unused buffers.
370 * @sdp: The GFS2 superblock
372 * This is complex. We need to reserve room for all our currently used
373 * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
374 * all our journaled data buffers for journaled files (e.g. files in the
375 * meta_fs like rindex, or files for which chattr +j was done.)
376 * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
377 * will count it as free space (sd_log_blks_free) and corruption will follow.
379 * We can have metadata bufs and jdata bufs in the same journal. So each
380 * type gets its own log header, for which we need to reserve a block.
381 * In fact, each type has the potential for needing more than one header
382 * in cases where we have more buffers than will fit on a journal page.
383 * Metadata journal entries take up half the space of journaled buffer entries.
384 * Thus, metadata entries have buf_limit (502) and journaled buffers have
385 * databuf_limit (251) before they cause a wrap around.
387 * Also, we need to reserve blocks for revoke journal entries and one for an
388 * overall header for the lot.
390 * Returns: the number of blocks reserved
392 static unsigned int calc_reserved(struct gfs2_sbd *sdp)
394 unsigned int reserved = 0;
395 unsigned int mbuf_limit, metabufhdrs_needed;
396 unsigned int dbuf_limit, databufhdrs_needed;
397 unsigned int revokes = 0;
399 mbuf_limit = buf_limit(sdp);
400 metabufhdrs_needed = (sdp->sd_log_commited_buf +
401 (mbuf_limit - 1)) / mbuf_limit;
402 dbuf_limit = databuf_limit(sdp);
403 databufhdrs_needed = (sdp->sd_log_commited_databuf +
404 (dbuf_limit - 1)) / dbuf_limit;
406 if (sdp->sd_log_commited_revoke > 0)
407 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
408 sizeof(u64));
410 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
411 sdp->sd_log_commited_databuf + databufhdrs_needed +
412 revokes;
413 /* One for the overall header */
414 if (reserved)
415 reserved++;
416 return reserved;
419 static unsigned int current_tail(struct gfs2_sbd *sdp)
421 struct gfs2_ail *ai;
422 unsigned int tail;
424 spin_lock(&sdp->sd_ail_lock);
426 if (list_empty(&sdp->sd_ail1_list)) {
427 tail = sdp->sd_log_head;
428 } else {
429 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
430 tail = ai->ai_first;
433 spin_unlock(&sdp->sd_ail_lock);
435 return tail;
438 void gfs2_log_incr_head(struct gfs2_sbd *sdp)
440 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
441 BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
443 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
444 sdp->sd_log_flush_head = 0;
445 sdp->sd_log_flush_wrapped = 1;
450 * gfs2_log_write_endio - End of I/O for a log buffer
451 * @bh: The buffer head
452 * @uptodate: I/O Status
456 static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
458 struct gfs2_sbd *sdp = bh->b_private;
459 bh->b_private = NULL;
461 end_buffer_write_sync(bh, uptodate);
462 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
463 wake_up(&sdp->sd_log_flush_wait);
467 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
468 * @sdp: The GFS2 superblock
470 * Returns: the buffer_head
473 struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
475 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
476 struct buffer_head *bh;
478 bh = sb_getblk(sdp->sd_vfs, blkno);
479 lock_buffer(bh);
480 memset(bh->b_data, 0, bh->b_size);
481 set_buffer_uptodate(bh);
482 clear_buffer_dirty(bh);
483 gfs2_log_incr_head(sdp);
484 atomic_inc(&sdp->sd_log_in_flight);
485 bh->b_private = sdp;
486 bh->b_end_io = gfs2_log_write_endio;
488 return bh;
492 * gfs2_fake_write_endio -
493 * @bh: The buffer head
494 * @uptodate: The I/O Status
498 static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
500 struct buffer_head *real_bh = bh->b_private;
501 struct gfs2_bufdata *bd = real_bh->b_private;
502 struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
504 end_buffer_write_sync(bh, uptodate);
505 free_buffer_head(bh);
506 unlock_buffer(real_bh);
507 brelse(real_bh);
508 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
509 wake_up(&sdp->sd_log_flush_wait);
513 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
514 * @sdp: the filesystem
515 * @data: the data the buffer_head should point to
517 * Returns: the log buffer descriptor
520 struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
521 struct buffer_head *real)
523 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
524 struct buffer_head *bh;
526 bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
527 atomic_set(&bh->b_count, 1);
528 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
529 set_bh_page(bh, real->b_page, bh_offset(real));
530 bh->b_blocknr = blkno;
531 bh->b_size = sdp->sd_sb.sb_bsize;
532 bh->b_bdev = sdp->sd_vfs->s_bdev;
533 bh->b_private = real;
534 bh->b_end_io = gfs2_fake_write_endio;
536 gfs2_log_incr_head(sdp);
537 atomic_inc(&sdp->sd_log_in_flight);
539 return bh;
542 static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
544 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
546 ail2_empty(sdp, new_tail);
548 atomic_add(dist, &sdp->sd_log_blks_free);
549 trace_gfs2_log_blocks(sdp, dist);
550 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
551 sdp->sd_jdesc->jd_blocks);
553 sdp->sd_log_tail = new_tail;
557 * log_write_header - Get and initialize a journal header buffer
558 * @sdp: The GFS2 superblock
560 * Returns: the initialized log buffer descriptor
563 static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
565 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
566 struct buffer_head *bh;
567 struct gfs2_log_header *lh;
568 unsigned int tail;
569 u32 hash;
571 bh = sb_getblk(sdp->sd_vfs, blkno);
572 lock_buffer(bh);
573 memset(bh->b_data, 0, bh->b_size);
574 set_buffer_uptodate(bh);
575 clear_buffer_dirty(bh);
577 gfs2_ail1_empty(sdp, 0);
578 tail = current_tail(sdp);
580 lh = (struct gfs2_log_header *)bh->b_data;
581 memset(lh, 0, sizeof(struct gfs2_log_header));
582 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
583 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
584 lh->lh_header.__pad0 = cpu_to_be64(0);
585 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
586 lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
587 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
588 lh->lh_flags = cpu_to_be32(flags);
589 lh->lh_tail = cpu_to_be32(tail);
590 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
591 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
592 lh->lh_hash = cpu_to_be32(hash);
594 bh->b_end_io = end_buffer_write_sync;
595 get_bh(bh);
596 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
597 submit_bh(WRITE_SYNC | REQ_META, bh);
598 else
599 submit_bh(WRITE_FLUSH_FUA | REQ_META, bh);
600 wait_on_buffer(bh);
602 if (!buffer_uptodate(bh))
603 gfs2_io_error_bh(sdp, bh);
604 brelse(bh);
606 if (sdp->sd_log_tail != tail)
607 log_pull_tail(sdp, tail);
608 else
609 gfs2_assert_withdraw(sdp, !pull);
611 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
612 gfs2_log_incr_head(sdp);
615 static void log_flush_commit(struct gfs2_sbd *sdp)
617 DEFINE_WAIT(wait);
619 if (atomic_read(&sdp->sd_log_in_flight)) {
620 do {
621 prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
622 TASK_UNINTERRUPTIBLE);
623 if (atomic_read(&sdp->sd_log_in_flight))
624 io_schedule();
625 } while(atomic_read(&sdp->sd_log_in_flight));
626 finish_wait(&sdp->sd_log_flush_wait, &wait);
629 log_write_header(sdp, 0, 0);
632 static void gfs2_ordered_write(struct gfs2_sbd *sdp)
634 struct gfs2_bufdata *bd;
635 struct buffer_head *bh;
636 LIST_HEAD(written);
638 gfs2_log_lock(sdp);
639 while (!list_empty(&sdp->sd_log_le_ordered)) {
640 bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
641 list_move(&bd->bd_le.le_list, &written);
642 bh = bd->bd_bh;
643 if (!buffer_dirty(bh))
644 continue;
645 get_bh(bh);
646 gfs2_log_unlock(sdp);
647 lock_buffer(bh);
648 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
649 bh->b_end_io = end_buffer_write_sync;
650 submit_bh(WRITE_SYNC, bh);
651 } else {
652 unlock_buffer(bh);
653 brelse(bh);
655 gfs2_log_lock(sdp);
657 list_splice(&written, &sdp->sd_log_le_ordered);
658 gfs2_log_unlock(sdp);
661 static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
663 struct gfs2_bufdata *bd;
664 struct buffer_head *bh;
666 gfs2_log_lock(sdp);
667 while (!list_empty(&sdp->sd_log_le_ordered)) {
668 bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
669 bh = bd->bd_bh;
670 if (buffer_locked(bh)) {
671 get_bh(bh);
672 gfs2_log_unlock(sdp);
673 wait_on_buffer(bh);
674 brelse(bh);
675 gfs2_log_lock(sdp);
676 continue;
678 list_del_init(&bd->bd_le.le_list);
680 gfs2_log_unlock(sdp);
684 * gfs2_log_flush - flush incore transaction(s)
685 * @sdp: the filesystem
686 * @gl: The glock structure to flush. If NULL, flush the whole incore log
690 void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
692 struct gfs2_ail *ai;
694 down_write(&sdp->sd_log_flush_lock);
696 /* Log might have been flushed while we waited for the flush lock */
697 if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
698 up_write(&sdp->sd_log_flush_lock);
699 return;
701 trace_gfs2_log_flush(sdp, 1);
703 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
704 INIT_LIST_HEAD(&ai->ai_ail1_list);
705 INIT_LIST_HEAD(&ai->ai_ail2_list);
707 if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
708 printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
709 sdp->sd_log_commited_buf);
710 gfs2_assert_withdraw(sdp, 0);
712 if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
713 printk(KERN_INFO "GFS2: log databuf %u %u\n",
714 sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
715 gfs2_assert_withdraw(sdp, 0);
717 gfs2_assert_withdraw(sdp,
718 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
720 sdp->sd_log_flush_head = sdp->sd_log_head;
721 sdp->sd_log_flush_wrapped = 0;
722 ai->ai_first = sdp->sd_log_flush_head;
724 gfs2_ordered_write(sdp);
725 lops_before_commit(sdp);
726 gfs2_ordered_wait(sdp);
728 if (sdp->sd_log_head != sdp->sd_log_flush_head)
729 log_flush_commit(sdp);
730 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
731 gfs2_log_lock(sdp);
732 atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
733 trace_gfs2_log_blocks(sdp, -1);
734 gfs2_log_unlock(sdp);
735 log_write_header(sdp, 0, PULL);
737 lops_after_commit(sdp, ai);
739 gfs2_log_lock(sdp);
740 sdp->sd_log_head = sdp->sd_log_flush_head;
741 sdp->sd_log_blks_reserved = 0;
742 sdp->sd_log_commited_buf = 0;
743 sdp->sd_log_commited_databuf = 0;
744 sdp->sd_log_commited_revoke = 0;
746 spin_lock(&sdp->sd_ail_lock);
747 if (!list_empty(&ai->ai_ail1_list)) {
748 list_add(&ai->ai_list, &sdp->sd_ail1_list);
749 ai = NULL;
751 spin_unlock(&sdp->sd_ail_lock);
752 gfs2_log_unlock(sdp);
753 trace_gfs2_log_flush(sdp, 0);
754 up_write(&sdp->sd_log_flush_lock);
756 kfree(ai);
759 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
761 unsigned int reserved;
762 unsigned int unused;
764 gfs2_log_lock(sdp);
766 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
767 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
768 tr->tr_num_databuf_rm;
769 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
770 (((int)sdp->sd_log_commited_databuf) >= 0));
771 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
772 reserved = calc_reserved(sdp);
773 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
774 unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
775 atomic_add(unused, &sdp->sd_log_blks_free);
776 trace_gfs2_log_blocks(sdp, unused);
777 gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
778 sdp->sd_jdesc->jd_blocks);
779 sdp->sd_log_blks_reserved = reserved;
781 gfs2_log_unlock(sdp);
784 static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
786 struct list_head *head = &tr->tr_list_buf;
787 struct gfs2_bufdata *bd;
789 gfs2_log_lock(sdp);
790 while (!list_empty(head)) {
791 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
792 list_del_init(&bd->bd_list_tr);
793 tr->tr_num_buf--;
795 gfs2_log_unlock(sdp);
796 gfs2_assert_warn(sdp, !tr->tr_num_buf);
800 * gfs2_log_commit - Commit a transaction to the log
801 * @sdp: the filesystem
802 * @tr: the transaction
804 * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
805 * or the total number of used blocks (pinned blocks plus AIL blocks)
806 * is greater than thresh2.
808 * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
809 * journal size.
811 * Returns: errno
814 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
816 log_refund(sdp, tr);
817 buf_lo_incore_commit(sdp, tr);
819 up_read(&sdp->sd_log_flush_lock);
821 if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
822 ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
823 atomic_read(&sdp->sd_log_thresh2)))
824 wake_up(&sdp->sd_logd_waitq);
828 * gfs2_log_shutdown - write a shutdown header into a journal
829 * @sdp: the filesystem
833 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
835 down_write(&sdp->sd_log_flush_lock);
837 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
838 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
839 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
840 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
841 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
842 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
844 sdp->sd_log_flush_head = sdp->sd_log_head;
845 sdp->sd_log_flush_wrapped = 0;
847 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
848 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
850 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
851 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
852 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
854 sdp->sd_log_head = sdp->sd_log_flush_head;
855 sdp->sd_log_tail = sdp->sd_log_head;
857 up_write(&sdp->sd_log_flush_lock);
862 * gfs2_meta_syncfs - sync all the buffers in a filesystem
863 * @sdp: the filesystem
867 void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
869 gfs2_log_flush(sdp, NULL);
870 for (;;) {
871 gfs2_ail1_start(sdp);
872 if (gfs2_ail1_empty(sdp, DIO_ALL))
873 break;
874 msleep(10);
878 static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
880 return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
883 static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
885 unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
886 return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
890 * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
891 * @sdp: Pointer to GFS2 superblock
893 * Also, periodically check to make sure that we're using the most recent
894 * journal index.
897 int gfs2_logd(void *data)
899 struct gfs2_sbd *sdp = data;
900 unsigned long t = 1;
901 DEFINE_WAIT(wait);
902 unsigned preflush;
904 while (!kthread_should_stop()) {
906 preflush = atomic_read(&sdp->sd_log_pinned);
907 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
908 gfs2_ail1_empty(sdp, DIO_ALL);
909 gfs2_log_flush(sdp, NULL);
910 gfs2_ail1_empty(sdp, DIO_ALL);
913 if (gfs2_ail_flush_reqd(sdp)) {
914 gfs2_ail1_start(sdp);
915 io_schedule();
916 gfs2_ail1_empty(sdp, 0);
917 gfs2_log_flush(sdp, NULL);
918 gfs2_ail1_empty(sdp, DIO_ALL);
921 wake_up(&sdp->sd_log_waitq);
922 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
923 if (freezing(current))
924 refrigerator();
926 do {
927 prepare_to_wait(&sdp->sd_logd_waitq, &wait,
928 TASK_INTERRUPTIBLE);
929 if (!gfs2_ail_flush_reqd(sdp) &&
930 !gfs2_jrnl_flush_reqd(sdp) &&
931 !kthread_should_stop())
932 t = schedule_timeout(t);
933 } while(t && !gfs2_ail_flush_reqd(sdp) &&
934 !gfs2_jrnl_flush_reqd(sdp) &&
935 !kthread_should_stop());
936 finish_wait(&sdp->sd_logd_waitq, &wait);
939 return 0;