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>
32 * gfs2_struct2blk - compute stuff
33 * @sdp: the filesystem
34 * @nstruct: the number of structures
35 * @ssize: the size of the structures
37 * Compute the number of log descriptor blocks needed to hold a certain number
38 * of structures of a certain size.
40 * Returns: the number of blocks needed (minimum is always 1)
43 unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
47 unsigned int first, second;
50 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
52 if (nstruct > first) {
53 second = (sdp->sd_sb.sb_bsize -
54 sizeof(struct gfs2_meta_header)) / ssize;
55 blks += DIV_ROUND_UP(nstruct - first, second);
62 * gfs2_ail1_start_one - Start I/O on a part of the AIL
63 * @sdp: the filesystem
64 * @tr: the part of the AIL
68 static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
70 struct gfs2_bufdata *bd, *s;
71 struct buffer_head *bh;
74 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
79 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
83 gfs2_assert(sdp, bd->bd_ail == ai);
85 if (!buffer_busy(bh)) {
86 if (!buffer_uptodate(bh)) {
88 gfs2_io_error_bh(sdp, bh);
91 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
95 if (!buffer_dirty(bh))
98 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
100 gfs2_log_unlock(sdp);
102 ll_rw_block(WRITE, 1, &bh);
112 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
113 * @sdp: the filesystem
118 static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
120 struct gfs2_bufdata *bd, *s;
121 struct buffer_head *bh;
123 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
127 gfs2_assert(sdp, bd->bd_ail == ai);
129 if (buffer_busy(bh)) {
136 if (!buffer_uptodate(bh))
137 gfs2_io_error_bh(sdp, bh);
139 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
142 return list_empty(&ai->ai_ail1_list);
145 void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
147 struct list_head *head = &sdp->sd_ail1_list;
149 struct list_head *first;
150 struct gfs2_ail *first_ai, *ai, *tmp;
154 if (list_empty(head)) {
155 gfs2_log_unlock(sdp);
158 sync_gen = sdp->sd_ail_sync_gen++;
161 first_ai = list_entry(first, struct gfs2_ail, ai_list);
162 first_ai->ai_sync_gen = sync_gen;
163 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
169 if (first && (head->prev != first ||
170 gfs2_ail1_empty_one(sdp, first_ai, 0)))
174 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
175 if (ai->ai_sync_gen >= sync_gen)
177 ai->ai_sync_gen = sync_gen;
178 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
184 gfs2_log_unlock(sdp);
187 int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
189 struct gfs2_ail *ai, *s;
194 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
195 if (gfs2_ail1_empty_one(sdp, ai, flags))
196 list_move(&ai->ai_list, &sdp->sd_ail2_list);
197 else if (!(flags & DIO_ALL))
201 ret = list_empty(&sdp->sd_ail1_list);
203 gfs2_log_unlock(sdp);
210 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
211 * @sdp: the filesystem
216 static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
218 struct list_head *head = &ai->ai_ail2_list;
219 struct gfs2_bufdata *bd;
221 while (!list_empty(head)) {
222 bd = list_entry(head->prev, struct gfs2_bufdata,
224 gfs2_assert(sdp, bd->bd_ail == ai);
226 list_del(&bd->bd_ail_st_list);
227 list_del(&bd->bd_ail_gl_list);
228 atomic_dec(&bd->bd_gl->gl_ail_count);
233 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
235 struct gfs2_ail *ai, *safe;
236 unsigned int old_tail = sdp->sd_log_tail;
237 int wrap = (new_tail < old_tail);
242 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
243 a = (old_tail <= ai->ai_first);
244 b = (ai->ai_first < new_tail);
245 rm = (wrap) ? (a || b) : (a && b);
249 gfs2_ail2_empty_one(sdp, ai);
250 list_del(&ai->ai_list);
251 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
252 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
256 gfs2_log_unlock(sdp);
260 * gfs2_log_reserve - Make a log reservation
261 * @sdp: The GFS2 superblock
262 * @blks: The number of blocks to reserve
264 * Note that we never give out the last 6 blocks of the journal. Thats
265 * due to the fact that there is are a small number of header blocks
266 * associated with each log flush. The exact number can't be known until
267 * flush time, so we ensure that we have just enough free blocks at all
268 * times to avoid running out during a log flush.
273 int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
275 unsigned int try = 0;
277 if (gfs2_assert_warn(sdp, blks) ||
278 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
281 mutex_lock(&sdp->sd_log_reserve_mutex);
283 while(sdp->sd_log_blks_free <= (blks + 6)) {
284 gfs2_log_unlock(sdp);
285 gfs2_ail1_empty(sdp, 0);
286 gfs2_log_flush(sdp, NULL);
289 gfs2_ail1_start(sdp, 0);
292 sdp->sd_log_blks_free -= blks;
293 gfs2_log_unlock(sdp);
294 mutex_unlock(&sdp->sd_log_reserve_mutex);
296 down_read(&sdp->sd_log_flush_lock);
302 * gfs2_log_release - Release a given number of log blocks
303 * @sdp: The GFS2 superblock
304 * @blks: The number of blocks
308 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
312 sdp->sd_log_blks_free += blks;
313 gfs2_assert_withdraw(sdp,
314 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
315 gfs2_log_unlock(sdp);
316 up_read(&sdp->sd_log_flush_lock);
319 static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
321 struct inode *inode = sdp->sd_jdesc->jd_inode;
323 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
325 bh_map.b_size = 1 << inode->i_blkbits;
326 error = gfs2_block_map(inode, lbn, 0, &bh_map);
327 if (error || !bh_map.b_blocknr)
328 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, bh_map.b_blocknr, lbn);
329 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
331 return bh_map.b_blocknr;
335 * log_distance - Compute distance between two journal blocks
336 * @sdp: The GFS2 superblock
337 * @newer: The most recent journal block of the pair
338 * @older: The older journal block of the pair
340 * Compute the distance (in the journal direction) between two
341 * blocks in the journal
343 * Returns: the distance in blocks
346 static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
351 dist = newer - older;
353 dist += sdp->sd_jdesc->jd_blocks;
358 static unsigned int current_tail(struct gfs2_sbd *sdp)
365 if (list_empty(&sdp->sd_ail1_list)) {
366 tail = sdp->sd_log_head;
368 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
372 gfs2_log_unlock(sdp);
377 static inline void log_incr_head(struct gfs2_sbd *sdp)
379 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
380 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
382 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
383 sdp->sd_log_flush_head = 0;
384 sdp->sd_log_flush_wrapped = 1;
389 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
390 * @sdp: The GFS2 superblock
392 * Returns: the buffer_head
395 struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
397 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
398 struct gfs2_log_buf *lb;
399 struct buffer_head *bh;
401 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
402 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
404 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
406 memset(bh->b_data, 0, bh->b_size);
407 set_buffer_uptodate(bh);
408 clear_buffer_dirty(bh);
417 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
418 * @sdp: the filesystem
419 * @data: the data the buffer_head should point to
421 * Returns: the log buffer descriptor
424 struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
425 struct buffer_head *real)
427 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
428 struct gfs2_log_buf *lb;
429 struct buffer_head *bh;
431 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
432 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
435 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
436 atomic_set(&bh->b_count, 1);
437 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
438 set_bh_page(bh, real->b_page, bh_offset(real));
439 bh->b_blocknr = blkno;
440 bh->b_size = sdp->sd_sb.sb_bsize;
441 bh->b_bdev = sdp->sd_vfs->s_bdev;
448 static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
450 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
452 ail2_empty(sdp, new_tail);
455 sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
456 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
457 gfs2_log_unlock(sdp);
459 sdp->sd_log_tail = new_tail;
463 * log_write_header - Get and initialize a journal header buffer
464 * @sdp: The GFS2 superblock
466 * Returns: the initialized log buffer descriptor
469 static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
471 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
472 struct buffer_head *bh;
473 struct gfs2_log_header *lh;
477 bh = sb_getblk(sdp->sd_vfs, blkno);
479 memset(bh->b_data, 0, bh->b_size);
480 set_buffer_uptodate(bh);
481 clear_buffer_dirty(bh);
484 gfs2_ail1_empty(sdp, 0);
485 tail = current_tail(sdp);
487 lh = (struct gfs2_log_header *)bh->b_data;
488 memset(lh, 0, sizeof(struct gfs2_log_header));
489 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
490 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
491 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
492 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
493 lh->lh_flags = cpu_to_be32(flags);
494 lh->lh_tail = cpu_to_be32(tail);
495 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
496 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
497 lh->lh_hash = cpu_to_be32(hash);
499 set_buffer_dirty(bh);
500 if (sync_dirty_buffer(bh))
501 gfs2_io_error_bh(sdp, bh);
504 if (sdp->sd_log_tail != tail)
505 log_pull_tail(sdp, tail, pull);
507 gfs2_assert_withdraw(sdp, !pull);
509 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
513 static void log_flush_commit(struct gfs2_sbd *sdp)
515 struct list_head *head = &sdp->sd_log_flush_list;
516 struct gfs2_log_buf *lb;
517 struct buffer_head *bh;
519 while (!list_empty(head)) {
520 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
521 list_del(&lb->lb_list);
525 if (!buffer_uptodate(bh))
526 gfs2_io_error_bh(sdp, bh);
528 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
530 free_buffer_head(bh);
536 log_write_header(sdp, 0, 0);
540 * gfs2_log_flush - flush incore transaction(s)
541 * @sdp: the filesystem
542 * @gl: The glock structure to flush. If NULL, flush the whole incore log
546 void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
550 down_write(&sdp->sd_log_flush_lock);
554 if (list_empty(&gl->gl_le.le_list)) {
555 gfs2_log_unlock(sdp);
556 up_write(&sdp->sd_log_flush_lock);
559 gfs2_log_unlock(sdp);
562 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
563 INIT_LIST_HEAD(&ai->ai_ail1_list);
564 INIT_LIST_HEAD(&ai->ai_ail2_list);
566 gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
567 gfs2_assert_withdraw(sdp,
568 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
570 sdp->sd_log_flush_head = sdp->sd_log_head;
571 sdp->sd_log_flush_wrapped = 0;
572 ai->ai_first = sdp->sd_log_flush_head;
574 lops_before_commit(sdp);
575 if (!list_empty(&sdp->sd_log_flush_list))
576 log_flush_commit(sdp);
577 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
578 log_write_header(sdp, 0, PULL);
579 lops_after_commit(sdp, ai);
582 sdp->sd_log_head = sdp->sd_log_flush_head;
583 sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
584 sdp->sd_log_blks_reserved = 0;
585 sdp->sd_log_commited_buf = 0;
586 sdp->sd_log_num_hdrs = 0;
587 sdp->sd_log_commited_revoke = 0;
589 if (!list_empty(&ai->ai_ail1_list)) {
590 list_add(&ai->ai_list, &sdp->sd_ail1_list);
593 gfs2_log_unlock(sdp);
595 sdp->sd_vfs->s_dirt = 0;
596 up_write(&sdp->sd_log_flush_lock);
601 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
603 unsigned int reserved = 0;
608 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
609 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
610 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
611 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
613 if (sdp->sd_log_commited_buf)
614 reserved += sdp->sd_log_commited_buf;
615 if (sdp->sd_log_commited_revoke)
616 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
621 old = sdp->sd_log_blks_free;
622 sdp->sd_log_blks_free += tr->tr_reserved -
623 (reserved - sdp->sd_log_blks_reserved);
625 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
626 gfs2_assert_withdraw(sdp,
627 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
628 sdp->sd_log_num_hdrs);
630 sdp->sd_log_blks_reserved = reserved;
632 gfs2_log_unlock(sdp);
636 * gfs2_log_commit - Commit a transaction to the log
637 * @sdp: the filesystem
638 * @tr: the transaction
643 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
646 lops_incore_commit(sdp, tr);
648 sdp->sd_vfs->s_dirt = 1;
649 up_read(&sdp->sd_log_flush_lock);
652 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
653 wake_up_process(sdp->sd_logd_process);
654 gfs2_log_unlock(sdp);
658 * gfs2_log_shutdown - write a shutdown header into a journal
659 * @sdp: the filesystem
663 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
665 down_write(&sdp->sd_log_flush_lock);
667 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
668 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
669 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
670 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
671 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
672 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
673 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
674 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
675 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
677 sdp->sd_log_flush_head = sdp->sd_log_head;
678 sdp->sd_log_flush_wrapped = 0;
680 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
682 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
683 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
684 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
686 sdp->sd_log_head = sdp->sd_log_flush_head;
687 sdp->sd_log_tail = sdp->sd_log_head;
689 up_write(&sdp->sd_log_flush_lock);