2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 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.
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/lm_interface.h>
18 #include <linux/delay.h>
33 * gfs2_struct2blk - compute stuff
34 * @sdp: the filesystem
35 * @nstruct: the number of structures
36 * @ssize: the size of the structures
38 * Compute the number of log descriptor blocks needed to hold a certain number
39 * of structures of a certain size.
41 * Returns: the number of blocks needed (minimum is always 1)
44 unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
48 unsigned int first, second;
51 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
53 if (nstruct > first) {
54 second = (sdp->sd_sb.sb_bsize -
55 sizeof(struct gfs2_meta_header)) / ssize;
56 blks += DIV_ROUND_UP(nstruct - first, second);
63 * gfs2_ail1_start_one - Start I/O on a part of the AIL
64 * @sdp: the filesystem
65 * @tr: the part of the AIL
69 static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
71 struct gfs2_bufdata *bd, *s;
72 struct buffer_head *bh;
75 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
80 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
84 gfs2_assert(sdp, bd->bd_ail == ai);
86 if (!buffer_busy(bh)) {
87 if (!buffer_uptodate(bh)) {
89 gfs2_io_error_bh(sdp, bh);
92 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
96 if (!buffer_dirty(bh))
99 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
101 gfs2_log_unlock(sdp);
103 ll_rw_block(WRITE, 1, &bh);
113 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
114 * @sdp: the filesystem
119 static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
121 struct gfs2_bufdata *bd, *s;
122 struct buffer_head *bh;
124 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
128 gfs2_assert(sdp, bd->bd_ail == ai);
130 if (buffer_busy(bh)) {
137 if (!buffer_uptodate(bh))
138 gfs2_io_error_bh(sdp, bh);
140 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
143 return list_empty(&ai->ai_ail1_list);
146 static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
148 struct list_head *head;
150 struct list_head *first;
151 struct gfs2_ail *first_ai, *ai, *tmp;
155 head = &sdp->sd_ail1_list;
156 if (list_empty(head)) {
157 gfs2_log_unlock(sdp);
160 sync_gen = sdp->sd_ail_sync_gen++;
163 first_ai = list_entry(first, struct gfs2_ail, ai_list);
164 first_ai->ai_sync_gen = sync_gen;
165 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
171 if (first && (head->prev != first ||
172 gfs2_ail1_empty_one(sdp, first_ai, 0)))
176 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
177 if (ai->ai_sync_gen >= sync_gen)
179 ai->ai_sync_gen = sync_gen;
180 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
186 gfs2_log_unlock(sdp);
189 int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
191 struct gfs2_ail *ai, *s;
196 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
197 if (gfs2_ail1_empty_one(sdp, ai, flags))
198 list_move(&ai->ai_list, &sdp->sd_ail2_list);
199 else if (!(flags & DIO_ALL))
203 ret = list_empty(&sdp->sd_ail1_list);
205 gfs2_log_unlock(sdp);
212 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
213 * @sdp: the filesystem
218 static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
220 struct list_head *head = &ai->ai_ail2_list;
221 struct gfs2_bufdata *bd;
222 struct gfs2_inode *bh_ip;
224 while (!list_empty(head)) {
225 bd = list_entry(head->prev, struct gfs2_bufdata,
227 gfs2_assert(sdp, bd->bd_ail == ai);
229 list_del(&bd->bd_ail_st_list);
230 list_del(&bd->bd_ail_gl_list);
231 atomic_dec(&bd->bd_gl->gl_ail_count);
232 if (bd->bd_bh->b_page->mapping) {
233 bh_ip = GFS2_I(bd->bd_bh->b_page->mapping->host);
234 gfs2_meta_cache_flush(bh_ip);
240 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
242 struct gfs2_ail *ai, *safe;
243 unsigned int old_tail = sdp->sd_log_tail;
244 int wrap = (new_tail < old_tail);
249 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
250 a = (old_tail <= ai->ai_first);
251 b = (ai->ai_first < new_tail);
252 rm = (wrap) ? (a || b) : (a && b);
256 gfs2_ail2_empty_one(sdp, ai);
257 list_del(&ai->ai_list);
258 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
259 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
263 gfs2_log_unlock(sdp);
267 * gfs2_log_reserve - Make a log reservation
268 * @sdp: The GFS2 superblock
269 * @blks: The number of blocks to reserve
271 * Note that we never give out the last few blocks of the journal. Thats
272 * due to the fact that there is a small number of header blocks
273 * associated with each log flush. The exact number can't be known until
274 * flush time, so we ensure that we have just enough free blocks at all
275 * times to avoid running out during a log flush.
280 int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
282 unsigned int try = 0;
283 unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
285 if (gfs2_assert_warn(sdp, blks) ||
286 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
289 mutex_lock(&sdp->sd_log_reserve_mutex);
291 while(sdp->sd_log_blks_free <= (blks + reserved_blks)) {
292 gfs2_log_unlock(sdp);
293 gfs2_ail1_empty(sdp, 0);
294 gfs2_log_flush(sdp, NULL);
297 gfs2_ail1_start(sdp, 0);
300 sdp->sd_log_blks_free -= blks;
301 gfs2_log_unlock(sdp);
302 mutex_unlock(&sdp->sd_log_reserve_mutex);
304 down_read(&sdp->sd_log_flush_lock);
310 * gfs2_log_release - Release a given number of log blocks
311 * @sdp: The GFS2 superblock
312 * @blks: The number of blocks
316 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
320 sdp->sd_log_blks_free += blks;
321 gfs2_assert_withdraw(sdp,
322 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
323 gfs2_log_unlock(sdp);
324 up_read(&sdp->sd_log_flush_lock);
327 static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
329 struct inode *inode = sdp->sd_jdesc->jd_inode;
331 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
333 bh_map.b_size = 1 << inode->i_blkbits;
334 error = gfs2_block_map(inode, lbn, 0, &bh_map);
335 if (error || !bh_map.b_blocknr)
336 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error,
337 (unsigned long long)bh_map.b_blocknr, lbn);
338 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
340 return bh_map.b_blocknr;
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,
360 dist = newer - older;
362 dist += sdp->sd_jdesc->jd_blocks;
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)
407 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
410 reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
411 sdp->sd_log_commited_databuf + databufhdrs_needed +
413 /* One for the overall header */
419 static unsigned int current_tail(struct gfs2_sbd *sdp)
426 if (list_empty(&sdp->sd_ail1_list)) {
427 tail = sdp->sd_log_head;
429 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
433 gfs2_log_unlock(sdp);
438 static inline void log_incr_head(struct gfs2_sbd *sdp)
440 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
441 gfs2_assert_withdraw(sdp, 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_get_buf - Get and initialize a buffer to use for log control data
451 * @sdp: The GFS2 superblock
453 * Returns: the buffer_head
456 struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
458 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
459 struct gfs2_log_buf *lb;
460 struct buffer_head *bh;
462 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
463 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
465 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
467 memset(bh->b_data, 0, bh->b_size);
468 set_buffer_uptodate(bh);
469 clear_buffer_dirty(bh);
478 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
479 * @sdp: the filesystem
480 * @data: the data the buffer_head should point to
482 * Returns: the log buffer descriptor
485 struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
486 struct buffer_head *real)
488 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
489 struct gfs2_log_buf *lb;
490 struct buffer_head *bh;
492 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
493 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
496 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
497 atomic_set(&bh->b_count, 1);
498 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
499 set_bh_page(bh, real->b_page, bh_offset(real));
500 bh->b_blocknr = blkno;
501 bh->b_size = sdp->sd_sb.sb_bsize;
502 bh->b_bdev = sdp->sd_vfs->s_bdev;
509 static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
511 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
513 ail2_empty(sdp, new_tail);
516 sdp->sd_log_blks_free += dist;
517 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
518 gfs2_log_unlock(sdp);
520 sdp->sd_log_tail = new_tail;
524 * log_write_header - Get and initialize a journal header buffer
525 * @sdp: The GFS2 superblock
527 * Returns: the initialized log buffer descriptor
530 static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
532 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
533 struct buffer_head *bh;
534 struct gfs2_log_header *lh;
538 bh = sb_getblk(sdp->sd_vfs, blkno);
540 memset(bh->b_data, 0, bh->b_size);
541 set_buffer_uptodate(bh);
542 clear_buffer_dirty(bh);
545 gfs2_ail1_empty(sdp, 0);
546 tail = current_tail(sdp);
548 lh = (struct gfs2_log_header *)bh->b_data;
549 memset(lh, 0, sizeof(struct gfs2_log_header));
550 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
551 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
552 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
553 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
554 lh->lh_flags = cpu_to_be32(flags);
555 lh->lh_tail = cpu_to_be32(tail);
556 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
557 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
558 lh->lh_hash = cpu_to_be32(hash);
560 set_buffer_dirty(bh);
561 if (sync_dirty_buffer(bh))
562 gfs2_io_error_bh(sdp, bh);
565 if (sdp->sd_log_tail != tail)
566 log_pull_tail(sdp, tail);
568 gfs2_assert_withdraw(sdp, !pull);
570 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
574 static void log_flush_commit(struct gfs2_sbd *sdp)
576 struct list_head *head = &sdp->sd_log_flush_list;
577 struct gfs2_log_buf *lb;
578 struct buffer_head *bh;
581 while (!list_empty(head)) {
582 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
583 list_del(&lb->lb_list);
587 if (!buffer_uptodate(bh))
588 gfs2_io_error_bh(sdp, bh);
590 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
592 free_buffer_head(bh);
599 /* If nothing was journaled, the header is unplanned and unwanted. */
601 log_write_header(sdp, 0, 0);
604 tail = current_tail(sdp);
606 gfs2_ail1_empty(sdp, 0);
607 if (sdp->sd_log_tail != tail)
608 log_pull_tail(sdp, tail);
613 * gfs2_log_flush - flush incore transaction(s)
614 * @sdp: the filesystem
615 * @gl: The glock structure to flush. If NULL, flush the whole incore log
619 void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
623 down_write(&sdp->sd_log_flush_lock);
627 if (list_empty(&gl->gl_le.le_list)) {
628 gfs2_log_unlock(sdp);
629 up_write(&sdp->sd_log_flush_lock);
632 gfs2_log_unlock(sdp);
635 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
636 INIT_LIST_HEAD(&ai->ai_ail1_list);
637 INIT_LIST_HEAD(&ai->ai_ail2_list);
639 gfs2_assert_withdraw(sdp,
640 sdp->sd_log_num_buf + sdp->sd_log_num_jdata ==
641 sdp->sd_log_commited_buf +
642 sdp->sd_log_commited_databuf);
643 gfs2_assert_withdraw(sdp,
644 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
646 sdp->sd_log_flush_head = sdp->sd_log_head;
647 sdp->sd_log_flush_wrapped = 0;
648 ai->ai_first = sdp->sd_log_flush_head;
650 lops_before_commit(sdp);
651 if (!list_empty(&sdp->sd_log_flush_list))
652 log_flush_commit(sdp);
653 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
655 sdp->sd_log_blks_free--; /* Adjust for unreserved buffer */
656 gfs2_log_unlock(sdp);
657 log_write_header(sdp, 0, PULL);
659 lops_after_commit(sdp, ai);
662 sdp->sd_log_head = sdp->sd_log_flush_head;
663 sdp->sd_log_blks_reserved = 0;
664 sdp->sd_log_commited_buf = 0;
665 sdp->sd_log_commited_databuf = 0;
666 sdp->sd_log_commited_revoke = 0;
668 if (!list_empty(&ai->ai_ail1_list)) {
669 list_add(&ai->ai_list, &sdp->sd_ail1_list);
672 gfs2_log_unlock(sdp);
674 sdp->sd_vfs->s_dirt = 0;
675 up_write(&sdp->sd_log_flush_lock);
680 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
682 unsigned int reserved;
687 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
688 sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
689 tr->tr_num_databuf_rm;
690 gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
691 (((int)sdp->sd_log_commited_databuf) >= 0));
692 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
693 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
694 reserved = calc_reserved(sdp);
695 old = sdp->sd_log_blks_free;
696 sdp->sd_log_blks_free += tr->tr_reserved -
697 (reserved - sdp->sd_log_blks_reserved);
699 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
700 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <=
701 sdp->sd_jdesc->jd_blocks);
703 sdp->sd_log_blks_reserved = reserved;
705 gfs2_log_unlock(sdp);
709 * gfs2_log_commit - Commit a transaction to the log
710 * @sdp: the filesystem
711 * @tr: the transaction
716 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
719 lops_incore_commit(sdp, tr);
721 sdp->sd_vfs->s_dirt = 1;
722 up_read(&sdp->sd_log_flush_lock);
725 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
726 wake_up_process(sdp->sd_logd_process);
727 gfs2_log_unlock(sdp);
731 * gfs2_log_shutdown - write a shutdown header into a journal
732 * @sdp: the filesystem
736 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
738 down_write(&sdp->sd_log_flush_lock);
740 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
741 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
742 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
743 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
744 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
745 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
746 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
747 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
749 sdp->sd_log_flush_head = sdp->sd_log_head;
750 sdp->sd_log_flush_wrapped = 0;
752 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
753 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
755 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
756 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
757 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
759 sdp->sd_log_head = sdp->sd_log_flush_head;
760 sdp->sd_log_tail = sdp->sd_log_head;
762 up_write(&sdp->sd_log_flush_lock);
767 * gfs2_meta_syncfs - sync all the buffers in a filesystem
768 * @sdp: the filesystem
772 void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
774 gfs2_log_flush(sdp, NULL);
776 gfs2_ail1_start(sdp, DIO_ALL);
777 if (gfs2_ail1_empty(sdp, DIO_ALL))